[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 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] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 5 | #include "net/url_request/url_request_unittest.h" |
| 6 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7 | #include "build/build_config.h" |
| 8 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 9 | #if defined(OS_WIN) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 10 | #include <windows.h> |
| 11 | #include <shlobj.h> |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 12 | #elif defined(OS_LINUX) |
| 13 | #include "base/nss_init.h" |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 14 | #endif |
| 15 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <string> |
| 18 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 19 | #include "base/file_util.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 20 | #include "base/format_macros.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] | 73f5d66 | 2008-11-20 01:08:17 | [diff] [blame] | 24 | #include "base/string_piece.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 25 | #include "base/string_util.h" |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 26 | #include "net/base/cookie_monster.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | #include "net/base/load_flags.h" |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 28 | #include "net/base/load_log.h" |
| 29 | #include "net/base/load_log_unittest.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 30 | #include "net/base/net_errors.h" |
| 31 | #include "net/base/net_module.h" |
| 32 | #include "net/base/net_util.h" |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 33 | #include "net/base/upload_data.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | #include "net/disk_cache/disk_cache.h" |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 35 | #include "net/ftp/ftp_network_layer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 36 | #include "net/http/http_cache.h" |
| 37 | #include "net/http/http_network_layer.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 38 | #include "net/http/http_response_headers.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 39 | #include "net/proxy/proxy_service.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 40 | #include "net/socket/ssl_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 41 | #include "net/url_request/url_request.h" |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 42 | #include "net/url_request/url_request_file_dir_job.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 43 | #include "net/url_request/url_request_http_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 44 | #include "net/url_request/url_request_test_job.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 45 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 46 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 47 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 48 | using base::Time; |
| 49 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | namespace { |
| 51 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 52 | class URLRequestTestContext : public URLRequestContext { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 53 | public: |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 54 | URLRequestTestContext() { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 55 | host_resolver_ = net::CreateSystemHostResolver(); |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 56 | proxy_service_ = net::ProxyService::CreateNull(); |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 57 | ftp_transaction_factory_ = new net::FtpNetworkLayer(host_resolver_); |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 58 | ssl_config_service_ = new net::SSLConfigServiceDefaults; |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 59 | http_transaction_factory_ = |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 60 | new net::HttpCache( |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 61 | net::HttpNetworkLayer::CreateFactory(host_resolver_, proxy_service_, |
| 62 | ssl_config_service_), |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 63 | disk_cache::CreateInMemoryCacheBackend(0)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 64 | // In-memory cookie store. |
| 65 | cookie_store_ = new net::CookieMonster(); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 66 | accept_language_ = "en-us,fr"; |
| 67 | accept_charset_ = "iso-8859-1,*,utf-8"; |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 70 | private: |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 71 | virtual ~URLRequestTestContext() { |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 72 | delete ftp_transaction_factory_; |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 73 | delete http_transaction_factory_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
| 77 | class TestURLRequest : public URLRequest { |
| 78 | public: |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 79 | TestURLRequest(const GURL& url, Delegate* delegate) |
| 80 | : URLRequest(url, delegate) { |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 81 | set_context(new URLRequestTestContext()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 82 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 83 | }; |
| 84 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 85 | base::StringPiece TestNetResourceProvider(int key) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 86 | return "header"; |
| 87 | } |
| 88 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 89 | // Do a case-insensitive search through |haystack| for |needle|. |
| 90 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 91 | std::string::const_iterator it = |
| 92 | std::search(haystack.begin(), |
| 93 | haystack.end(), |
| 94 | needle, |
| 95 | needle + strlen(needle), |
| 96 | CaseInsensitiveCompare<char>()); |
| 97 | return it != haystack.end(); |
| 98 | } |
| 99 | |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 100 | void FillBuffer(char* buffer, size_t len) { |
| 101 | static bool called = false; |
| 102 | if (!called) { |
| 103 | called = true; |
| 104 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 105 | srand(seed); |
| 106 | } |
| 107 | |
| 108 | for (size_t i = 0; i < len; i++) { |
| 109 | buffer[i] = static_cast<char>(rand()); |
| 110 | if (!buffer[i]) |
| 111 | buffer[i] = 'g'; |
| 112 | } |
| 113 | } |
| 114 | |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 115 | scoped_refptr<net::UploadData> CreateSimpleUploadData(const char* data) { |
| 116 | scoped_refptr<net::UploadData> upload = new net::UploadData; |
| 117 | upload->AppendBytes(data, strlen(data)); |
| 118 | return upload; |
| 119 | } |
| 120 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 121 | } // namespace |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 122 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 123 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |
| 124 | class URLRequestTest : public PlatformTest { |
| 125 | }; |
| 126 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 127 | class URLRequestTestHTTP : public URLRequestTest { |
| 128 | protected: |
| 129 | static void SetUpTestCase() { |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 130 | server_ = HTTPTestServer::CreateForkingServer( |
| 131 | L"net/data/url_request_unittest/"); |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void TearDownTestCase() { |
| 135 | server_ = NULL; |
| 136 | } |
| 137 | |
| 138 | static scoped_refptr<HTTPTestServer> server_; |
| 139 | }; |
| 140 | |
| 141 | // static |
| 142 | scoped_refptr<HTTPTestServer> URLRequestTestHTTP::server_; |
| 143 | |
| 144 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 145 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 146 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 147 | // The HTTPTestServer will return a 302 response, which we should not |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 148 | // follow. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 149 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 150 | TestDelegate d; |
| 151 | { |
| 152 | URLRequest r(GURL("https://www.redirect.com/"), &d); |
| 153 | std::string proxy("localhost:"); |
| 154 | proxy.append(IntToString(kHTTPDefaultPort)); |
| 155 | r.set_context(new TestURLRequestContext(proxy)); |
| 156 | |
| 157 | r.Start(); |
| 158 | EXPECT_TRUE(r.is_pending()); |
| 159 | |
| 160 | MessageLoop::current()->Run(); |
| 161 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 162 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
| 163 | EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 164 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 165 | // We should not have followed the redirect. |
| 166 | EXPECT_EQ(0, d.received_redirect_count()); |
| 167 | } |
| 168 | } |
| 169 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 170 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 171 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 172 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 173 | // The HTTPTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 174 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 175 | TestDelegate d; |
| 176 | { |
| 177 | URLRequest r(GURL("https://www.server-auth.com/"), &d); |
| 178 | std::string proxy("localhost:"); |
| 179 | proxy.append(IntToString(kHTTPDefaultPort)); |
| 180 | r.set_context(new TestURLRequestContext(proxy)); |
| 181 | |
| 182 | r.Start(); |
| 183 | EXPECT_TRUE(r.is_pending()); |
| 184 | |
| 185 | MessageLoop::current()->Run(); |
| 186 | |
| 187 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 188 | EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 192 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
| 193 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 194 | TestDelegate d; |
| 195 | { |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 196 | TestURLRequest r(server_->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 197 | |
| 198 | r.Start(); |
| 199 | EXPECT_TRUE(r.is_pending()); |
| 200 | |
| 201 | MessageLoop::current()->Run(); |
| 202 | |
| 203 | EXPECT_EQ(1, d.response_started_count()); |
| 204 | EXPECT_FALSE(d.received_data_before_response()); |
| 205 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 206 | |
[email protected] | ab6a5c4 | 2009-11-13 00:25:29 | [diff] [blame] | 207 | // The first part of the log will be for URL_REQUEST_START. |
| 208 | // After that, there should be an HTTP_TRANSACTION_READ_BODY |
| 209 | EXPECT_TRUE( |
| 210 | net::LogContains(*r.load_log(), 0, |
| 211 | net::LoadLog::TYPE_URL_REQUEST_START, |
| 212 | net::LoadLog::PHASE_BEGIN)); |
| 213 | EXPECT_TRUE( |
| 214 | net::LogContains(*r.load_log(), -3, |
| 215 | net::LoadLog::TYPE_URL_REQUEST_START, |
| 216 | net::LoadLog::PHASE_END)); |
| 217 | EXPECT_TRUE( |
| 218 | net::LogContains(*r.load_log(), -2, |
| 219 | net::LoadLog::TYPE_HTTP_TRANSACTION_READ_BODY, |
| 220 | net::LoadLog::PHASE_BEGIN)); |
| 221 | EXPECT_TRUE( |
| 222 | net::LogContains(*r.load_log(), -1, |
| 223 | net::LoadLog::TYPE_HTTP_TRANSACTION_READ_BODY, |
| 224 | net::LoadLog::PHASE_END)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 225 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 228 | TEST_F(URLRequestTestHTTP, GetTest) { |
| 229 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 230 | TestDelegate d; |
| 231 | { |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 232 | TestURLRequest r(server_->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 233 | |
| 234 | r.Start(); |
| 235 | EXPECT_TRUE(r.is_pending()); |
| 236 | |
| 237 | MessageLoop::current()->Run(); |
| 238 | |
| 239 | EXPECT_EQ(1, d.response_started_count()); |
| 240 | EXPECT_FALSE(d.received_data_before_response()); |
| 241 | EXPECT_NE(0, d.bytes_received()); |
| 242 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 245 | TEST_F(URLRequestTest, QuitTest) { |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 246 | // Don't use shared server here because we order it to quit. |
| 247 | // It would impact other tests. |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 248 | scoped_refptr<HTTPTestServer> server = |
| 249 | HTTPTestServer::CreateServer(L"", NULL); |
| 250 | ASSERT_TRUE(NULL != server.get()); |
| 251 | server->SendQuit(); |
| 252 | EXPECT_TRUE(server->WaitToFinish(20000)); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | class HTTPSRequestTest : public testing::Test { |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | #if defined(OS_MACOSX) |
[email protected] | 3016ded | 2009-06-19 18:27:59 | [diff] [blame] | 259 | // Status 6/19/09: |
| 260 | // |
| 261 | // If these tests are enabled on OSX, the first one (HTTPSGetTest) |
| 262 | // will fail. I didn't track it down explicitly, but did observe that |
| 263 | // the testserver.py kills itself (e.g. "process_util_posix.cc(84)] |
| 264 | // Unable to terminate process."). tlslite and testserver.py are hard |
| 265 | // to debug (redirection of stdout/stderr to a file so you can't see |
| 266 | // errors; lots of naked "except:" statements, etc), but I did track |
| 267 | // down an SSL auth failure as one cause of it deciding to die |
| 268 | // silently. |
| 269 | // |
| 270 | // The next test, HTTPSMismatchedTest, will look like it hangs by |
| 271 | // looping over calls to SSLHandshake() (Security framework call) as |
| 272 | // called from SSLClientSocketMac::DoHandshake(). Return values are a |
| 273 | // repeating pattern of -9803 (come back later) and -9812 (cert valid |
| 274 | // but root not trusted). If you don't have the cert in your keychain |
| 275 | // as documented on http://dev.chromium.org/developers/testing, the |
| 276 | // -9812 becomes a -9813 (no root cert). Interestingly, the handshake |
| 277 | // also appears to be a failure point for other disabled tests, such |
| 278 | // as (SSLClientSocketTest,Connect) in |
| 279 | // net/base/ssl_client_socket_unittest.cc. |
| 280 | // |
| 281 | // Old comment (not sure if obsolete): |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 282 | // ssl_client_socket_mac.cc crashes currently in GetSSLInfo |
| 283 | // when called on a connection with an unrecognized certificate |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 284 | #define MAYBE_HTTPSGetTest DISABLED_HTTPSGetTest |
| 285 | #define MAYBE_HTTPSMismatchedTest DISABLED_HTTPSMismatchedTest |
| 286 | #define MAYBE_HTTPSExpiredTest DISABLED_HTTPSExpiredTest |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 287 | #else |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 288 | #define MAYBE_HTTPSGetTest HTTPSGetTest |
| 289 | #define MAYBE_HTTPSMismatchedTest HTTPSMismatchedTest |
| 290 | #define MAYBE_HTTPSExpiredTest HTTPSExpiredTest |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 291 | #endif |
| 292 | |
| 293 | TEST_F(HTTPSRequestTest, MAYBE_HTTPSGetTest) { |
| 294 | // Note: tools/testserver/testserver.py does not need |
| 295 | // a working document root to server the pages / and /hello.html, |
| 296 | // so this test doesn't really need to specify a document root. |
| 297 | // But if it did, a good one would be net/data/ssl. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 298 | scoped_refptr<HTTPSTestServer> server = |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 299 | HTTPSTestServer::CreateGoodServer(L"net/data/ssl"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 300 | ASSERT_TRUE(NULL != server.get()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 301 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 302 | TestDelegate d; |
| 303 | { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 304 | TestURLRequest r(server->TestServerPage(""), &d); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 305 | |
| 306 | r.Start(); |
| 307 | EXPECT_TRUE(r.is_pending()); |
| 308 | |
| 309 | MessageLoop::current()->Run(); |
| 310 | |
| 311 | EXPECT_EQ(1, d.response_started_count()); |
| 312 | EXPECT_FALSE(d.received_data_before_response()); |
| 313 | EXPECT_NE(0, d.bytes_received()); |
| 314 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 315 | } |
| 316 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 317 | TEST_F(HTTPSRequestTest, MAYBE_HTTPSMismatchedTest) { |
| 318 | scoped_refptr<HTTPSTestServer> server = |
| 319 | HTTPSTestServer::CreateMismatchedServer(L"net/data/ssl"); |
| 320 | ASSERT_TRUE(NULL != server.get()); |
| 321 | |
| 322 | bool err_allowed = true; |
| 323 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 324 | TestDelegate d; |
| 325 | { |
| 326 | d.set_allow_certificate_errors(err_allowed); |
| 327 | TestURLRequest r(server->TestServerPage(""), &d); |
| 328 | |
| 329 | r.Start(); |
| 330 | EXPECT_TRUE(r.is_pending()); |
| 331 | |
| 332 | MessageLoop::current()->Run(); |
| 333 | |
| 334 | EXPECT_EQ(1, d.response_started_count()); |
| 335 | EXPECT_FALSE(d.received_data_before_response()); |
| 336 | EXPECT_TRUE(d.have_certificate_errors()); |
| 337 | if (err_allowed) |
| 338 | EXPECT_NE(0, d.bytes_received()); |
| 339 | else |
| 340 | EXPECT_EQ(0, d.bytes_received()); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | TEST_F(HTTPSRequestTest, MAYBE_HTTPSExpiredTest) { |
| 346 | scoped_refptr<HTTPSTestServer> server = |
| 347 | HTTPSTestServer::CreateExpiredServer(L"net/data/ssl"); |
| 348 | ASSERT_TRUE(NULL != server.get()); |
| 349 | |
| 350 | // Iterate from false to true, just so that we do the opposite of the |
| 351 | // previous test in order to increase test coverage. |
| 352 | bool err_allowed = false; |
| 353 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 354 | TestDelegate d; |
| 355 | { |
| 356 | d.set_allow_certificate_errors(err_allowed); |
| 357 | TestURLRequest r(server->TestServerPage(""), &d); |
| 358 | |
| 359 | r.Start(); |
| 360 | EXPECT_TRUE(r.is_pending()); |
| 361 | |
| 362 | MessageLoop::current()->Run(); |
| 363 | |
| 364 | EXPECT_EQ(1, d.response_started_count()); |
| 365 | EXPECT_FALSE(d.received_data_before_response()); |
| 366 | EXPECT_TRUE(d.have_certificate_errors()); |
| 367 | if (err_allowed) |
| 368 | EXPECT_NE(0, d.bytes_received()); |
| 369 | else |
| 370 | EXPECT_EQ(0, d.bytes_received()); |
| 371 | } |
| 372 | } |
| 373 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 374 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 375 | TEST_F(URLRequestTestHTTP, CancelTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 376 | TestDelegate d; |
| 377 | { |
| 378 | TestURLRequest r(GURL("http://www.google.com/"), &d); |
| 379 | |
| 380 | r.Start(); |
| 381 | EXPECT_TRUE(r.is_pending()); |
| 382 | |
| 383 | r.Cancel(); |
| 384 | |
| 385 | MessageLoop::current()->Run(); |
| 386 | |
| 387 | // We expect to receive OnResponseStarted even though the request has been |
| 388 | // cancelled. |
| 389 | EXPECT_EQ(1, d.response_started_count()); |
| 390 | EXPECT_EQ(0, d.bytes_received()); |
| 391 | EXPECT_FALSE(d.received_data_before_response()); |
| 392 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 395 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
| 396 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 397 | |
| 398 | // error C2446: '!=' : no conversion from 'HTTPTestServer *const ' |
| 399 | // to 'const int' |
| 400 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 401 | TestDelegate d; |
| 402 | { |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 403 | TestURLRequest r(server_->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 404 | |
| 405 | d.set_cancel_in_response_started(true); |
| 406 | |
| 407 | r.Start(); |
| 408 | EXPECT_TRUE(r.is_pending()); |
| 409 | |
| 410 | MessageLoop::current()->Run(); |
| 411 | |
| 412 | EXPECT_EQ(1, d.response_started_count()); |
| 413 | EXPECT_EQ(0, d.bytes_received()); |
| 414 | EXPECT_FALSE(d.received_data_before_response()); |
| 415 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 416 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 417 | } |
| 418 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 419 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
| 420 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 421 | TestDelegate d; |
| 422 | { |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 423 | TestURLRequest r(server_->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 424 | |
| 425 | d.set_cancel_in_received_data(true); |
| 426 | |
| 427 | r.Start(); |
| 428 | EXPECT_TRUE(r.is_pending()); |
| 429 | |
| 430 | MessageLoop::current()->Run(); |
| 431 | |
| 432 | EXPECT_EQ(1, d.response_started_count()); |
| 433 | // There is no guarantee about how much data was received |
| 434 | // before the cancel was issued. It could have been 0 bytes, |
| 435 | // or it could have been all the bytes. |
| 436 | // EXPECT_EQ(0, d.bytes_received()); |
| 437 | EXPECT_FALSE(d.received_data_before_response()); |
| 438 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 439 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 440 | } |
| 441 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 442 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
| 443 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 444 | TestDelegate d; |
| 445 | { |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 446 | TestURLRequest r(server_->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 447 | |
| 448 | r.Start(); |
| 449 | EXPECT_TRUE(r.is_pending()); |
| 450 | |
| 451 | // The request will be implicitly canceled when it is destroyed. The |
| 452 | // test delegate must not post a quit message when this happens because |
| 453 | // this test doesn't actually have a message loop. The quit message would |
| 454 | // get put on this thread's message queue and the next test would exit |
| 455 | // early, causing problems. |
| 456 | d.set_quit_on_complete(false); |
| 457 | } |
| 458 | // expect things to just cleanup properly. |
| 459 | |
| 460 | // we won't actually get a received reponse here because we've never run the |
| 461 | // message loop |
| 462 | EXPECT_FALSE(d.received_data_before_response()); |
| 463 | EXPECT_EQ(0, d.bytes_received()); |
| 464 | } |
| 465 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 466 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
| 467 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 468 | scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 469 | |
| 470 | // populate cache |
| 471 | { |
| 472 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 473 | URLRequest r(server_->TestServerPage("cachetime"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 474 | r.set_context(context); |
| 475 | r.Start(); |
| 476 | MessageLoop::current()->Run(); |
| 477 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 478 | } |
| 479 | |
| 480 | // cancel read from cache (see bug 990242) |
| 481 | { |
| 482 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 483 | URLRequest r(server_->TestServerPage("cachetime"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 484 | r.set_context(context); |
| 485 | r.Start(); |
| 486 | r.Cancel(); |
| 487 | MessageLoop::current()->Run(); |
| 488 | |
| 489 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 490 | EXPECT_EQ(1, d.response_started_count()); |
| 491 | EXPECT_EQ(0, d.bytes_received()); |
| 492 | EXPECT_FALSE(d.received_data_before_response()); |
| 493 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 494 | } |
| 495 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 496 | TEST_F(URLRequestTestHTTP, PostTest) { |
| 497 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 498 | const int kMsgSize = 20000; // multiple of 10 |
| 499 | const int kIterations = 50; |
| 500 | char *uploadBytes = new char[kMsgSize+1]; |
| 501 | char *ptr = uploadBytes; |
| 502 | char marker = 'a'; |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 503 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 504 | memcpy(ptr, "----------", 10); |
| 505 | ptr += 10; |
| 506 | if (idx % 100 == 0) { |
| 507 | ptr--; |
| 508 | *ptr++ = marker; |
| 509 | if (++marker > 'z') |
| 510 | marker = 'a'; |
| 511 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 512 | } |
| 513 | uploadBytes[kMsgSize] = '\0'; |
| 514 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 515 | scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 516 | |
| 517 | for (int i = 0; i < kIterations; ++i) { |
| 518 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 519 | URLRequest r(server_->TestServerPage("echo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 520 | r.set_context(context); |
| 521 | r.set_method("POST"); |
| 522 | |
| 523 | r.AppendBytesToUpload(uploadBytes, kMsgSize); |
| 524 | |
| 525 | r.Start(); |
| 526 | EXPECT_TRUE(r.is_pending()); |
| 527 | |
| 528 | MessageLoop::current()->Run(); |
| 529 | |
| 530 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 531 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 532 | |
| 533 | EXPECT_FALSE(d.received_data_before_response()); |
| 534 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 535 | EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 536 | EXPECT_EQ(d.data_received().compare(uploadBytes), 0); |
| 537 | } |
| 538 | delete[] uploadBytes; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 539 | } |
| 540 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 541 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
| 542 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 543 | TestDelegate d; |
| 544 | { |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 545 | TestURLRequest r(server_->TestServerPage("echo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 546 | r.set_method("POST"); |
| 547 | |
| 548 | r.Start(); |
| 549 | EXPECT_TRUE(r.is_pending()); |
| 550 | |
| 551 | MessageLoop::current()->Run(); |
| 552 | |
| 553 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 554 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 555 | |
| 556 | EXPECT_FALSE(d.received_data_before_response()); |
| 557 | EXPECT_TRUE(d.data_received().empty()); |
| 558 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 559 | } |
| 560 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 561 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
| 562 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 563 | TestDelegate d; |
| 564 | { |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 565 | TestURLRequest r(server_->TestServerPage("echo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 566 | r.set_method("POST"); |
| 567 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 568 | FilePath dir; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 569 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 570 | file_util::SetCurrentDirectory(dir); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 571 | |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 572 | FilePath path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 573 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 574 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 575 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 576 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 577 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 578 | r.AppendFileToUpload(path); |
| 579 | |
| 580 | // This file should just be ignored in the upload stream. |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 581 | r.AppendFileToUpload(FilePath(FILE_PATH_LITERAL( |
| 582 | "c:\\path\\to\\non\\existant\\file.randomness.12345"))); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 583 | |
| 584 | r.Start(); |
| 585 | EXPECT_TRUE(r.is_pending()); |
| 586 | |
| 587 | MessageLoop::current()->Run(); |
| 588 | |
[email protected] | 10a1fe9 | 2008-11-04 21:47:02 | [diff] [blame] | 589 | int64 longsize; |
| 590 | ASSERT_EQ(true, file_util::GetFileSize(path, &longsize)); |
| 591 | int size = static_cast<int>(longsize); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 592 | scoped_array<char> buf(new char[size]); |
| 593 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 594 | int size_read = static_cast<int>(file_util::ReadFile(path, |
| 595 | buf.get(), size)); |
[email protected] | eac0709a | 2008-11-04 21:00:46 | [diff] [blame] | 596 | ASSERT_EQ(size, size_read); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 597 | |
| 598 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 599 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 600 | |
| 601 | EXPECT_FALSE(d.received_data_before_response()); |
| 602 | |
| 603 | ASSERT_EQ(size, d.bytes_received()); |
| 604 | EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size)); |
| 605 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 606 | } |
| 607 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 608 | TEST_F(URLRequestTest, AboutBlankTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 609 | TestDelegate d; |
| 610 | { |
| 611 | TestURLRequest r(GURL("about:blank"), &d); |
| 612 | |
| 613 | r.Start(); |
| 614 | EXPECT_TRUE(r.is_pending()); |
| 615 | |
| 616 | MessageLoop::current()->Run(); |
| 617 | |
| 618 | EXPECT_TRUE(!r.is_pending()); |
| 619 | EXPECT_FALSE(d.received_data_before_response()); |
| 620 | EXPECT_EQ(d.bytes_received(), 0); |
| 621 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 622 | } |
| 623 | |
[email protected] | fcb7fd9 | 2009-10-22 04:18:58 | [diff] [blame] | 624 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 625 | TestDelegate d; |
| 626 | { |
| 627 | // Use our nice little Chrome logo. |
| 628 | TestURLRequest r(GURL( |
| 629 | "data:image/png;base64," |
| 630 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3" |
| 631 | "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD" |
| 632 | "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t" |
| 633 | "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9" |
| 634 | "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1" |
| 635 | "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z" |
| 636 | "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW" |
| 637 | "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW" |
| 638 | "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb" |
| 639 | "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5" |
| 640 | "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV" |
| 641 | "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq" |
| 642 | "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F" |
| 643 | "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB" |
| 644 | "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM" |
| 645 | "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm" |
| 646 | "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En" |
| 647 | "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
| 648 | &d); |
| 649 | |
| 650 | r.Start(); |
| 651 | EXPECT_TRUE(r.is_pending()); |
| 652 | |
| 653 | MessageLoop::current()->Run(); |
| 654 | |
| 655 | EXPECT_TRUE(!r.is_pending()); |
| 656 | EXPECT_FALSE(d.received_data_before_response()); |
| 657 | EXPECT_EQ(d.bytes_received(), 911); |
| 658 | } |
| 659 | } |
| 660 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 661 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 662 | FilePath app_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 663 | PathService::Get(base::FILE_EXE, &app_path); |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 664 | GURL app_url = net::FilePathToFileURL(app_path); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 665 | |
| 666 | TestDelegate d; |
| 667 | { |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 668 | TestURLRequest r(app_url, &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 669 | |
| 670 | r.Start(); |
| 671 | EXPECT_TRUE(r.is_pending()); |
| 672 | |
| 673 | MessageLoop::current()->Run(); |
| 674 | |
[email protected] | b77280c | 2009-03-20 17:27:47 | [diff] [blame] | 675 | int64 file_size = -1; |
| 676 | EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 677 | |
| 678 | EXPECT_TRUE(!r.is_pending()); |
| 679 | EXPECT_EQ(1, d.response_started_count()); |
| 680 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 681 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 682 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 683 | } |
| 684 | |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 685 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 686 | const size_t buffer_size = 4000; |
| 687 | scoped_array<char> buffer(new char[buffer_size]); |
| 688 | FillBuffer(buffer.get(), buffer_size); |
| 689 | |
| 690 | FilePath temp_path; |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 691 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 692 | GURL temp_url = net::FilePathToFileURL(temp_path); |
[email protected] | 1e5ae86 | 2009-10-14 22:14:53 | [diff] [blame] | 693 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 694 | |
| 695 | int64 file_size; |
| 696 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 697 | |
| 698 | const size_t first_byte_position = 500; |
| 699 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 700 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 701 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 702 | buffer.get() + last_byte_position + 1); |
| 703 | |
| 704 | TestDelegate d; |
| 705 | { |
| 706 | TestURLRequest r(temp_url, &d); |
| 707 | |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 708 | r.SetExtraRequestHeaders( |
| 709 | StringPrintf("Range: bytes=%" PRIuS "-%" PRIuS "\n", |
| 710 | first_byte_position, last_byte_position)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 711 | r.Start(); |
| 712 | EXPECT_TRUE(r.is_pending()); |
| 713 | |
| 714 | MessageLoop::current()->Run(); |
| 715 | EXPECT_TRUE(!r.is_pending()); |
| 716 | EXPECT_EQ(1, d.response_started_count()); |
| 717 | EXPECT_FALSE(d.received_data_before_response()); |
| 718 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 719 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 720 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 721 | } |
| 722 | |
| 723 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 727 | const size_t buffer_size = 4000; |
| 728 | scoped_array<char> buffer(new char[buffer_size]); |
| 729 | FillBuffer(buffer.get(), buffer_size); |
| 730 | |
| 731 | FilePath temp_path; |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 732 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 733 | GURL temp_url = net::FilePathToFileURL(temp_path); |
[email protected] | 1e5ae86 | 2009-10-14 22:14:53 | [diff] [blame] | 734 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 735 | |
| 736 | int64 file_size; |
| 737 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 738 | |
| 739 | const size_t first_byte_position = 500; |
| 740 | const size_t last_byte_position = buffer_size - 1; |
| 741 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 742 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 743 | buffer.get() + last_byte_position + 1); |
| 744 | |
| 745 | TestDelegate d; |
| 746 | { |
| 747 | TestURLRequest r(temp_url, &d); |
| 748 | |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 749 | r.SetExtraRequestHeaders(StringPrintf("Range: bytes=%" PRIuS "-\n", |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 750 | first_byte_position)); |
| 751 | r.Start(); |
| 752 | EXPECT_TRUE(r.is_pending()); |
| 753 | |
| 754 | MessageLoop::current()->Run(); |
| 755 | EXPECT_TRUE(!r.is_pending()); |
| 756 | EXPECT_EQ(1, d.response_started_count()); |
| 757 | EXPECT_FALSE(d.received_data_before_response()); |
| 758 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 759 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 760 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 761 | } |
| 762 | |
| 763 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 767 | const size_t buffer_size = 400000; |
| 768 | scoped_array<char> buffer(new char[buffer_size]); |
| 769 | FillBuffer(buffer.get(), buffer_size); |
| 770 | |
| 771 | FilePath temp_path; |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 772 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 773 | GURL temp_url = net::FilePathToFileURL(temp_path); |
[email protected] | 1e5ae86 | 2009-10-14 22:14:53 | [diff] [blame] | 774 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 775 | |
| 776 | int64 file_size; |
| 777 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 778 | |
| 779 | TestDelegate d; |
| 780 | { |
| 781 | TestURLRequest r(temp_url, &d); |
| 782 | |
| 783 | r.SetExtraRequestHeaders(StringPrintf("Range: bytes=0-0,10-200,200-300\n")); |
| 784 | r.Start(); |
| 785 | EXPECT_TRUE(r.is_pending()); |
| 786 | |
| 787 | MessageLoop::current()->Run(); |
| 788 | EXPECT_TRUE(d.request_failed()); |
| 789 | } |
| 790 | |
| 791 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 792 | } |
| 793 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 794 | TEST_F(URLRequestTest, InvalidUrlTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 795 | TestDelegate d; |
| 796 | { |
| 797 | TestURLRequest r(GURL("invalid url"), &d); |
| 798 | |
| 799 | r.Start(); |
| 800 | EXPECT_TRUE(r.is_pending()); |
| 801 | |
| 802 | MessageLoop::current()->Run(); |
| 803 | EXPECT_TRUE(d.request_failed()); |
| 804 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 805 | } |
| 806 | |
[email protected] | 6412d32 | 2008-11-21 23:49:30 | [diff] [blame] | 807 | // This test is disabled because it fails on some computers due to proxies |
| 808 | // returning a page in response to this request rather than reporting failure. |
| 809 | TEST_F(URLRequestTest, DISABLED_DnsFailureTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 810 | TestDelegate d; |
| 811 | { |
| 812 | URLRequest r(GURL("http://thisisnotavalidurl0123456789foo.com/"), &d); |
| 813 | |
| 814 | r.Start(); |
| 815 | EXPECT_TRUE(r.is_pending()); |
| 816 | |
| 817 | MessageLoop::current()->Run(); |
| 818 | EXPECT_TRUE(d.request_failed()); |
| 819 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 820 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 821 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 822 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
| 823 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 824 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 825 | TestURLRequest req(server_->TestServerPage("files/with-headers.html"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 826 | req.Start(); |
| 827 | MessageLoop::current()->Run(); |
| 828 | |
| 829 | const net::HttpResponseHeaders* headers = req.response_headers(); |
[email protected] | 589deddb | 2009-10-05 23:41:40 | [diff] [blame] | 830 | |
| 831 | // Simple sanity check that response_info() accesses the same data. |
| 832 | EXPECT_EQ(headers, req.response_info().headers.get()); |
| 833 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 834 | std::string header; |
| 835 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 836 | EXPECT_EQ("private", header); |
| 837 | |
| 838 | header.clear(); |
| 839 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 840 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 841 | |
| 842 | // The response has two "X-Multiple-Entries" headers. |
| 843 | // This verfies our output has them concatenated together. |
| 844 | header.clear(); |
| 845 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 846 | EXPECT_EQ("a, b", header); |
| 847 | } |
| 848 | |
[email protected] | a86c97cc | 2009-06-24 21:26:27 | [diff] [blame] | 849 | // TODO(jar): 14801 Remove BZIP code completely. |
| 850 | TEST_F(URLRequestTest, DISABLED_BZip2ContentTest) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 851 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 852 | HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 853 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 854 | |
| 855 | // for localhost domain, we also should support bzip2 encoding |
| 856 | // first, get the original file |
| 857 | TestDelegate d1; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 858 | TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 859 | req1.Start(); |
| 860 | MessageLoop::current()->Run(); |
| 861 | |
| 862 | const std::string& got_content = d1.data_received(); |
| 863 | |
| 864 | // second, get bzip2 content |
| 865 | TestDelegate d2; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 866 | TestURLRequest req2(server->TestServerPage("realbz2files/google.txt"), &d2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 867 | req2.Start(); |
| 868 | MessageLoop::current()->Run(); |
| 869 | |
| 870 | const std::string& got_bz2_content = d2.data_received(); |
| 871 | |
| 872 | // compare those two results |
[email protected] | 24cfabd | 2008-08-15 00:05:39 | [diff] [blame] | 873 | EXPECT_EQ(got_content, got_bz2_content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 874 | } |
| 875 | |
[email protected] | a86c97cc | 2009-06-24 21:26:27 | [diff] [blame] | 876 | // TODO(jar): 14801 Remove BZIP code completely. |
| 877 | TEST_F(URLRequestTest, DISABLED_BZip2ContentTest_IncrementalHeader) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 878 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 879 | HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 880 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 881 | |
| 882 | // for localhost domain, we also should support bzip2 encoding |
| 883 | // first, get the original file |
| 884 | TestDelegate d1; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 885 | TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 886 | req1.Start(); |
| 887 | MessageLoop::current()->Run(); |
| 888 | |
| 889 | const std::string& got_content = d1.data_received(); |
| 890 | |
| 891 | // second, get bzip2 content. ask the testserver to send the BZ2 header in |
| 892 | // two chunks with a delay between them. this tests our fix for bug 867161. |
| 893 | TestDelegate d2; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 894 | TestURLRequest req2(server->TestServerPage( |
| 895 | "realbz2files/google.txt?incremental-header"), &d2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 896 | req2.Start(); |
| 897 | MessageLoop::current()->Run(); |
| 898 | |
| 899 | const std::string& got_bz2_content = d2.data_received(); |
| 900 | |
| 901 | // compare those two results |
[email protected] | 24cfabd | 2008-08-15 00:05:39 | [diff] [blame] | 902 | EXPECT_EQ(got_content, got_bz2_content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 903 | } |
| 904 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 905 | #if defined(OS_WIN) |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 906 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 907 | FilePath app_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 908 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 909 | app_path = app_path.AppendASCII("net"); |
| 910 | app_path = app_path.AppendASCII("data"); |
| 911 | app_path = app_path.AppendASCII("url_request_unittest"); |
| 912 | app_path = app_path.AppendASCII("with-headers.html"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 913 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 914 | std::wstring lnk_path = app_path.value() + L".lnk"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 915 | |
| 916 | HRESULT result; |
| 917 | IShellLink *shell = NULL; |
| 918 | IPersistFile *persist = NULL; |
| 919 | |
| 920 | CoInitialize(NULL); |
| 921 | // Temporarily create a shortcut for test |
| 922 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 923 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 924 | reinterpret_cast<LPVOID*>(&shell)); |
[email protected] | d149ce8 | 2009-07-01 23:57:02 | [diff] [blame] | 925 | ASSERT_TRUE(SUCCEEDED(result)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 926 | result = shell->QueryInterface(IID_IPersistFile, |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 927 | reinterpret_cast<LPVOID*>(&persist)); |
[email protected] | d149ce8 | 2009-07-01 23:57:02 | [diff] [blame] | 928 | ASSERT_TRUE(SUCCEEDED(result)); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 929 | result = shell->SetPath(app_path.value().c_str()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 930 | EXPECT_TRUE(SUCCEEDED(result)); |
| 931 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 932 | EXPECT_TRUE(SUCCEEDED(result)); |
| 933 | result = persist->Save(lnk_path.c_str(), TRUE); |
| 934 | EXPECT_TRUE(SUCCEEDED(result)); |
| 935 | if (persist) |
| 936 | persist->Release(); |
| 937 | if (shell) |
| 938 | shell->Release(); |
| 939 | |
| 940 | TestDelegate d; |
| 941 | { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 942 | TestURLRequest r(net::FilePathToFileURL(FilePath(lnk_path)), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 943 | |
| 944 | r.Start(); |
| 945 | EXPECT_TRUE(r.is_pending()); |
| 946 | |
| 947 | MessageLoop::current()->Run(); |
| 948 | |
| 949 | WIN32_FILE_ATTRIBUTE_DATA data; |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 950 | GetFileAttributesEx(app_path.value().c_str(), |
| 951 | GetFileExInfoStandard, &data); |
| 952 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 953 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 954 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 955 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 956 | scoped_array<char> buffer(new char[data.nFileSizeLow]); |
| 957 | DWORD read_size; |
| 958 | BOOL result; |
| 959 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 960 | &read_size, NULL); |
| 961 | std::string content(buffer.get(), read_size); |
| 962 | CloseHandle(file); |
| 963 | |
| 964 | EXPECT_TRUE(!r.is_pending()); |
| 965 | EXPECT_EQ(1, d.received_redirect_count()); |
| 966 | EXPECT_EQ(content, d.data_received()); |
| 967 | } |
| 968 | |
| 969 | // Clean the shortcut |
| 970 | DeleteFile(lnk_path.c_str()); |
| 971 | CoUninitialize(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 972 | } |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 973 | #endif // defined(OS_WIN) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 974 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 975 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
| 976 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 977 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 978 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 979 | TestURLRequest req(server_->TestServerPage( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 980 | "files/content-type-normalization.html"), &d); |
| 981 | req.Start(); |
| 982 | MessageLoop::current()->Run(); |
| 983 | |
| 984 | std::string mime_type; |
| 985 | req.GetMimeType(&mime_type); |
| 986 | EXPECT_EQ("text/html", mime_type); |
| 987 | |
| 988 | std::string charset; |
| 989 | req.GetCharset(&charset); |
| 990 | EXPECT_EQ("utf-8", charset); |
| 991 | req.Cancel(); |
| 992 | } |
| 993 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 994 | TEST_F(URLRequestTest, FileDirCancelTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 995 | // Put in mock resource provider. |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 996 | net::NetModule::SetResourceProvider(TestNetResourceProvider); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 997 | |
| 998 | TestDelegate d; |
| 999 | { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 1000 | FilePath file_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1001 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 1002 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1003 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1004 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 1005 | TestURLRequest req(net::FilePathToFileURL(file_path), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1006 | req.Start(); |
| 1007 | EXPECT_TRUE(req.is_pending()); |
| 1008 | |
| 1009 | d.set_cancel_in_received_data_pending(true); |
| 1010 | |
| 1011 | MessageLoop::current()->Run(); |
| 1012 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1013 | |
| 1014 | // Take out mock resource provider. |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 1015 | net::NetModule::SetResourceProvider(NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1016 | } |
| 1017 | |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 1018 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1019 | // There is an implicit redirect when loading a file path that matches a |
| 1020 | // directory and does not end with a slash. Ensure that following such |
| 1021 | // redirects does not crash. See http://crbug.com/18686. |
| 1022 | |
| 1023 | FilePath path; |
| 1024 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 1025 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 1026 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 1027 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 1028 | |
| 1029 | TestDelegate d; |
| 1030 | d.set_quit_on_redirect(true); |
| 1031 | TestURLRequest req(net::FilePathToFileURL(path), &d); |
| 1032 | req.Start(); |
| 1033 | MessageLoop::current()->Run(); |
| 1034 | |
| 1035 | // Let the directory lister have time to finish its work, which will |
| 1036 | // cause the URLRequestFileDirJob's ref count to drop to 1. |
| 1037 | URLRequestFileDirJob* job = static_cast<URLRequestFileDirJob*>(req.job()); |
| 1038 | while (!job->list_complete()) { |
| 1039 | PlatformThread::Sleep(10); |
| 1040 | MessageLoop::current()->RunAllPending(); |
| 1041 | } |
| 1042 | |
| 1043 | // Should not crash during this call! |
| 1044 | req.FollowDeferredRedirect(); |
| 1045 | |
| 1046 | // Flush event queue. |
| 1047 | MessageLoop::current()->RunAllPending(); |
| 1048 | } |
| 1049 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1050 | TEST_F(URLRequestTestHTTP, RestrictRedirects) { |
| 1051 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1052 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1053 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1054 | TestURLRequest req(server_->TestServerPage( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1055 | "files/redirect-to-file.html"), &d); |
| 1056 | req.Start(); |
| 1057 | MessageLoop::current()->Run(); |
| 1058 | |
| 1059 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 1060 | EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error()); |
| 1061 | } |
| 1062 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1063 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
| 1064 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 4e66ed1 | 2009-07-21 23:38:42 | [diff] [blame] | 1065 | |
| 1066 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1067 | TestURLRequest req(server_->TestServerPage( |
[email protected] | 4e66ed1 | 2009-07-21 23:38:42 | [diff] [blame] | 1068 | "files/redirect-to-invalid-url.html"), &d); |
| 1069 | req.Start(); |
| 1070 | MessageLoop::current()->Run(); |
| 1071 | |
| 1072 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 1073 | EXPECT_EQ(net::ERR_INVALID_URL, req.status().os_error()); |
| 1074 | } |
| 1075 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1076 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
| 1077 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1078 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1079 | TestURLRequest req(server_->TestServerPage( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1080 | "echoheader?Referer"), &d); |
| 1081 | req.set_referrer("http://user:[email protected]/"); |
| 1082 | req.Start(); |
| 1083 | MessageLoop::current()->Run(); |
| 1084 | |
| 1085 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 1086 | } |
| 1087 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1088 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
| 1089 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1090 | TestDelegate d; |
| 1091 | { |
| 1092 | d.set_cancel_in_received_redirect(true); |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1093 | TestURLRequest req(server_->TestServerPage( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1094 | "files/redirect-test.html"), &d); |
| 1095 | req.Start(); |
| 1096 | MessageLoop::current()->Run(); |
| 1097 | |
| 1098 | EXPECT_EQ(1, d.response_started_count()); |
| 1099 | EXPECT_EQ(0, d.bytes_received()); |
| 1100 | EXPECT_FALSE(d.received_data_before_response()); |
| 1101 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1102 | } |
| 1103 | } |
| 1104 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1105 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
| 1106 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1107 | TestDelegate d; |
| 1108 | { |
| 1109 | d.set_quit_on_redirect(true); |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1110 | TestURLRequest req(server_->TestServerPage( |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1111 | "files/redirect-test.html"), &d); |
| 1112 | req.Start(); |
| 1113 | MessageLoop::current()->Run(); |
| 1114 | |
| 1115 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1116 | |
| 1117 | req.FollowDeferredRedirect(); |
| 1118 | MessageLoop::current()->Run(); |
| 1119 | |
| 1120 | EXPECT_EQ(1, d.response_started_count()); |
| 1121 | EXPECT_FALSE(d.received_data_before_response()); |
| 1122 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 1123 | |
| 1124 | FilePath path; |
| 1125 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 1126 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 1127 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 1128 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 1129 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 1130 | |
| 1131 | std::string contents; |
| 1132 | EXPECT_TRUE(file_util::ReadFileToString(path, &contents)); |
| 1133 | EXPECT_EQ(contents, d.data_received()); |
| 1134 | } |
| 1135 | } |
| 1136 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1137 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
| 1138 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1139 | TestDelegate d; |
| 1140 | { |
| 1141 | d.set_quit_on_redirect(true); |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1142 | TestURLRequest req(server_->TestServerPage( |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1143 | "files/redirect-test.html"), &d); |
| 1144 | req.Start(); |
| 1145 | MessageLoop::current()->Run(); |
| 1146 | |
| 1147 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1148 | |
| 1149 | req.Cancel(); |
| 1150 | MessageLoop::current()->Run(); |
| 1151 | |
| 1152 | EXPECT_EQ(1, d.response_started_count()); |
| 1153 | EXPECT_EQ(0, d.bytes_received()); |
| 1154 | EXPECT_FALSE(d.received_data_before_response()); |
| 1155 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1156 | } |
| 1157 | } |
| 1158 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1159 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
| 1160 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1161 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 1162 | scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1163 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1164 | // populate the cache |
| 1165 | { |
| 1166 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1167 | URLRequest req(server_->TestServerPage("echoheader?foo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1168 | req.set_context(context); |
| 1169 | req.SetExtraRequestHeaders("foo:1"); |
| 1170 | req.Start(); |
| 1171 | MessageLoop::current()->Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1172 | } |
| 1173 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1174 | // expect a cache hit |
| 1175 | { |
| 1176 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1177 | URLRequest req(server_->TestServerPage("echoheader?foo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1178 | req.set_context(context); |
| 1179 | req.SetExtraRequestHeaders("foo:1"); |
| 1180 | req.Start(); |
| 1181 | MessageLoop::current()->Run(); |
| 1182 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 1183 | EXPECT_TRUE(req.was_cached()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | // expect a cache miss |
| 1187 | { |
| 1188 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1189 | URLRequest req(server_->TestServerPage("echoheader?foo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1190 | req.set_context(context); |
| 1191 | req.SetExtraRequestHeaders("foo:2"); |
| 1192 | req.Start(); |
| 1193 | MessageLoop::current()->Run(); |
| 1194 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 1195 | EXPECT_FALSE(req.was_cached()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 1199 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 1200 | scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1201 | ASSERT_TRUE(NULL != server_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1202 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1203 | // populate the cache |
| 1204 | { |
| 1205 | TestDelegate d; |
| 1206 | d.set_username(L"user"); |
| 1207 | d.set_password(L"secret"); |
| 1208 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1209 | URLRequest r(server_->TestServerPage("auth-basic"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1210 | r.set_context(context); |
| 1211 | r.Start(); |
| 1212 | |
| 1213 | MessageLoop::current()->Run(); |
| 1214 | |
| 1215 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1216 | } |
| 1217 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1218 | // repeat request with end-to-end validation. since auth-basic results in a |
| 1219 | // cachable page, we expect this test to result in a 304. in which case, the |
| 1220 | // response should be fetched from the cache. |
| 1221 | { |
| 1222 | TestDelegate d; |
| 1223 | d.set_username(L"user"); |
| 1224 | d.set_password(L"secret"); |
| 1225 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1226 | URLRequest r(server_->TestServerPage("auth-basic"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1227 | r.set_context(context); |
| 1228 | r.set_load_flags(net::LOAD_VALIDATE_CACHE); |
| 1229 | r.Start(); |
| 1230 | |
| 1231 | MessageLoop::current()->Run(); |
| 1232 | |
| 1233 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 1234 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 1235 | // Should be the same cached document. |
| 1236 | EXPECT_TRUE(r.was_cached()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1237 | } |
| 1238 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1239 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1240 | // Check that Set-Cookie headers in 401 responses are respected. |
| 1241 | // http://crbug.com/6450 |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1242 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
| 1243 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1244 | |
| 1245 | GURL url_requiring_auth = |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1246 | server_->TestServerPage("auth-basic?set-cookie-if-challenged"); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1247 | |
| 1248 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 1249 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 1250 | { |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 1251 | scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1252 | TestDelegate d; |
| 1253 | d.set_username(L"user"); |
| 1254 | d.set_password(L"secret"); |
| 1255 | |
| 1256 | URLRequest r(url_requiring_auth, &d); |
| 1257 | r.set_context(context); |
| 1258 | r.Start(); |
| 1259 | |
| 1260 | MessageLoop::current()->Run(); |
| 1261 | |
| 1262 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 1263 | |
| 1264 | // Make sure we sent the cookie in the restarted transaction. |
| 1265 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 1266 | != std::string::npos); |
| 1267 | } |
| 1268 | |
| 1269 | // Same test as above, except this time the restart is initiated earlier |
| 1270 | // (without user intervention since identity is embedded in the URL). |
| 1271 | { |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 1272 | scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1273 | TestDelegate d; |
| 1274 | |
| 1275 | GURL::Replacements replacements; |
| 1276 | std::string username("user2"); |
| 1277 | std::string password("secret"); |
| 1278 | replacements.SetUsernameStr(username); |
| 1279 | replacements.SetPasswordStr(password); |
| 1280 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 1281 | |
| 1282 | URLRequest r(url_with_identity, &d); |
| 1283 | r.set_context(context); |
| 1284 | r.Start(); |
| 1285 | |
| 1286 | MessageLoop::current()->Run(); |
| 1287 | |
| 1288 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 1289 | |
| 1290 | // Make sure we sent the cookie in the restarted transaction. |
| 1291 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 1292 | != std::string::npos); |
| 1293 | } |
| 1294 | } |
| 1295 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1296 | TEST_F(URLRequestTest, DoNotSendCookies) { |
| 1297 | scoped_refptr<HTTPTestServer> server = |
| 1298 | HTTPTestServer::CreateServer(L"", NULL); |
| 1299 | ASSERT_TRUE(NULL != server.get()); |
| 1300 | scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
| 1301 | |
| 1302 | // Set up a cookie. |
| 1303 | { |
| 1304 | TestDelegate d; |
| 1305 | URLRequest req(server->TestServerPage("set-cookie?CookieToNotSend=1"), &d); |
| 1306 | req.set_context(context); |
| 1307 | req.Start(); |
| 1308 | MessageLoop::current()->Run(); |
| 1309 | } |
| 1310 | |
| 1311 | // Verify that the cookie is set. |
| 1312 | { |
| 1313 | TestDelegate d; |
| 1314 | TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d); |
| 1315 | req.set_context(context); |
| 1316 | req.Start(); |
| 1317 | MessageLoop::current()->Run(); |
| 1318 | |
| 1319 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 1320 | != std::string::npos); |
| 1321 | } |
| 1322 | |
| 1323 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 1324 | { |
| 1325 | TestDelegate d; |
| 1326 | TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d); |
| 1327 | req.set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES); |
| 1328 | req.set_context(context); |
| 1329 | req.Start(); |
| 1330 | MessageLoop::current()->Run(); |
| 1331 | |
| 1332 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 1333 | == std::string::npos); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 1338 | scoped_refptr<HTTPTestServer> server = |
| 1339 | HTTPTestServer::CreateServer(L"", NULL); |
| 1340 | ASSERT_TRUE(NULL != server.get()); |
| 1341 | scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
| 1342 | |
| 1343 | // Set up a cookie. |
| 1344 | { |
| 1345 | TestDelegate d; |
| 1346 | URLRequest req(server->TestServerPage("set-cookie?CookieToNotUpdate=2"), |
| 1347 | &d); |
| 1348 | req.set_context(context); |
| 1349 | req.Start(); |
| 1350 | MessageLoop::current()->Run(); |
| 1351 | } |
| 1352 | |
| 1353 | // Try to set-up another cookie and update the previous cookie. |
| 1354 | { |
| 1355 | scoped_refptr<URLRequestContext> context = new URLRequestTestContext(); |
| 1356 | TestDelegate d; |
| 1357 | URLRequest req(server->TestServerPage( |
| 1358 | "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d); |
| 1359 | req.set_load_flags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| 1360 | req.set_context(context); |
| 1361 | req.Start(); |
| 1362 | |
| 1363 | MessageLoop::current()->Run(); |
| 1364 | } |
| 1365 | |
| 1366 | // Verify the cookies weren't saved or updated. |
| 1367 | { |
| 1368 | TestDelegate d; |
| 1369 | TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d); |
| 1370 | req.set_context(context); |
| 1371 | req.Start(); |
| 1372 | MessageLoop::current()->Run(); |
| 1373 | |
| 1374 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 1375 | == std::string::npos); |
| 1376 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 1377 | != std::string::npos); |
| 1378 | } |
| 1379 | } |
| 1380 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1381 | // In this test, we do a POST which the server will 302 redirect. |
| 1382 | // The subsequent transaction should use GET, and should not send the |
| 1383 | // Content-Type header. |
| 1384 | // http://code.google.com/p/chromium/issues/detail?id=843 |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1385 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1386 | const char kData[] = "hello world"; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1387 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1388 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1389 | TestURLRequest req(server_->TestServerPage("files/redirect-to-echoall"), &d); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1390 | req.set_method("POST"); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1391 | req.set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1392 | |
| 1393 | // Set headers (some of which are specific to the POST). |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1394 | req.SetExtraRequestHeaders( |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1395 | "Content-Type: multipart/form-data; " |
| 1396 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 1397 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 1398 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1399 | "Accept-Language: en-US,en\r\n" |
| 1400 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1401 | "Content-Length: 11\r\n" |
[email protected] | a86c97cc | 2009-06-24 21:26:27 | [diff] [blame] | 1402 | "Origin: http://localhost:1337/"); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1403 | req.Start(); |
| 1404 | MessageLoop::current()->Run(); |
| 1405 | |
| 1406 | std::string mime_type; |
| 1407 | req.GetMimeType(&mime_type); |
| 1408 | EXPECT_EQ("text/html", mime_type); |
| 1409 | |
| 1410 | const std::string& data = d.data_received(); |
| 1411 | |
| 1412 | // Check that the post-specific headers were stripped: |
| 1413 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 1414 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 1415 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 1416 | |
| 1417 | // These extra request headers should not have been stripped. |
| 1418 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 1419 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 1420 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 1421 | } |
| 1422 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1423 | TEST_F(URLRequestTestHTTP, Post307RedirectPost) { |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1424 | const char kData[] = "hello world"; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1425 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1426 | TestDelegate d; |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1427 | TestURLRequest req(server_->TestServerPage("files/redirect307-to-echo"), |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1428 | &d); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1429 | req.set_method("POST"); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1430 | req.set_upload(CreateSimpleUploadData(kData).get()); |
| 1431 | req.SetExtraRequestHeaders( |
| 1432 | "Content-Length: " + UintToString(sizeof(kData) - 1)); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1433 | req.Start(); |
| 1434 | MessageLoop::current()->Run(); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1435 | EXPECT_EQ("POST", req.method()); |
| 1436 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1437 | } |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1438 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1439 | // Custom URLRequestJobs for use with interceptor tests |
| 1440 | class RestartTestJob : public URLRequestTestJob { |
| 1441 | public: |
[email protected] | a86c97cc | 2009-06-24 21:26:27 | [diff] [blame] | 1442 | explicit RestartTestJob(URLRequest* request) |
| 1443 | : URLRequestTestJob(request, true) {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1444 | protected: |
| 1445 | virtual void StartAsync() { |
| 1446 | this->NotifyRestartRequired(); |
| 1447 | } |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 1448 | private: |
| 1449 | ~RestartTestJob() {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1450 | }; |
| 1451 | |
| 1452 | class CancelTestJob : public URLRequestTestJob { |
| 1453 | public: |
[email protected] | a86c97cc | 2009-06-24 21:26:27 | [diff] [blame] | 1454 | explicit CancelTestJob(URLRequest* request) |
| 1455 | : URLRequestTestJob(request, true) {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1456 | protected: |
| 1457 | virtual void StartAsync() { |
| 1458 | request_->Cancel(); |
| 1459 | } |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 1460 | private: |
| 1461 | ~CancelTestJob() {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1462 | }; |
| 1463 | |
| 1464 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1465 | public: |
[email protected] | a86c97cc | 2009-06-24 21:26:27 | [diff] [blame] | 1466 | explicit CancelThenRestartTestJob(URLRequest* request) |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1467 | : URLRequestTestJob(request, true) { |
| 1468 | } |
| 1469 | protected: |
| 1470 | virtual void StartAsync() { |
| 1471 | request_->Cancel(); |
| 1472 | this->NotifyRestartRequired(); |
| 1473 | } |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 1474 | private: |
| 1475 | ~CancelThenRestartTestJob() {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1476 | }; |
| 1477 | |
| 1478 | // An Interceptor for use with interceptor tests |
| 1479 | class TestInterceptor : URLRequest::Interceptor { |
| 1480 | public: |
| 1481 | TestInterceptor() |
| 1482 | : intercept_main_request_(false), restart_main_request_(false), |
| 1483 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1484 | simulate_main_network_error_(false), |
| 1485 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1486 | intercept_final_response_(false), cancel_final_request_(false), |
| 1487 | did_intercept_main_(false), did_restart_main_(false), |
| 1488 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1489 | did_simulate_error_main_(false), |
| 1490 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1491 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1492 | URLRequest::RegisterRequestInterceptor(this); |
| 1493 | } |
| 1494 | |
| 1495 | ~TestInterceptor() { |
| 1496 | URLRequest::UnregisterRequestInterceptor(this); |
| 1497 | } |
| 1498 | |
| 1499 | virtual URLRequestJob* MaybeIntercept(URLRequest* request) { |
| 1500 | if (restart_main_request_) { |
| 1501 | restart_main_request_ = false; |
| 1502 | did_restart_main_ = true; |
| 1503 | return new RestartTestJob(request); |
| 1504 | } |
| 1505 | if (cancel_main_request_) { |
| 1506 | cancel_main_request_ = false; |
| 1507 | did_cancel_main_ = true; |
| 1508 | return new CancelTestJob(request); |
| 1509 | } |
| 1510 | if (cancel_then_restart_main_request_) { |
| 1511 | cancel_then_restart_main_request_ = false; |
| 1512 | did_cancel_then_restart_main_ = true; |
| 1513 | return new CancelThenRestartTestJob(request); |
| 1514 | } |
| 1515 | if (simulate_main_network_error_) { |
| 1516 | simulate_main_network_error_ = false; |
| 1517 | did_simulate_error_main_ = true; |
| 1518 | // will error since the requeted url is not one of its canned urls |
| 1519 | return new URLRequestTestJob(request, true); |
| 1520 | } |
| 1521 | if (!intercept_main_request_) |
| 1522 | return NULL; |
| 1523 | intercept_main_request_ = false; |
| 1524 | did_intercept_main_ = true; |
| 1525 | return new URLRequestTestJob(request, |
| 1526 | main_headers_, |
| 1527 | main_data_, |
| 1528 | true); |
| 1529 | } |
| 1530 | |
| 1531 | virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1532 | const GURL& location) { |
| 1533 | if (cancel_redirect_request_) { |
| 1534 | cancel_redirect_request_ = false; |
| 1535 | did_cancel_redirect_ = true; |
| 1536 | return new CancelTestJob(request); |
| 1537 | } |
| 1538 | if (!intercept_redirect_) |
| 1539 | return NULL; |
| 1540 | intercept_redirect_ = false; |
| 1541 | did_intercept_redirect_ = true; |
| 1542 | return new URLRequestTestJob(request, |
| 1543 | redirect_headers_, |
| 1544 | redirect_data_, |
| 1545 | true); |
| 1546 | } |
| 1547 | |
| 1548 | virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) { |
| 1549 | if (cancel_final_request_) { |
| 1550 | cancel_final_request_ = false; |
| 1551 | did_cancel_final_ = true; |
| 1552 | return new CancelTestJob(request); |
| 1553 | } |
| 1554 | if (!intercept_final_response_) |
| 1555 | return NULL; |
| 1556 | intercept_final_response_ = false; |
| 1557 | did_intercept_final_ = true; |
| 1558 | return new URLRequestTestJob(request, |
| 1559 | final_headers_, |
| 1560 | final_data_, |
| 1561 | true); |
| 1562 | } |
| 1563 | |
| 1564 | // Whether to intercept the main request, and if so the response to return. |
| 1565 | bool intercept_main_request_; |
| 1566 | std::string main_headers_; |
| 1567 | std::string main_data_; |
| 1568 | |
| 1569 | // Other actions we take at MaybeIntercept time |
| 1570 | bool restart_main_request_; |
| 1571 | bool cancel_main_request_; |
| 1572 | bool cancel_then_restart_main_request_; |
| 1573 | bool simulate_main_network_error_; |
| 1574 | |
| 1575 | // Whether to intercept redirects, and if so the response to return. |
| 1576 | bool intercept_redirect_; |
| 1577 | std::string redirect_headers_; |
| 1578 | std::string redirect_data_; |
| 1579 | |
| 1580 | // Other actions we can take at MaybeInterceptRedirect time |
| 1581 | bool cancel_redirect_request_; |
| 1582 | |
| 1583 | // Whether to intercept final response, and if so the response to return. |
| 1584 | bool intercept_final_response_; |
| 1585 | std::string final_headers_; |
| 1586 | std::string final_data_; |
| 1587 | |
| 1588 | // Other actions we can take at MaybeInterceptResponse time |
| 1589 | bool cancel_final_request_; |
| 1590 | |
| 1591 | // If we did something or not |
| 1592 | bool did_intercept_main_; |
| 1593 | bool did_restart_main_; |
| 1594 | bool did_cancel_main_; |
| 1595 | bool did_cancel_then_restart_main_; |
| 1596 | bool did_simulate_error_main_; |
| 1597 | bool did_intercept_redirect_; |
| 1598 | bool did_cancel_redirect_; |
| 1599 | bool did_intercept_final_; |
| 1600 | bool did_cancel_final_; |
| 1601 | |
| 1602 | // Static getters for canned response header and data strings |
| 1603 | |
| 1604 | static std::string ok_data() { |
| 1605 | return URLRequestTestJob::test_data_1(); |
| 1606 | } |
| 1607 | |
| 1608 | static std::string ok_headers() { |
| 1609 | return URLRequestTestJob::test_headers(); |
| 1610 | } |
| 1611 | |
| 1612 | static std::string redirect_data() { |
| 1613 | return std::string(); |
| 1614 | } |
| 1615 | |
| 1616 | static std::string redirect_headers() { |
| 1617 | return URLRequestTestJob::test_redirect_headers(); |
| 1618 | } |
| 1619 | |
| 1620 | static std::string error_data() { |
| 1621 | return std::string("ohhh nooooo mr. bill!"); |
| 1622 | } |
| 1623 | |
| 1624 | static std::string error_headers() { |
| 1625 | return URLRequestTestJob::test_error_headers(); |
| 1626 | } |
| 1627 | }; |
| 1628 | |
| 1629 | TEST_F(URLRequestTest, Intercept) { |
| 1630 | TestInterceptor interceptor; |
| 1631 | |
| 1632 | // intercept the main request and respond with a simple response |
| 1633 | interceptor.intercept_main_request_ = true; |
| 1634 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1635 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1636 | |
| 1637 | TestDelegate d; |
| 1638 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 1639 | URLRequest::UserData* user_data0 = new URLRequest::UserData(); |
| 1640 | URLRequest::UserData* user_data1 = new URLRequest::UserData(); |
| 1641 | URLRequest::UserData* user_data2 = new URLRequest::UserData(); |
| 1642 | req.SetUserData(NULL, user_data0); |
| 1643 | req.SetUserData(&user_data1, user_data1); |
| 1644 | req.SetUserData(&user_data2, user_data2); |
| 1645 | req.set_method("GET"); |
| 1646 | req.Start(); |
| 1647 | MessageLoop::current()->Run(); |
| 1648 | |
| 1649 | // Make sure we can retrieve our specific user data |
| 1650 | EXPECT_EQ(user_data0, req.GetUserData(NULL)); |
| 1651 | EXPECT_EQ(user_data1, req.GetUserData(&user_data1)); |
| 1652 | EXPECT_EQ(user_data2, req.GetUserData(&user_data2)); |
| 1653 | |
| 1654 | // Check the interceptor got called as expected |
| 1655 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1656 | |
| 1657 | // Check we got one good response |
| 1658 | EXPECT_TRUE(req.status().is_success()); |
| 1659 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1660 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1661 | EXPECT_EQ(1, d.response_started_count()); |
| 1662 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1663 | } |
| 1664 | |
| 1665 | TEST_F(URLRequestTest, InterceptRedirect) { |
| 1666 | TestInterceptor interceptor; |
| 1667 | |
| 1668 | // intercept the main request and respond with a redirect |
| 1669 | interceptor.intercept_main_request_ = true; |
| 1670 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1671 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1672 | |
| 1673 | // intercept that redirect and respond a final OK response |
| 1674 | interceptor.intercept_redirect_ = true; |
| 1675 | interceptor.redirect_headers_ = TestInterceptor::ok_headers(); |
| 1676 | interceptor.redirect_data_ = TestInterceptor::ok_data(); |
| 1677 | |
| 1678 | TestDelegate d; |
| 1679 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 1680 | req.set_method("GET"); |
| 1681 | req.Start(); |
| 1682 | MessageLoop::current()->Run(); |
| 1683 | |
| 1684 | // Check the interceptor got called as expected |
| 1685 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1686 | EXPECT_TRUE(interceptor.did_intercept_redirect_); |
| 1687 | |
| 1688 | // Check we got one good response |
| 1689 | EXPECT_TRUE(req.status().is_success()); |
[email protected] | 4463782 | 2009-08-27 17:01:11 | [diff] [blame] | 1690 | if (req.status().is_success()) { |
| 1691 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1692 | } |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1693 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1694 | EXPECT_EQ(1, d.response_started_count()); |
| 1695 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1696 | } |
| 1697 | |
| 1698 | TEST_F(URLRequestTest, InterceptServerError) { |
| 1699 | TestInterceptor interceptor; |
| 1700 | |
| 1701 | // intercept the main request to generate a server error response |
| 1702 | interceptor.intercept_main_request_ = true; |
| 1703 | interceptor.main_headers_ = TestInterceptor::error_headers(); |
| 1704 | interceptor.main_data_ = TestInterceptor::error_data(); |
| 1705 | |
| 1706 | // intercept that error and respond with an OK response |
| 1707 | interceptor.intercept_final_response_ = true; |
| 1708 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1709 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1710 | |
| 1711 | TestDelegate d; |
| 1712 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 1713 | req.set_method("GET"); |
| 1714 | req.Start(); |
| 1715 | MessageLoop::current()->Run(); |
| 1716 | |
| 1717 | // Check the interceptor got called as expected |
| 1718 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1719 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1720 | |
| 1721 | // Check we got one good response |
| 1722 | EXPECT_TRUE(req.status().is_success()); |
| 1723 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1724 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1725 | EXPECT_EQ(1, d.response_started_count()); |
| 1726 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1727 | } |
| 1728 | |
| 1729 | TEST_F(URLRequestTest, InterceptNetworkError) { |
| 1730 | TestInterceptor interceptor; |
| 1731 | |
| 1732 | // intercept the main request to simulate a network error |
| 1733 | interceptor.simulate_main_network_error_ = true; |
| 1734 | |
| 1735 | // intercept that error and respond with an OK response |
| 1736 | interceptor.intercept_final_response_ = true; |
| 1737 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1738 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1739 | |
| 1740 | TestDelegate d; |
| 1741 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 1742 | req.set_method("GET"); |
| 1743 | req.Start(); |
| 1744 | MessageLoop::current()->Run(); |
| 1745 | |
| 1746 | // Check the interceptor got called as expected |
| 1747 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1748 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1749 | |
| 1750 | // Check we received one good response |
| 1751 | EXPECT_TRUE(req.status().is_success()); |
| 1752 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1753 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1754 | EXPECT_EQ(1, d.response_started_count()); |
| 1755 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1756 | } |
| 1757 | |
| 1758 | TEST_F(URLRequestTest, InterceptRestartRequired) { |
| 1759 | TestInterceptor interceptor; |
| 1760 | |
| 1761 | // restart the main request |
| 1762 | interceptor.restart_main_request_ = true; |
| 1763 | |
| 1764 | // then intercept the new main request and respond with an OK response |
| 1765 | interceptor.intercept_main_request_ = true; |
| 1766 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1767 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1768 | |
| 1769 | TestDelegate d; |
| 1770 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 1771 | req.set_method("GET"); |
| 1772 | req.Start(); |
| 1773 | MessageLoop::current()->Run(); |
| 1774 | |
| 1775 | // Check the interceptor got called as expected |
| 1776 | EXPECT_TRUE(interceptor.did_restart_main_); |
| 1777 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1778 | |
| 1779 | // Check we received one good response |
| 1780 | EXPECT_TRUE(req.status().is_success()); |
[email protected] | 4463782 | 2009-08-27 17:01:11 | [diff] [blame] | 1781 | if (req.status().is_success()) { |
| 1782 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1783 | } |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1784 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1785 | EXPECT_EQ(1, d.response_started_count()); |
| 1786 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1787 | } |
| 1788 | |
| 1789 | TEST_F(URLRequestTest, InterceptRespectsCancelMain) { |
| 1790 | TestInterceptor interceptor; |
| 1791 | |
| 1792 | // intercept the main request and cancel from within the restarted job |
| 1793 | interceptor.cancel_main_request_ = true; |
| 1794 | |
| 1795 | // setup to intercept final response and override it with an OK response |
| 1796 | interceptor.intercept_final_response_ = true; |
| 1797 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1798 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1799 | |
| 1800 | TestDelegate d; |
| 1801 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 1802 | req.set_method("GET"); |
| 1803 | req.Start(); |
| 1804 | MessageLoop::current()->Run(); |
| 1805 | |
| 1806 | // Check the interceptor got called as expected |
| 1807 | EXPECT_TRUE(interceptor.did_cancel_main_); |
| 1808 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1809 | |
| 1810 | // Check we see a canceled request |
| 1811 | EXPECT_FALSE(req.status().is_success()); |
| 1812 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1813 | } |
| 1814 | |
| 1815 | TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) { |
| 1816 | TestInterceptor interceptor; |
| 1817 | |
| 1818 | // intercept the main request and respond with a redirect |
| 1819 | interceptor.intercept_main_request_ = true; |
| 1820 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1821 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1822 | |
| 1823 | // intercept the redirect and cancel from within that job |
| 1824 | interceptor.cancel_redirect_request_ = true; |
| 1825 | |
| 1826 | // setup to intercept final response and override it with an OK response |
| 1827 | interceptor.intercept_final_response_ = true; |
| 1828 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1829 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1830 | |
| 1831 | TestDelegate d; |
| 1832 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 1833 | req.set_method("GET"); |
| 1834 | req.Start(); |
| 1835 | MessageLoop::current()->Run(); |
| 1836 | |
| 1837 | // Check the interceptor got called as expected |
| 1838 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1839 | EXPECT_TRUE(interceptor.did_cancel_redirect_); |
| 1840 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1841 | |
| 1842 | // Check we see a canceled request |
| 1843 | EXPECT_FALSE(req.status().is_success()); |
| 1844 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1845 | } |
| 1846 | |
| 1847 | TEST_F(URLRequestTest, InterceptRespectsCancelFinal) { |
| 1848 | TestInterceptor interceptor; |
| 1849 | |
| 1850 | // intercept the main request to simulate a network error |
| 1851 | interceptor.simulate_main_network_error_ = true; |
| 1852 | |
| 1853 | // setup to intercept final response and cancel from within that job |
| 1854 | interceptor.cancel_final_request_ = true; |
| 1855 | |
| 1856 | TestDelegate d; |
| 1857 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 1858 | req.set_method("GET"); |
| 1859 | req.Start(); |
| 1860 | MessageLoop::current()->Run(); |
| 1861 | |
| 1862 | // Check the interceptor got called as expected |
| 1863 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1864 | EXPECT_TRUE(interceptor.did_cancel_final_); |
| 1865 | |
| 1866 | // Check we see a canceled request |
| 1867 | EXPECT_FALSE(req.status().is_success()); |
| 1868 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1869 | } |
| 1870 | |
| 1871 | TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) { |
| 1872 | TestInterceptor interceptor; |
| 1873 | |
| 1874 | // intercept the main request and cancel then restart from within that job |
| 1875 | interceptor.cancel_then_restart_main_request_ = true; |
| 1876 | |
| 1877 | // setup to intercept final response and override it with an OK response |
| 1878 | interceptor.intercept_final_response_ = true; |
| 1879 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1880 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1881 | |
| 1882 | TestDelegate d; |
| 1883 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 1884 | req.set_method("GET"); |
| 1885 | req.Start(); |
| 1886 | MessageLoop::current()->Run(); |
| 1887 | |
| 1888 | // Check the interceptor got called as expected |
| 1889 | EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); |
| 1890 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1891 | |
| 1892 | // Check we see a canceled request |
| 1893 | EXPECT_FALSE(req.status().is_success()); |
| 1894 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1895 | } |
| 1896 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 1897 | class URLRequestTestFTP : public URLRequestTest { |
| 1898 | protected: |
| 1899 | static void SetUpTestCase() { |
| 1900 | server_ = FTPTestServer::CreateServer(L""); |
| 1901 | } |
| 1902 | |
| 1903 | static void TearDownTestCase() { |
| 1904 | server_ = NULL; |
| 1905 | } |
| 1906 | |
| 1907 | static scoped_refptr<FTPTestServer> server_; |
| 1908 | }; |
| 1909 | |
| 1910 | // static |
| 1911 | scoped_refptr<FTPTestServer> URLRequestTestFTP::server_; |
| 1912 | |
[email protected] | 5accf733 | 2009-11-24 03:41:38 | [diff] [blame^] | 1913 | // Flaky, see http://crbug.com/25045. |
| 1914 | TEST_F(URLRequestTestFTP, FLAKY_FTPDirectoryListing) { |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 1915 | ASSERT_TRUE(NULL != server_.get()); |
| 1916 | TestDelegate d; |
| 1917 | { |
| 1918 | TestURLRequest r(server_->TestServerPage("/"), &d); |
| 1919 | r.Start(); |
| 1920 | EXPECT_TRUE(r.is_pending()); |
| 1921 | |
| 1922 | MessageLoop::current()->Run(); |
| 1923 | |
| 1924 | EXPECT_FALSE(r.is_pending()); |
| 1925 | EXPECT_EQ(1, d.response_started_count()); |
| 1926 | EXPECT_FALSE(d.received_data_before_response()); |
| 1927 | EXPECT_LT(0, d.bytes_received()); |
| 1928 | } |
| 1929 | } |
| 1930 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 1931 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
| 1932 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 1933 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1934 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 1935 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1936 | TestDelegate d; |
| 1937 | { |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 1938 | TestURLRequest r(server_->TestServerPage("/LICENSE"), &d); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1939 | r.Start(); |
| 1940 | EXPECT_TRUE(r.is_pending()); |
| 1941 | |
| 1942 | MessageLoop::current()->Run(); |
| 1943 | |
| 1944 | int64 file_size = 0; |
| 1945 | file_util::GetFileSize(app_path, &file_size); |
| 1946 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 1947 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1948 | EXPECT_EQ(1, d.response_started_count()); |
| 1949 | EXPECT_FALSE(d.received_data_before_response()); |
| 1950 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 1951 | } |
| 1952 | } |
| 1953 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 1954 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
| 1955 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 1956 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1957 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 1958 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1959 | TestDelegate d; |
| 1960 | { |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 1961 | TestURLRequest r(server_->TestServerPage("/LICENSE", "chrome", "chrome"), |
| 1962 | &d); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1963 | r.Start(); |
| 1964 | EXPECT_TRUE(r.is_pending()); |
| 1965 | |
| 1966 | MessageLoop::current()->Run(); |
| 1967 | |
| 1968 | int64 file_size = 0; |
| 1969 | file_util::GetFileSize(app_path, &file_size); |
| 1970 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 1971 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1972 | EXPECT_EQ(1, d.response_started_count()); |
| 1973 | EXPECT_FALSE(d.received_data_before_response()); |
| 1974 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 1975 | } |
| 1976 | } |
| 1977 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 1978 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
| 1979 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 1980 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1981 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 1982 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1983 | TestDelegate d; |
| 1984 | { |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 1985 | TestURLRequest r(server_->TestServerPage("/LICENSE", |
| 1986 | "chrome", "wrong_password"), &d); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1987 | r.Start(); |
| 1988 | EXPECT_TRUE(r.is_pending()); |
| 1989 | |
| 1990 | MessageLoop::current()->Run(); |
| 1991 | |
| 1992 | int64 file_size = 0; |
| 1993 | file_util::GetFileSize(app_path, &file_size); |
| 1994 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 1995 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1996 | EXPECT_EQ(1, d.response_started_count()); |
| 1997 | EXPECT_FALSE(d.received_data_before_response()); |
| 1998 | EXPECT_EQ(d.bytes_received(), 0); |
| 1999 | } |
| 2000 | } |
| 2001 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 2002 | // Flaky, see http://crbug.com/25045. |
| 2003 | TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPasswordRestart) { |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 2004 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2005 | FilePath app_path; |
| 2006 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2007 | app_path = app_path.AppendASCII("LICENSE"); |
| 2008 | TestDelegate d; |
| 2009 | // Set correct login credentials. The delegate will be asked for them when |
| 2010 | // the initial login with wrong credentials will fail. |
| 2011 | d.set_username(L"chrome"); |
| 2012 | d.set_password(L"chrome"); |
| 2013 | { |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 2014 | TestURLRequest r(server_->TestServerPage("/LICENSE", |
| 2015 | "chrome", "wrong_password"), &d); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2016 | r.Start(); |
| 2017 | EXPECT_TRUE(r.is_pending()); |
| 2018 | |
| 2019 | MessageLoop::current()->Run(); |
| 2020 | |
| 2021 | int64 file_size = 0; |
| 2022 | file_util::GetFileSize(app_path, &file_size); |
| 2023 | |
| 2024 | EXPECT_FALSE(r.is_pending()); |
| 2025 | EXPECT_EQ(1, d.response_started_count()); |
| 2026 | EXPECT_FALSE(d.received_data_before_response()); |
| 2027 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 2028 | } |
| 2029 | } |
| 2030 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 2031 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
| 2032 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2033 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2034 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2035 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2036 | TestDelegate d; |
| 2037 | { |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 2038 | TestURLRequest r(server_->TestServerPage("/LICENSE", |
| 2039 | "wrong_user", "chrome"), &d); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2040 | r.Start(); |
| 2041 | EXPECT_TRUE(r.is_pending()); |
| 2042 | |
| 2043 | MessageLoop::current()->Run(); |
| 2044 | |
| 2045 | int64 file_size = 0; |
| 2046 | file_util::GetFileSize(app_path, &file_size); |
| 2047 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 2048 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2049 | EXPECT_EQ(1, d.response_started_count()); |
| 2050 | EXPECT_FALSE(d.received_data_before_response()); |
| 2051 | EXPECT_EQ(d.bytes_received(), 0); |
| 2052 | } |
| 2053 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2054 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 2055 | // Flaky, see http://crbug.com/25045. |
| 2056 | TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUserRestart) { |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 2057 | ASSERT_TRUE(NULL != server_.get()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2058 | FilePath app_path; |
| 2059 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2060 | app_path = app_path.AppendASCII("LICENSE"); |
| 2061 | TestDelegate d; |
| 2062 | // Set correct login credentials. The delegate will be asked for them when |
| 2063 | // the initial login with wrong credentials will fail. |
| 2064 | d.set_username(L"chrome"); |
| 2065 | d.set_password(L"chrome"); |
| 2066 | { |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 2067 | TestURLRequest r(server_->TestServerPage("/LICENSE", |
| 2068 | "wrong_user", "chrome"), &d); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2069 | r.Start(); |
| 2070 | EXPECT_TRUE(r.is_pending()); |
| 2071 | |
| 2072 | MessageLoop::current()->Run(); |
| 2073 | |
| 2074 | int64 file_size = 0; |
| 2075 | file_util::GetFileSize(app_path, &file_size); |
| 2076 | |
| 2077 | EXPECT_FALSE(r.is_pending()); |
| 2078 | EXPECT_EQ(1, d.response_started_count()); |
| 2079 | EXPECT_FALSE(d.received_data_before_response()); |
| 2080 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 2081 | } |
| 2082 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2083 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 2084 | // Flaky, see http://crbug.com/25045. |
| 2085 | TEST_F(URLRequestTestFTP, FLAKY_FTPCacheURLCredentials) { |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2086 | ASSERT_TRUE(NULL != server_.get()); |
| 2087 | FilePath app_path; |
| 2088 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2089 | app_path = app_path.AppendASCII("LICENSE"); |
| 2090 | |
| 2091 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 2092 | { |
| 2093 | // Pass correct login identity in the URL. |
| 2094 | TestURLRequest r(server_->TestServerPage("/LICENSE", |
| 2095 | "chrome", "chrome"), |
| 2096 | d.get()); |
| 2097 | r.Start(); |
| 2098 | EXPECT_TRUE(r.is_pending()); |
| 2099 | |
| 2100 | MessageLoop::current()->Run(); |
| 2101 | |
| 2102 | int64 file_size = 0; |
| 2103 | file_util::GetFileSize(app_path, &file_size); |
| 2104 | |
| 2105 | EXPECT_FALSE(r.is_pending()); |
| 2106 | EXPECT_EQ(1, d->response_started_count()); |
| 2107 | EXPECT_FALSE(d->received_data_before_response()); |
| 2108 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 2109 | } |
| 2110 | |
| 2111 | d.reset(new TestDelegate); |
| 2112 | { |
| 2113 | // This request should use cached identity from previous request. |
| 2114 | TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get()); |
| 2115 | r.Start(); |
| 2116 | EXPECT_TRUE(r.is_pending()); |
| 2117 | |
| 2118 | MessageLoop::current()->Run(); |
| 2119 | |
| 2120 | int64 file_size = 0; |
| 2121 | file_util::GetFileSize(app_path, &file_size); |
| 2122 | |
| 2123 | EXPECT_FALSE(r.is_pending()); |
| 2124 | EXPECT_EQ(1, d->response_started_count()); |
| 2125 | EXPECT_FALSE(d->received_data_before_response()); |
| 2126 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 2127 | } |
| 2128 | } |
| 2129 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 2130 | // Flaky, see http://crbug.com/25045. |
| 2131 | TEST_F(URLRequestTestFTP, FLAKY_FTPCacheLoginBoxCredentials) { |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2132 | ASSERT_TRUE(NULL != server_.get()); |
| 2133 | FilePath app_path; |
| 2134 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2135 | app_path = app_path.AppendASCII("LICENSE"); |
| 2136 | |
| 2137 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 2138 | // Set correct login credentials. The delegate will be asked for them when |
| 2139 | // the initial login with wrong credentials will fail. |
| 2140 | d->set_username(L"chrome"); |
| 2141 | d->set_password(L"chrome"); |
| 2142 | { |
| 2143 | TestURLRequest r(server_->TestServerPage("/LICENSE", |
| 2144 | "chrome", "wrong_password"), |
| 2145 | d.get()); |
| 2146 | r.Start(); |
| 2147 | EXPECT_TRUE(r.is_pending()); |
| 2148 | |
| 2149 | MessageLoop::current()->Run(); |
| 2150 | |
| 2151 | int64 file_size = 0; |
| 2152 | file_util::GetFileSize(app_path, &file_size); |
| 2153 | |
| 2154 | EXPECT_FALSE(r.is_pending()); |
| 2155 | EXPECT_EQ(1, d->response_started_count()); |
| 2156 | EXPECT_FALSE(d->received_data_before_response()); |
| 2157 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 2158 | } |
| 2159 | |
| 2160 | // Use a new delegate without explicit credentials. The cached ones should be |
| 2161 | // used. |
| 2162 | d.reset(new TestDelegate); |
| 2163 | { |
| 2164 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 2165 | // ones. |
| 2166 | TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get()); |
| 2167 | r.Start(); |
| 2168 | EXPECT_TRUE(r.is_pending()); |
| 2169 | |
| 2170 | MessageLoop::current()->Run(); |
| 2171 | |
| 2172 | int64 file_size = 0; |
| 2173 | file_util::GetFileSize(app_path, &file_size); |
| 2174 | |
| 2175 | EXPECT_FALSE(r.is_pending()); |
| 2176 | EXPECT_EQ(1, d->response_started_count()); |
| 2177 | EXPECT_FALSE(d->received_data_before_response()); |
| 2178 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 2179 | } |
| 2180 | } |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2181 | |
| 2182 | // Check that default A-L header is sent. |
| 2183 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
| 2184 | ASSERT_TRUE(NULL != server_.get()); |
| 2185 | TestDelegate d; |
| 2186 | TestURLRequest req(server_->TestServerPage("echoheader?Accept-Language"), &d); |
| 2187 | req.set_context(new URLRequestTestContext()); |
| 2188 | req.Start(); |
| 2189 | MessageLoop::current()->Run(); |
| 2190 | EXPECT_EQ(req.context()->accept_language(), d.data_received()); |
| 2191 | } |
| 2192 | |
| 2193 | // Check that if request overrides the A-L header, the default is not appended. |
| 2194 | // See http://crbug.com/20894 |
| 2195 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
| 2196 | ASSERT_TRUE(NULL != server_.get()); |
| 2197 | TestDelegate d; |
[email protected] | eb834fb | 2009-10-23 16:09:41 | [diff] [blame] | 2198 | TestURLRequest |
| 2199 | req(server_->TestServerPage("echoheaderoverride?Accept-Language"), &d); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2200 | req.set_context(new URLRequestTestContext()); |
| 2201 | req.SetExtraRequestHeaders("Accept-Language: ru"); |
| 2202 | req.Start(); |
| 2203 | MessageLoop::current()->Run(); |
| 2204 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 2205 | } |
| 2206 | |
| 2207 | // Check that default A-C header is sent. |
| 2208 | TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) { |
| 2209 | ASSERT_TRUE(NULL != server_.get()); |
| 2210 | TestDelegate d; |
| 2211 | TestURLRequest req(server_->TestServerPage("echoheader?Accept-Charset"), &d); |
| 2212 | req.set_context(new URLRequestTestContext()); |
| 2213 | req.Start(); |
| 2214 | MessageLoop::current()->Run(); |
| 2215 | EXPECT_EQ(req.context()->accept_charset(), d.data_received()); |
| 2216 | } |
| 2217 | |
| 2218 | // Check that if request overrides the A-C header, the default is not appended. |
| 2219 | // See http://crbug.com/20894 |
| 2220 | TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) { |
| 2221 | ASSERT_TRUE(NULL != server_.get()); |
| 2222 | TestDelegate d; |
[email protected] | eb834fb | 2009-10-23 16:09:41 | [diff] [blame] | 2223 | TestURLRequest |
| 2224 | req(server_->TestServerPage("echoheaderoverride?Accept-Charset"), &d); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2225 | req.set_context(new URLRequestTestContext()); |
| 2226 | req.SetExtraRequestHeaders("Accept-Charset: koi-8r"); |
| 2227 | req.Start(); |
| 2228 | MessageLoop::current()->Run(); |
| 2229 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 2230 | } |