[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1 | // Copyright (c) 2011 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 <shlobj.h> |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 11 | #include <windows.h> |
[email protected] | 1a15730 | 2010-01-29 03:36:45 | [diff] [blame] | 12 | #elif defined(USE_NSS) |
[email protected] | 1b1a264a | 2010-01-14 22:36:35 | [diff] [blame] | 13 | #include "base/nss_util.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] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 24 | #include "base/string_number_conversions.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 25 | #include "base/string_piece.h" |
| 26 | #include "base/stringprintf.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 27 | #include "base/utf_string_conversions.h" |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 28 | #include "net/base/cookie_monster.h" |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 29 | #include "net/base/cookie_policy.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 30 | #include "net/base/load_flags.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 31 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 32 | #include "net/base/net_log.h" |
| 33 | #include "net/base/net_log_unittest.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | #include "net/base/net_module.h" |
| 35 | #include "net/base/net_util.h" |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 36 | #include "net/base/ssl_connection_status_flags.h" |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 37 | #include "net/base/upload_data.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | #include "net/disk_cache/disk_cache.h" |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 39 | #include "net/ftp/ftp_network_layer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 40 | #include "net/http/http_cache.h" |
| 41 | #include "net/http/http_network_layer.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 42 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 43 | #include "net/http/http_response_headers.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 44 | #include "net/proxy/proxy_service.h" |
[email protected] | 1b9565c | 2010-07-21 01:19:31 | [diff] [blame] | 45 | #include "net/test/test_server.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 46 | #include "net/url_request/url_request.h" |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 47 | #include "net/url_request/url_request_file_dir_job.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 48 | #include "net/url_request/url_request_http_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 49 | #include "net/url_request/url_request_test_job.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 51 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 52 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 53 | using base::Time; |
| 54 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | namespace { |
| 56 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 57 | const string16 kChrome(ASCIIToUTF16("chrome")); |
| 58 | const string16 kSecret(ASCIIToUTF16("secret")); |
| 59 | const string16 kUser(ASCIIToUTF16("user")); |
| 60 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 61 | base::StringPiece TestNetResourceProvider(int key) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | return "header"; |
| 63 | } |
| 64 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 65 | // Do a case-insensitive search through |haystack| for |needle|. |
| 66 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 67 | std::string::const_iterator it = |
| 68 | std::search(haystack.begin(), |
| 69 | haystack.end(), |
| 70 | needle, |
| 71 | needle + strlen(needle), |
[email protected] | 07f1cee | 2010-11-03 03:53:35 | [diff] [blame] | 72 | base::CaseInsensitiveCompare<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 73 | return it != haystack.end(); |
| 74 | } |
| 75 | |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 76 | void FillBuffer(char* buffer, size_t len) { |
| 77 | static bool called = false; |
| 78 | if (!called) { |
| 79 | called = true; |
| 80 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 81 | srand(seed); |
| 82 | } |
| 83 | |
| 84 | for (size_t i = 0; i < len; i++) { |
| 85 | buffer[i] = static_cast<char>(rand()); |
| 86 | if (!buffer[i]) |
| 87 | buffer[i] = 'g'; |
| 88 | } |
| 89 | } |
| 90 | |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 91 | scoped_refptr<net::UploadData> CreateSimpleUploadData(const char* data) { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 92 | scoped_refptr<net::UploadData> upload(new net::UploadData); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 93 | upload->AppendBytes(data, strlen(data)); |
| 94 | return upload; |
| 95 | } |
| 96 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 97 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
| 98 | void CheckSSLInfo(const net::SSLInfo& ssl_info) { |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 99 | // Allow ChromeFrame fake SSLInfo to get through. |
[email protected] | c679be2 | 2010-08-29 15:20:05 | [diff] [blame] | 100 | if (ssl_info.cert.get() && |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 101 | ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") { |
| 102 | // -1 means unknown. |
| 103 | EXPECT_EQ(ssl_info.security_bits, -1); |
[email protected] | c679be2 | 2010-08-29 15:20:05 | [diff] [blame] | 104 | return; |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 107 | // -1 means unknown. 0 means no encryption. |
| 108 | EXPECT_GT(ssl_info.security_bits, 0); |
| 109 | |
| 110 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
| 111 | int cipher_suite = net::SSLConnectionStatusToCipherSuite( |
| 112 | ssl_info.connection_status); |
| 113 | EXPECT_NE(0, cipher_suite); |
| 114 | } |
| 115 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 116 | } // namespace |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 117 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 118 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |
| 119 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 120 | public: |
| 121 | static void SetUpTestCase() { |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 122 | net::URLRequest::AllowFileAccess(); |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 123 | } |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 124 | }; |
| 125 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 126 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 127 | public: |
| 128 | URLRequestTestHTTP() |
| 129 | : test_server_(net::TestServer::TYPE_HTTP, |
| 130 | FilePath(FILE_PATH_LITERAL( |
| 131 | "net/data/url_request_unittest"))) { |
| 132 | } |
| 133 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 134 | protected: |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 135 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 136 | const int kMsgSize = 20000; // multiple of 10 |
| 137 | const int kIterations = 50; |
| 138 | char *uploadBytes = new char[kMsgSize+1]; |
| 139 | char *ptr = uploadBytes; |
| 140 | char marker = 'a'; |
| 141 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 142 | memcpy(ptr, "----------", 10); |
| 143 | ptr += 10; |
| 144 | if (idx % 100 == 0) { |
| 145 | ptr--; |
| 146 | *ptr++ = marker; |
| 147 | if (++marker > 'z') |
| 148 | marker = 'a'; |
| 149 | } |
| 150 | } |
| 151 | uploadBytes[kMsgSize] = '\0'; |
| 152 | |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 153 | scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 154 | |
| 155 | for (int i = 0; i < kIterations; ++i) { |
| 156 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 157 | net::URLRequest r(test_server_.GetURL("echo"), &d); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 158 | r.set_context(context); |
| 159 | r.set_method(method.c_str()); |
| 160 | |
| 161 | r.AppendBytesToUpload(uploadBytes, kMsgSize); |
| 162 | |
| 163 | r.Start(); |
| 164 | EXPECT_TRUE(r.is_pending()); |
| 165 | |
| 166 | MessageLoop::current()->Run(); |
| 167 | |
| 168 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 169 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 170 | |
| 171 | EXPECT_FALSE(d.received_data_before_response()); |
| 172 | EXPECT_EQ(uploadBytes, d.data_received()); |
| 173 | EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0); |
| 174 | EXPECT_EQ(d.data_received().compare(uploadBytes), 0); |
| 175 | } |
| 176 | delete[] uploadBytes; |
| 177 | } |
| 178 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 179 | net::TestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 180 | }; |
| 181 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 182 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 183 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 184 | // The HTTPTestServer will return a 302 response, which we should not |
| 185 | // follow. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 186 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 187 | ASSERT_TRUE(test_server_.Start()); |
| 188 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 189 | TestDelegate d; |
| 190 | { |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 191 | net::URLRequest r(GURL("https://www.redirect.com/"), &d); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 192 | r.set_context( |
| 193 | new TestURLRequestContext(test_server_.host_port_pair().ToString())); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 194 | |
| 195 | r.Start(); |
| 196 | EXPECT_TRUE(r.is_pending()); |
| 197 | |
| 198 | MessageLoop::current()->Run(); |
| 199 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 200 | EXPECT_EQ(net::URLRequestStatus::FAILED, r.status().status()); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 201 | EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 202 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 203 | // We should not have followed the redirect. |
| 204 | EXPECT_EQ(0, d.received_redirect_count()); |
| 205 | } |
| 206 | } |
| 207 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 208 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 209 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 210 | // The HTTPTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 211 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 212 | ASSERT_TRUE(test_server_.Start()); |
| 213 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 214 | TestDelegate d; |
| 215 | { |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 216 | net::URLRequest r(GURL("https://www.server-auth.com/"), &d); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 217 | r.set_context( |
| 218 | new TestURLRequestContext(test_server_.host_port_pair().ToString())); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 219 | |
| 220 | r.Start(); |
| 221 | EXPECT_TRUE(r.is_pending()); |
| 222 | |
| 223 | MessageLoop::current()->Run(); |
| 224 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 225 | EXPECT_EQ(net::URLRequestStatus::FAILED, r.status().status()); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 226 | EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 230 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 231 | ASSERT_TRUE(test_server_.Start()); |
| 232 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 233 | TestDelegate d; |
| 234 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 235 | TestURLRequest r(test_server_.GetURL(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 236 | |
| 237 | r.Start(); |
| 238 | EXPECT_TRUE(r.is_pending()); |
| 239 | |
| 240 | MessageLoop::current()->Run(); |
| 241 | |
| 242 | EXPECT_EQ(1, d.response_started_count()); |
| 243 | EXPECT_FALSE(d.received_data_before_response()); |
| 244 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 245 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 246 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 247 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 250 | TEST_F(URLRequestTestHTTP, GetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 251 | ASSERT_TRUE(test_server_.Start()); |
| 252 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 253 | TestDelegate d; |
| 254 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 255 | TestURLRequest r(test_server_.GetURL(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 256 | |
| 257 | r.Start(); |
| 258 | EXPECT_TRUE(r.is_pending()); |
| 259 | |
| 260 | MessageLoop::current()->Run(); |
| 261 | |
| 262 | EXPECT_EQ(1, d.response_started_count()); |
| 263 | EXPECT_FALSE(d.received_data_before_response()); |
| 264 | EXPECT_NE(0, d.bytes_received()); |
| 265 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 268 | TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 269 | ASSERT_TRUE(test_server_.Start()); |
| 270 | |
| 271 | net::TestServer https_test_server( |
| 272 | net::TestServer::TYPE_HTTPS, FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 273 | ASSERT_TRUE(https_test_server.Start()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 274 | |
| 275 | // An https server is sent a request with an https referer, |
| 276 | // and responds with a redirect to an http url. The http |
| 277 | // server should not be sent the referer. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 278 | GURL http_destination = test_server_.GetURL(""); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 279 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 280 | TestURLRequest req(https_test_server.GetURL( |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 281 | "server-redirect?" + http_destination.spec()), &d); |
| 282 | req.set_referrer("https://www.referrer.com/"); |
| 283 | req.Start(); |
| 284 | MessageLoop::current()->Run(); |
| 285 | |
| 286 | EXPECT_EQ(1, d.response_started_count()); |
| 287 | EXPECT_EQ(1, d.received_redirect_count()); |
| 288 | EXPECT_EQ(http_destination, req.url()); |
| 289 | EXPECT_EQ(std::string(), req.referrer()); |
| 290 | } |
| 291 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 292 | class HTTPSRequestTest : public testing::Test { |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 293 | }; |
| 294 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 295 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 296 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 297 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 298 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 299 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 300 | TestDelegate d; |
| 301 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 302 | TestURLRequest r(test_server.GetURL(""), &d); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 303 | |
| 304 | r.Start(); |
| 305 | EXPECT_TRUE(r.is_pending()); |
| 306 | |
| 307 | MessageLoop::current()->Run(); |
| 308 | |
| 309 | EXPECT_EQ(1, d.response_started_count()); |
| 310 | EXPECT_FALSE(d.received_data_before_response()); |
| 311 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 312 | CheckSSLInfo(r.ssl_info()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 313 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 314 | } |
| 315 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 316 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
[email protected] | 34759995 | 2010-10-28 11:57:36 | [diff] [blame] | 317 | net::TestServer::HTTPSOptions https_options( |
| 318 | net::TestServer::HTTPSOptions::CERT_MISMATCHED_NAME); |
| 319 | net::TestServer test_server(https_options, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 320 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 321 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 322 | |
| 323 | bool err_allowed = true; |
| 324 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 325 | TestDelegate d; |
| 326 | { |
| 327 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 328 | TestURLRequest r(test_server.GetURL(""), &d); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 329 | |
| 330 | r.Start(); |
| 331 | EXPECT_TRUE(r.is_pending()); |
| 332 | |
| 333 | MessageLoop::current()->Run(); |
| 334 | |
| 335 | EXPECT_EQ(1, d.response_started_count()); |
| 336 | EXPECT_FALSE(d.received_data_before_response()); |
| 337 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 338 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 339 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 340 | CheckSSLInfo(r.ssl_info()); |
| 341 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 342 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 343 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 348 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
[email protected] | 34759995 | 2010-10-28 11:57:36 | [diff] [blame] | 349 | net::TestServer::HTTPSOptions https_options( |
| 350 | net::TestServer::HTTPSOptions::CERT_EXPIRED); |
| 351 | net::TestServer test_server(https_options, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 352 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 353 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 354 | |
| 355 | // Iterate from false to true, just so that we do the opposite of the |
| 356 | // previous test in order to increase test coverage. |
| 357 | bool err_allowed = false; |
| 358 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 359 | TestDelegate d; |
| 360 | { |
| 361 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 362 | TestURLRequest r(test_server.GetURL(""), &d); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 363 | |
| 364 | r.Start(); |
| 365 | EXPECT_TRUE(r.is_pending()); |
| 366 | |
| 367 | MessageLoop::current()->Run(); |
| 368 | |
| 369 | EXPECT_EQ(1, d.response_started_count()); |
| 370 | EXPECT_FALSE(d.received_data_before_response()); |
| 371 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 372 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 373 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 374 | CheckSSLInfo(r.ssl_info()); |
| 375 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 376 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 377 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 381 | |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 382 | namespace { |
| 383 | |
| 384 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 385 | public: |
| 386 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 387 | } |
| 388 | virtual void OnCertificateRequested( |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 389 | net::URLRequest* request, |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 390 | net::SSLCertRequestInfo* cert_request_info) { |
| 391 | on_certificate_requested_count_++; |
| 392 | MessageLoop::current()->Quit(); |
| 393 | } |
| 394 | int on_certificate_requested_count() { |
| 395 | return on_certificate_requested_count_; |
| 396 | } |
| 397 | private: |
| 398 | int on_certificate_requested_count_; |
| 399 | }; |
| 400 | |
| 401 | } // namespace |
| 402 | |
| 403 | // TODO(davidben): Test the rest of the code. Specifically, |
| 404 | // - Filtering which certificates to select. |
| 405 | // - Sending a certificate back. |
| 406 | // - Getting a certificate request in an SSL renegotiation sending the |
| 407 | // HTTP request. |
| 408 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
[email protected] | 34759995 | 2010-10-28 11:57:36 | [diff] [blame] | 409 | net::TestServer::HTTPSOptions https_options; |
| 410 | https_options.request_client_certificate = true; |
| 411 | net::TestServer test_server(https_options, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 412 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 413 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 414 | |
| 415 | SSLClientAuthTestDelegate d; |
| 416 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 417 | TestURLRequest r(test_server.GetURL(""), &d); |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 418 | |
| 419 | r.Start(); |
| 420 | EXPECT_TRUE(r.is_pending()); |
| 421 | |
| 422 | MessageLoop::current()->Run(); |
| 423 | |
| 424 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 425 | EXPECT_FALSE(d.received_data_before_response()); |
| 426 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 427 | |
| 428 | // Send no certificate. |
| 429 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 430 | // all platforms so we can test sending a cert as well. |
| 431 | r.ContinueWithCertificate(NULL); |
| 432 | |
| 433 | MessageLoop::current()->Run(); |
| 434 | |
| 435 | EXPECT_EQ(1, d.response_started_count()); |
| 436 | EXPECT_FALSE(d.received_data_before_response()); |
| 437 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 441 | TEST_F(URLRequestTestHTTP, CancelTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 442 | TestDelegate d; |
| 443 | { |
| 444 | TestURLRequest r(GURL("http://www.google.com/"), &d); |
| 445 | |
| 446 | r.Start(); |
| 447 | EXPECT_TRUE(r.is_pending()); |
| 448 | |
| 449 | r.Cancel(); |
| 450 | |
| 451 | MessageLoop::current()->Run(); |
| 452 | |
| 453 | // We expect to receive OnResponseStarted even though the request has been |
| 454 | // cancelled. |
| 455 | EXPECT_EQ(1, d.response_started_count()); |
| 456 | EXPECT_EQ(0, d.bytes_received()); |
| 457 | EXPECT_FALSE(d.received_data_before_response()); |
| 458 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 459 | } |
| 460 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 461 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 462 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 463 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 464 | TestDelegate d; |
| 465 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 466 | TestURLRequest r(test_server_.GetURL(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 467 | |
| 468 | d.set_cancel_in_response_started(true); |
| 469 | |
| 470 | r.Start(); |
| 471 | EXPECT_TRUE(r.is_pending()); |
| 472 | |
| 473 | MessageLoop::current()->Run(); |
| 474 | |
| 475 | EXPECT_EQ(1, d.response_started_count()); |
| 476 | EXPECT_EQ(0, d.bytes_received()); |
| 477 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 478 | EXPECT_EQ(net::URLRequestStatus::CANCELED, r.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 479 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 480 | } |
| 481 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 482 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 483 | ASSERT_TRUE(test_server_.Start()); |
| 484 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 485 | TestDelegate d; |
| 486 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 487 | TestURLRequest r(test_server_.GetURL(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 488 | |
| 489 | d.set_cancel_in_received_data(true); |
| 490 | |
| 491 | r.Start(); |
| 492 | EXPECT_TRUE(r.is_pending()); |
| 493 | |
| 494 | MessageLoop::current()->Run(); |
| 495 | |
| 496 | EXPECT_EQ(1, d.response_started_count()); |
| 497 | // There is no guarantee about how much data was received |
| 498 | // before the cancel was issued. It could have been 0 bytes, |
| 499 | // or it could have been all the bytes. |
| 500 | // EXPECT_EQ(0, d.bytes_received()); |
| 501 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 502 | EXPECT_EQ(net::URLRequestStatus::CANCELED, r.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 503 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 506 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 507 | ASSERT_TRUE(test_server_.Start()); |
| 508 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 509 | TestDelegate d; |
| 510 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 511 | TestURLRequest r(test_server_.GetURL(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 512 | |
| 513 | r.Start(); |
| 514 | EXPECT_TRUE(r.is_pending()); |
| 515 | |
| 516 | // The request will be implicitly canceled when it is destroyed. The |
| 517 | // test delegate must not post a quit message when this happens because |
| 518 | // this test doesn't actually have a message loop. The quit message would |
| 519 | // get put on this thread's message queue and the next test would exit |
| 520 | // early, causing problems. |
| 521 | d.set_quit_on_complete(false); |
| 522 | } |
| 523 | // expect things to just cleanup properly. |
| 524 | |
| 525 | // we won't actually get a received reponse here because we've never run the |
| 526 | // message loop |
| 527 | EXPECT_FALSE(d.received_data_before_response()); |
| 528 | EXPECT_EQ(0, d.bytes_received()); |
| 529 | } |
| 530 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 531 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 532 | ASSERT_TRUE(test_server_.Start()); |
| 533 | |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 534 | scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 535 | |
| 536 | // populate cache |
| 537 | { |
| 538 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 539 | net::URLRequest r(test_server_.GetURL("cachetime"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 540 | r.set_context(context); |
| 541 | r.Start(); |
| 542 | MessageLoop::current()->Run(); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 543 | EXPECT_EQ(net::URLRequestStatus::SUCCESS, r.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // cancel read from cache (see bug 990242) |
| 547 | { |
| 548 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 549 | net::URLRequest r(test_server_.GetURL("cachetime"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 550 | r.set_context(context); |
| 551 | r.Start(); |
| 552 | r.Cancel(); |
| 553 | MessageLoop::current()->Run(); |
| 554 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 555 | EXPECT_EQ(net::URLRequestStatus::CANCELED, r.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 556 | EXPECT_EQ(1, d.response_started_count()); |
| 557 | EXPECT_EQ(0, d.bytes_received()); |
| 558 | EXPECT_FALSE(d.received_data_before_response()); |
| 559 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 560 | } |
| 561 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 562 | TEST_F(URLRequestTestHTTP, PostTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 563 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 564 | HTTPUploadDataOperationTest("POST"); |
| 565 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 566 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 567 | TEST_F(URLRequestTestHTTP, PutTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 568 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 569 | HTTPUploadDataOperationTest("PUT"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 570 | } |
| 571 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 572 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 573 | ASSERT_TRUE(test_server_.Start()); |
| 574 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 575 | TestDelegate d; |
| 576 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 577 | TestURLRequest r(test_server_.GetURL("echo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 578 | r.set_method("POST"); |
| 579 | |
| 580 | r.Start(); |
| 581 | EXPECT_TRUE(r.is_pending()); |
| 582 | |
| 583 | MessageLoop::current()->Run(); |
| 584 | |
| 585 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 586 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 587 | |
| 588 | EXPECT_FALSE(d.received_data_before_response()); |
| 589 | EXPECT_TRUE(d.data_received().empty()); |
| 590 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 591 | } |
| 592 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 593 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 594 | ASSERT_TRUE(test_server_.Start()); |
| 595 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 596 | TestDelegate d; |
| 597 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 598 | TestURLRequest r(test_server_.GetURL("echo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 599 | r.set_method("POST"); |
| 600 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 601 | FilePath dir; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 602 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 603 | file_util::SetCurrentDirectory(dir); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 604 | |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 605 | FilePath path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 606 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 607 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 608 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 609 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 610 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 611 | r.AppendFileToUpload(path); |
| 612 | |
| 613 | // This file should just be ignored in the upload stream. |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 614 | r.AppendFileToUpload(FilePath(FILE_PATH_LITERAL( |
| 615 | "c:\\path\\to\\non\\existant\\file.randomness.12345"))); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 616 | |
| 617 | r.Start(); |
| 618 | EXPECT_TRUE(r.is_pending()); |
| 619 | |
| 620 | MessageLoop::current()->Run(); |
| 621 | |
[email protected] | 10a1fe9 | 2008-11-04 21:47:02 | [diff] [blame] | 622 | int64 longsize; |
| 623 | ASSERT_EQ(true, file_util::GetFileSize(path, &longsize)); |
| 624 | int size = static_cast<int>(longsize); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 625 | scoped_array<char> buf(new char[size]); |
| 626 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 627 | int size_read = static_cast<int>(file_util::ReadFile(path, |
| 628 | buf.get(), size)); |
[email protected] | eac0709a | 2008-11-04 21:00:46 | [diff] [blame] | 629 | ASSERT_EQ(size, size_read); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 630 | |
| 631 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 632 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 633 | |
| 634 | EXPECT_FALSE(d.received_data_before_response()); |
| 635 | |
| 636 | ASSERT_EQ(size, d.bytes_received()); |
| 637 | EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size)); |
| 638 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 639 | } |
| 640 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 641 | TEST_F(URLRequestTest, AboutBlankTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 642 | TestDelegate d; |
| 643 | { |
| 644 | TestURLRequest r(GURL("about:blank"), &d); |
| 645 | |
| 646 | r.Start(); |
| 647 | EXPECT_TRUE(r.is_pending()); |
| 648 | |
| 649 | MessageLoop::current()->Run(); |
| 650 | |
| 651 | EXPECT_TRUE(!r.is_pending()); |
| 652 | EXPECT_FALSE(d.received_data_before_response()); |
| 653 | EXPECT_EQ(d.bytes_received(), 0); |
| 654 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 655 | } |
| 656 | |
[email protected] | fcb7fd9 | 2009-10-22 04:18:58 | [diff] [blame] | 657 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 658 | TestDelegate d; |
| 659 | { |
| 660 | // Use our nice little Chrome logo. |
| 661 | TestURLRequest r(GURL( |
| 662 | "data:image/png;base64," |
| 663 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3" |
| 664 | "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD" |
| 665 | "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t" |
| 666 | "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9" |
| 667 | "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1" |
| 668 | "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z" |
| 669 | "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW" |
| 670 | "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW" |
| 671 | "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb" |
| 672 | "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5" |
| 673 | "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV" |
| 674 | "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq" |
| 675 | "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F" |
| 676 | "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB" |
| 677 | "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM" |
| 678 | "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm" |
| 679 | "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En" |
| 680 | "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
| 681 | &d); |
| 682 | |
| 683 | r.Start(); |
| 684 | EXPECT_TRUE(r.is_pending()); |
| 685 | |
| 686 | MessageLoop::current()->Run(); |
| 687 | |
| 688 | EXPECT_TRUE(!r.is_pending()); |
| 689 | EXPECT_FALSE(d.received_data_before_response()); |
| 690 | EXPECT_EQ(d.bytes_received(), 911); |
| 691 | } |
| 692 | } |
| 693 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 694 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 695 | FilePath app_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 696 | PathService::Get(base::FILE_EXE, &app_path); |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 697 | GURL app_url = net::FilePathToFileURL(app_path); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 698 | |
| 699 | TestDelegate d; |
| 700 | { |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 701 | TestURLRequest r(app_url, &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 702 | |
| 703 | r.Start(); |
| 704 | EXPECT_TRUE(r.is_pending()); |
| 705 | |
| 706 | MessageLoop::current()->Run(); |
| 707 | |
[email protected] | b77280c | 2009-03-20 17:27:47 | [diff] [blame] | 708 | int64 file_size = -1; |
| 709 | EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 710 | |
| 711 | EXPECT_TRUE(!r.is_pending()); |
| 712 | EXPECT_EQ(1, d.response_started_count()); |
| 713 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 714 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 715 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 716 | } |
| 717 | |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 718 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 719 | const size_t buffer_size = 4000; |
| 720 | scoped_array<char> buffer(new char[buffer_size]); |
| 721 | FillBuffer(buffer.get(), buffer_size); |
| 722 | |
| 723 | FilePath temp_path; |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 724 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 725 | GURL temp_url = net::FilePathToFileURL(temp_path); |
[email protected] | 1e5ae86 | 2009-10-14 22:14:53 | [diff] [blame] | 726 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 727 | |
| 728 | int64 file_size; |
| 729 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 730 | |
| 731 | const size_t first_byte_position = 500; |
| 732 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 733 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 734 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 735 | buffer.get() + last_byte_position + 1); |
| 736 | |
| 737 | TestDelegate d; |
| 738 | { |
| 739 | TestURLRequest r(temp_url, &d); |
| 740 | |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 741 | net::HttpRequestHeaders headers; |
| 742 | headers.SetHeader(net::HttpRequestHeaders::kRange, |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 743 | base::StringPrintf( |
| 744 | "bytes=%" PRIuS "-%" PRIuS, |
| 745 | first_byte_position, last_byte_position)); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 746 | r.SetExtraRequestHeaders(headers); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 747 | r.Start(); |
| 748 | EXPECT_TRUE(r.is_pending()); |
| 749 | |
| 750 | MessageLoop::current()->Run(); |
| 751 | EXPECT_TRUE(!r.is_pending()); |
| 752 | EXPECT_EQ(1, d.response_started_count()); |
| 753 | EXPECT_FALSE(d.received_data_before_response()); |
| 754 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 755 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 756 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 757 | } |
| 758 | |
| 759 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 763 | const size_t buffer_size = 4000; |
| 764 | scoped_array<char> buffer(new char[buffer_size]); |
| 765 | FillBuffer(buffer.get(), buffer_size); |
| 766 | |
| 767 | FilePath temp_path; |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 768 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 769 | GURL temp_url = net::FilePathToFileURL(temp_path); |
[email protected] | 1e5ae86 | 2009-10-14 22:14:53 | [diff] [blame] | 770 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 771 | |
| 772 | int64 file_size; |
| 773 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 774 | |
| 775 | const size_t first_byte_position = 500; |
| 776 | const size_t last_byte_position = buffer_size - 1; |
| 777 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 778 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 779 | buffer.get() + last_byte_position + 1); |
| 780 | |
| 781 | TestDelegate d; |
| 782 | { |
| 783 | TestURLRequest r(temp_url, &d); |
| 784 | |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 785 | net::HttpRequestHeaders headers; |
| 786 | headers.SetHeader(net::HttpRequestHeaders::kRange, |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 787 | base::StringPrintf("bytes=%" PRIuS "-", |
| 788 | first_byte_position)); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 789 | r.SetExtraRequestHeaders(headers); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 790 | r.Start(); |
| 791 | EXPECT_TRUE(r.is_pending()); |
| 792 | |
| 793 | MessageLoop::current()->Run(); |
| 794 | EXPECT_TRUE(!r.is_pending()); |
| 795 | EXPECT_EQ(1, d.response_started_count()); |
| 796 | EXPECT_FALSE(d.received_data_before_response()); |
| 797 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 798 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 799 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 800 | } |
| 801 | |
| 802 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 806 | const size_t buffer_size = 400000; |
| 807 | scoped_array<char> buffer(new char[buffer_size]); |
| 808 | FillBuffer(buffer.get(), buffer_size); |
| 809 | |
| 810 | FilePath temp_path; |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 811 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 812 | GURL temp_url = net::FilePathToFileURL(temp_path); |
[email protected] | 1e5ae86 | 2009-10-14 22:14:53 | [diff] [blame] | 813 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 814 | |
| 815 | int64 file_size; |
| 816 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 817 | |
| 818 | TestDelegate d; |
| 819 | { |
| 820 | TestURLRequest r(temp_url, &d); |
| 821 | |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 822 | net::HttpRequestHeaders headers; |
| 823 | headers.SetHeader(net::HttpRequestHeaders::kRange, |
| 824 | "bytes=0-0,10-200,200-300"); |
| 825 | r.SetExtraRequestHeaders(headers); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 826 | r.Start(); |
| 827 | EXPECT_TRUE(r.is_pending()); |
| 828 | |
| 829 | MessageLoop::current()->Run(); |
| 830 | EXPECT_TRUE(d.request_failed()); |
| 831 | } |
| 832 | |
| 833 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 834 | } |
| 835 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 836 | TEST_F(URLRequestTest, InvalidUrlTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 837 | TestDelegate d; |
| 838 | { |
| 839 | TestURLRequest r(GURL("invalid url"), &d); |
| 840 | |
| 841 | r.Start(); |
| 842 | EXPECT_TRUE(r.is_pending()); |
| 843 | |
| 844 | MessageLoop::current()->Run(); |
| 845 | EXPECT_TRUE(d.request_failed()); |
| 846 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 847 | } |
| 848 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 849 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 850 | ASSERT_TRUE(test_server_.Start()); |
| 851 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 852 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 853 | TestURLRequest req(test_server_.GetURL("files/with-headers.html"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 854 | req.Start(); |
| 855 | MessageLoop::current()->Run(); |
| 856 | |
| 857 | const net::HttpResponseHeaders* headers = req.response_headers(); |
[email protected] | 589deddb | 2009-10-05 23:41:40 | [diff] [blame] | 858 | |
| 859 | // Simple sanity check that response_info() accesses the same data. |
| 860 | EXPECT_EQ(headers, req.response_info().headers.get()); |
| 861 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 862 | std::string header; |
| 863 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 864 | EXPECT_EQ("private", header); |
| 865 | |
| 866 | header.clear(); |
| 867 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 868 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 869 | |
| 870 | // The response has two "X-Multiple-Entries" headers. |
| 871 | // This verfies our output has them concatenated together. |
| 872 | header.clear(); |
| 873 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 874 | EXPECT_EQ("a, b", header); |
| 875 | } |
| 876 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 877 | #if defined(OS_WIN) |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 878 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 879 | FilePath app_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 880 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 881 | app_path = app_path.AppendASCII("net"); |
| 882 | app_path = app_path.AppendASCII("data"); |
| 883 | app_path = app_path.AppendASCII("url_request_unittest"); |
| 884 | app_path = app_path.AppendASCII("with-headers.html"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 885 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 886 | std::wstring lnk_path = app_path.value() + L".lnk"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 887 | |
| 888 | HRESULT result; |
| 889 | IShellLink *shell = NULL; |
| 890 | IPersistFile *persist = NULL; |
| 891 | |
| 892 | CoInitialize(NULL); |
| 893 | // Temporarily create a shortcut for test |
| 894 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 895 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 896 | reinterpret_cast<LPVOID*>(&shell)); |
[email protected] | d149ce8 | 2009-07-01 23:57:02 | [diff] [blame] | 897 | ASSERT_TRUE(SUCCEEDED(result)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 898 | result = shell->QueryInterface(IID_IPersistFile, |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 899 | reinterpret_cast<LPVOID*>(&persist)); |
[email protected] | d149ce8 | 2009-07-01 23:57:02 | [diff] [blame] | 900 | ASSERT_TRUE(SUCCEEDED(result)); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 901 | result = shell->SetPath(app_path.value().c_str()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 902 | EXPECT_TRUE(SUCCEEDED(result)); |
| 903 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 904 | EXPECT_TRUE(SUCCEEDED(result)); |
| 905 | result = persist->Save(lnk_path.c_str(), TRUE); |
| 906 | EXPECT_TRUE(SUCCEEDED(result)); |
| 907 | if (persist) |
| 908 | persist->Release(); |
| 909 | if (shell) |
| 910 | shell->Release(); |
| 911 | |
| 912 | TestDelegate d; |
| 913 | { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 914 | TestURLRequest r(net::FilePathToFileURL(FilePath(lnk_path)), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 915 | |
| 916 | r.Start(); |
| 917 | EXPECT_TRUE(r.is_pending()); |
| 918 | |
| 919 | MessageLoop::current()->Run(); |
| 920 | |
| 921 | WIN32_FILE_ATTRIBUTE_DATA data; |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 922 | GetFileAttributesEx(app_path.value().c_str(), |
| 923 | GetFileExInfoStandard, &data); |
| 924 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 925 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 926 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 927 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 928 | scoped_array<char> buffer(new char[data.nFileSizeLow]); |
| 929 | DWORD read_size; |
| 930 | BOOL result; |
| 931 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 932 | &read_size, NULL); |
| 933 | std::string content(buffer.get(), read_size); |
| 934 | CloseHandle(file); |
| 935 | |
| 936 | EXPECT_TRUE(!r.is_pending()); |
| 937 | EXPECT_EQ(1, d.received_redirect_count()); |
| 938 | EXPECT_EQ(content, d.data_received()); |
| 939 | } |
| 940 | |
| 941 | // Clean the shortcut |
| 942 | DeleteFile(lnk_path.c_str()); |
| 943 | CoUninitialize(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 944 | } |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 945 | #endif // defined(OS_WIN) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 946 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 947 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 948 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 949 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 950 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 951 | TestURLRequest req(test_server_.GetURL( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 952 | "files/content-type-normalization.html"), &d); |
| 953 | req.Start(); |
| 954 | MessageLoop::current()->Run(); |
| 955 | |
| 956 | std::string mime_type; |
| 957 | req.GetMimeType(&mime_type); |
| 958 | EXPECT_EQ("text/html", mime_type); |
| 959 | |
| 960 | std::string charset; |
| 961 | req.GetCharset(&charset); |
| 962 | EXPECT_EQ("utf-8", charset); |
| 963 | req.Cancel(); |
| 964 | } |
| 965 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 966 | TEST_F(URLRequestTest, FileDirCancelTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 967 | // Put in mock resource provider. |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 968 | net::NetModule::SetResourceProvider(TestNetResourceProvider); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 969 | |
| 970 | TestDelegate d; |
| 971 | { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 972 | FilePath file_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 973 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 974 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 975 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 976 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 977 | TestURLRequest req(net::FilePathToFileURL(file_path), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 978 | req.Start(); |
| 979 | EXPECT_TRUE(req.is_pending()); |
| 980 | |
| 981 | d.set_cancel_in_received_data_pending(true); |
| 982 | |
| 983 | MessageLoop::current()->Run(); |
| 984 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 985 | |
| 986 | // Take out mock resource provider. |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 987 | net::NetModule::SetResourceProvider(NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 988 | } |
| 989 | |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 990 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 991 | // There is an implicit redirect when loading a file path that matches a |
| 992 | // directory and does not end with a slash. Ensure that following such |
| 993 | // redirects does not crash. See http://crbug.com/18686. |
| 994 | |
| 995 | FilePath path; |
| 996 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 997 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 998 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 999 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 1000 | |
| 1001 | TestDelegate d; |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 1002 | TestURLRequest req(net::FilePathToFileURL(path), &d); |
| 1003 | req.Start(); |
| 1004 | MessageLoop::current()->Run(); |
| 1005 | |
[email protected] | 0db5577 | 2010-06-14 22:27:44 | [diff] [blame] | 1006 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1007 | ASSERT_LT(0, d.bytes_received()); |
| 1008 | ASSERT_FALSE(d.request_failed()); |
| 1009 | ASSERT_TRUE(req.status().is_success()); |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 1010 | } |
| 1011 | |
[email protected] | 0db5577 | 2010-06-14 22:27:44 | [diff] [blame] | 1012 | #if defined(OS_WIN) |
| 1013 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1014 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1015 | TestDelegate d; |
| 1016 | TestURLRequest req(GURL("file:///"), &d); |
| 1017 | req.Start(); |
| 1018 | MessageLoop::current()->Run(); |
| 1019 | |
| 1020 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1021 | ASSERT_FALSE(req.status().is_success()); |
| 1022 | } |
| 1023 | #endif |
| 1024 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1025 | TEST_F(URLRequestTestHTTP, RestrictRedirects) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1026 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1027 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1028 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1029 | TestURLRequest req(test_server_.GetURL( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1030 | "files/redirect-to-file.html"), &d); |
| 1031 | req.Start(); |
| 1032 | MessageLoop::current()->Run(); |
| 1033 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1034 | EXPECT_EQ(net::URLRequestStatus::FAILED, req.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1035 | EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error()); |
| 1036 | } |
| 1037 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1038 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1039 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | 4e66ed1 | 2009-07-21 23:38:42 | [diff] [blame] | 1040 | |
| 1041 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1042 | TestURLRequest req(test_server_.GetURL( |
[email protected] | 4e66ed1 | 2009-07-21 23:38:42 | [diff] [blame] | 1043 | "files/redirect-to-invalid-url.html"), &d); |
| 1044 | req.Start(); |
| 1045 | MessageLoop::current()->Run(); |
| 1046 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1047 | EXPECT_EQ(net::URLRequestStatus::FAILED, req.status().status()); |
[email protected] | 4e66ed1 | 2009-07-21 23:38:42 | [diff] [blame] | 1048 | EXPECT_EQ(net::ERR_INVALID_URL, req.status().os_error()); |
| 1049 | } |
| 1050 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1051 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1052 | ASSERT_TRUE(test_server_.Start()); |
| 1053 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1054 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1055 | TestURLRequest req(test_server_.GetURL( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1056 | "echoheader?Referer"), &d); |
| 1057 | req.set_referrer("http://user:[email protected]/"); |
| 1058 | req.Start(); |
| 1059 | MessageLoop::current()->Run(); |
| 1060 | |
| 1061 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 1062 | } |
| 1063 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1064 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1065 | ASSERT_TRUE(test_server_.Start()); |
| 1066 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1067 | TestDelegate d; |
| 1068 | { |
| 1069 | d.set_cancel_in_received_redirect(true); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1070 | TestURLRequest req(test_server_.GetURL( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1071 | "files/redirect-test.html"), &d); |
| 1072 | req.Start(); |
| 1073 | MessageLoop::current()->Run(); |
| 1074 | |
| 1075 | EXPECT_EQ(1, d.response_started_count()); |
| 1076 | EXPECT_EQ(0, d.bytes_received()); |
| 1077 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1078 | EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1082 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1083 | ASSERT_TRUE(test_server_.Start()); |
| 1084 | |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1085 | TestDelegate d; |
| 1086 | { |
| 1087 | d.set_quit_on_redirect(true); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1088 | TestURLRequest req(test_server_.GetURL( |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1089 | "files/redirect-test.html"), &d); |
| 1090 | req.Start(); |
| 1091 | MessageLoop::current()->Run(); |
| 1092 | |
| 1093 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1094 | |
| 1095 | req.FollowDeferredRedirect(); |
| 1096 | MessageLoop::current()->Run(); |
| 1097 | |
| 1098 | EXPECT_EQ(1, d.response_started_count()); |
| 1099 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1100 | EXPECT_EQ(net::URLRequestStatus::SUCCESS, req.status().status()); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1101 | |
| 1102 | FilePath path; |
| 1103 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 1104 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 1105 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 1106 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 1107 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 1108 | |
| 1109 | std::string contents; |
| 1110 | EXPECT_TRUE(file_util::ReadFileToString(path, &contents)); |
| 1111 | EXPECT_EQ(contents, d.data_received()); |
| 1112 | } |
| 1113 | } |
| 1114 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1115 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1116 | ASSERT_TRUE(test_server_.Start()); |
| 1117 | |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1118 | TestDelegate d; |
| 1119 | { |
| 1120 | d.set_quit_on_redirect(true); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1121 | TestURLRequest req(test_server_.GetURL( |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1122 | "files/redirect-test.html"), &d); |
| 1123 | req.Start(); |
| 1124 | MessageLoop::current()->Run(); |
| 1125 | |
| 1126 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1127 | |
| 1128 | req.Cancel(); |
| 1129 | MessageLoop::current()->Run(); |
| 1130 | |
| 1131 | EXPECT_EQ(1, d.response_started_count()); |
| 1132 | EXPECT_EQ(0, d.bytes_received()); |
| 1133 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1134 | EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1138 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1139 | ASSERT_TRUE(test_server_.Start()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1140 | |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 1141 | scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1142 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1143 | // populate the cache |
| 1144 | { |
| 1145 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1146 | net::URLRequest req(test_server_.GetURL("echoheader?foo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1147 | req.set_context(context); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 1148 | net::HttpRequestHeaders headers; |
| 1149 | headers.SetHeader("foo", "1"); |
| 1150 | req.SetExtraRequestHeaders(headers); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1151 | req.Start(); |
| 1152 | MessageLoop::current()->Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1153 | } |
| 1154 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1155 | // expect a cache hit |
| 1156 | { |
| 1157 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1158 | net::URLRequest req(test_server_.GetURL("echoheader?foo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1159 | req.set_context(context); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 1160 | net::HttpRequestHeaders headers; |
| 1161 | headers.SetHeader("foo", "1"); |
| 1162 | req.SetExtraRequestHeaders(headers); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1163 | req.Start(); |
| 1164 | MessageLoop::current()->Run(); |
| 1165 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 1166 | EXPECT_TRUE(req.was_cached()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | // expect a cache miss |
| 1170 | { |
| 1171 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1172 | net::URLRequest req(test_server_.GetURL("echoheader?foo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1173 | req.set_context(context); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 1174 | net::HttpRequestHeaders headers; |
| 1175 | headers.SetHeader("foo", "2"); |
| 1176 | req.SetExtraRequestHeaders(headers); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1177 | req.Start(); |
| 1178 | MessageLoop::current()->Run(); |
| 1179 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 1180 | EXPECT_FALSE(req.was_cached()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 1184 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1185 | ASSERT_TRUE(test_server_.Start()); |
| 1186 | |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 1187 | scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1188 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1189 | // populate the cache |
| 1190 | { |
| 1191 | TestDelegate d; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1192 | d.set_username(kUser); |
| 1193 | d.set_password(kSecret); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1194 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1195 | net::URLRequest r(test_server_.GetURL("auth-basic"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1196 | r.set_context(context); |
| 1197 | r.Start(); |
| 1198 | |
| 1199 | MessageLoop::current()->Run(); |
| 1200 | |
| 1201 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1202 | } |
| 1203 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1204 | // repeat request with end-to-end validation. since auth-basic results in a |
| 1205 | // cachable page, we expect this test to result in a 304. in which case, the |
| 1206 | // response should be fetched from the cache. |
| 1207 | { |
| 1208 | TestDelegate d; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1209 | d.set_username(kUser); |
| 1210 | d.set_password(kSecret); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1211 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1212 | net::URLRequest r(test_server_.GetURL("auth-basic"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1213 | r.set_context(context); |
| 1214 | r.set_load_flags(net::LOAD_VALIDATE_CACHE); |
| 1215 | r.Start(); |
| 1216 | |
| 1217 | MessageLoop::current()->Run(); |
| 1218 | |
| 1219 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 1220 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 1221 | // Should be the same cached document. |
| 1222 | EXPECT_TRUE(r.was_cached()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1223 | } |
| 1224 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1225 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1226 | // Check that Set-Cookie headers in 401 responses are respected. |
| 1227 | // http://crbug.com/6450 |
[email protected] | be6fca6c | 2010-01-30 21:48:57 | [diff] [blame] | 1228 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1229 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1230 | |
| 1231 | GURL url_requiring_auth = |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1232 | test_server_.GetURL("auth-basic?set-cookie-if-challenged"); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1233 | |
| 1234 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 1235 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 1236 | { |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 1237 | scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1238 | TestDelegate d; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1239 | d.set_username(kUser); |
| 1240 | d.set_password(kSecret); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1241 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1242 | net::URLRequest r(url_requiring_auth, &d); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1243 | r.set_context(context); |
| 1244 | r.Start(); |
| 1245 | |
| 1246 | MessageLoop::current()->Run(); |
| 1247 | |
| 1248 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 1249 | |
| 1250 | // Make sure we sent the cookie in the restarted transaction. |
| 1251 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 1252 | != std::string::npos); |
| 1253 | } |
| 1254 | |
| 1255 | // Same test as above, except this time the restart is initiated earlier |
| 1256 | // (without user intervention since identity is embedded in the URL). |
| 1257 | { |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 1258 | scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1259 | TestDelegate d; |
| 1260 | |
| 1261 | GURL::Replacements replacements; |
| 1262 | std::string username("user2"); |
| 1263 | std::string password("secret"); |
| 1264 | replacements.SetUsernameStr(username); |
| 1265 | replacements.SetPasswordStr(password); |
| 1266 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 1267 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1268 | net::URLRequest r(url_with_identity, &d); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1269 | r.set_context(context); |
| 1270 | r.Start(); |
| 1271 | |
| 1272 | MessageLoop::current()->Run(); |
| 1273 | |
| 1274 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 1275 | |
| 1276 | // Make sure we sent the cookie in the restarted transaction. |
| 1277 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 1278 | != std::string::npos); |
| 1279 | } |
| 1280 | } |
| 1281 | |
[email protected] | be6fca6c | 2010-01-30 21:48:57 | [diff] [blame] | 1282 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1283 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1284 | ASSERT_TRUE(test_server.Start()); |
| 1285 | |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 1286 | scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1287 | |
| 1288 | // Set up a cookie. |
| 1289 | { |
| 1290 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1291 | net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1292 | req.set_context(context); |
| 1293 | req.Start(); |
| 1294 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1295 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1296 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | // Verify that the cookie is set. |
| 1300 | { |
| 1301 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1302 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1303 | req.set_context(context); |
| 1304 | req.Start(); |
| 1305 | MessageLoop::current()->Run(); |
| 1306 | |
| 1307 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 1308 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1309 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1310 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 1314 | { |
| 1315 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1316 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1317 | req.set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES); |
| 1318 | req.set_context(context); |
| 1319 | req.Start(); |
| 1320 | MessageLoop::current()->Run(); |
| 1321 | |
| 1322 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 1323 | == std::string::npos); |
[email protected] | abf03a0 | 2010-03-12 05:02:13 | [diff] [blame] | 1324 | |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 1325 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
[email protected] | abf03a0 | 2010-03-12 05:02:13 | [diff] [blame] | 1326 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1327 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | |
[email protected] | be6fca6c | 2010-01-30 21:48:57 | [diff] [blame] | 1331 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1332 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1333 | ASSERT_TRUE(test_server.Start()); |
| 1334 | |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 1335 | scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1336 | |
| 1337 | // Set up a cookie. |
| 1338 | { |
| 1339 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1340 | net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), |
| 1341 | &d); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1342 | req.set_context(context); |
| 1343 | req.Start(); |
| 1344 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1345 | |
| 1346 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1347 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 1348 | EXPECT_EQ(1, d.set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | // Try to set-up another cookie and update the previous cookie. |
| 1352 | { |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1353 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1354 | net::URLRequest req(test_server.GetURL( |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1355 | "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d); |
| 1356 | req.set_load_flags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| 1357 | req.set_context(context); |
| 1358 | req.Start(); |
| 1359 | |
| 1360 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1361 | |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 1362 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1363 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
[email protected] | abf03a0 | 2010-03-12 05:02:13 | [diff] [blame] | 1364 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 1365 | EXPECT_EQ(0, d.set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | // Verify the cookies weren't saved or updated. |
| 1369 | { |
| 1370 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1371 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1372 | req.set_context(context); |
| 1373 | req.Start(); |
| 1374 | MessageLoop::current()->Run(); |
| 1375 | |
| 1376 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 1377 | == std::string::npos); |
| 1378 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 1379 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1380 | |
| 1381 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1382 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 1383 | EXPECT_EQ(0, d.set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1387 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1388 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1389 | ASSERT_TRUE(test_server.Start()); |
| 1390 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1391 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1392 | |
| 1393 | // Set up a cookie. |
| 1394 | { |
| 1395 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1396 | net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1397 | req.set_context(context); |
| 1398 | req.Start(); |
| 1399 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1400 | |
| 1401 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1402 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | // Verify that the cookie is set. |
| 1406 | { |
| 1407 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1408 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1409 | req.set_context(context); |
| 1410 | req.Start(); |
| 1411 | MessageLoop::current()->Run(); |
| 1412 | |
| 1413 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 1414 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1415 | |
| 1416 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1417 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | // Verify that the cookie isn't sent. |
| 1421 | { |
| 1422 | TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES); |
| 1423 | context->set_cookie_policy(&cookie_policy); |
| 1424 | |
| 1425 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1426 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1427 | req.set_context(context); |
| 1428 | req.Start(); |
| 1429 | MessageLoop::current()->Run(); |
| 1430 | |
| 1431 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 1432 | == std::string::npos); |
| 1433 | |
| 1434 | context->set_cookie_policy(NULL); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1435 | |
| 1436 | EXPECT_EQ(1, d.blocked_get_cookies_count()); |
| 1437 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1442 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1443 | ASSERT_TRUE(test_server.Start()); |
| 1444 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1445 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1446 | |
| 1447 | // Set up a cookie. |
| 1448 | { |
| 1449 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1450 | net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), |
| 1451 | &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1452 | req.set_context(context); |
| 1453 | req.Start(); |
| 1454 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1455 | |
| 1456 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1457 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | // Try to set-up another cookie and update the previous cookie. |
| 1461 | { |
| 1462 | TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE); |
| 1463 | context->set_cookie_policy(&cookie_policy); |
| 1464 | |
| 1465 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1466 | net::URLRequest req(test_server.GetURL( |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1467 | "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d); |
| 1468 | req.set_context(context); |
| 1469 | req.Start(); |
| 1470 | |
| 1471 | MessageLoop::current()->Run(); |
| 1472 | |
| 1473 | context->set_cookie_policy(NULL); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1474 | |
| 1475 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1476 | EXPECT_EQ(2, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | // Verify the cookies weren't saved or updated. |
| 1481 | { |
| 1482 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1483 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1484 | req.set_context(context); |
| 1485 | req.Start(); |
| 1486 | MessageLoop::current()->Run(); |
| 1487 | |
| 1488 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 1489 | == std::string::npos); |
| 1490 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 1491 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1492 | |
| 1493 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1494 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 1498 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 1499 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1500 | ASSERT_TRUE(test_server.Start()); |
| 1501 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1502 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 1503 | |
| 1504 | // Set up an empty cookie. |
| 1505 | { |
| 1506 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1507 | net::URLRequest req(test_server.GetURL("set-cookie"), &d); |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 1508 | req.set_context(context); |
| 1509 | req.Start(); |
| 1510 | MessageLoop::current()->Run(); |
| 1511 | |
| 1512 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1513 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
| 1514 | EXPECT_EQ(0, d.set_cookie_count()); |
| 1515 | } |
| 1516 | } |
| 1517 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1518 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1519 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1520 | ASSERT_TRUE(test_server.Start()); |
| 1521 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1522 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1523 | |
| 1524 | // Set up a cookie. |
| 1525 | { |
| 1526 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1527 | net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1528 | req.set_context(context); |
| 1529 | req.Start(); |
| 1530 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1531 | |
| 1532 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1533 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | // Verify that the cookie is set. |
| 1537 | { |
| 1538 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1539 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1540 | req.set_context(context); |
| 1541 | req.Start(); |
| 1542 | MessageLoop::current()->Run(); |
| 1543 | |
| 1544 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 1545 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1546 | |
| 1547 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1548 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | // Verify that the cookie isn't sent. |
| 1552 | { |
| 1553 | TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES | |
| 1554 | TestCookiePolicy::ASYNC); |
| 1555 | context->set_cookie_policy(&cookie_policy); |
| 1556 | |
| 1557 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1558 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1559 | req.set_context(context); |
| 1560 | req.Start(); |
| 1561 | MessageLoop::current()->Run(); |
| 1562 | |
| 1563 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 1564 | == std::string::npos); |
| 1565 | |
| 1566 | context->set_cookie_policy(NULL); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1567 | |
| 1568 | EXPECT_EQ(1, d.blocked_get_cookies_count()); |
| 1569 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1574 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1575 | ASSERT_TRUE(test_server.Start()); |
| 1576 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1577 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1578 | |
| 1579 | // Set up a cookie. |
| 1580 | { |
| 1581 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1582 | net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), |
| 1583 | &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1584 | req.set_context(context); |
| 1585 | req.Start(); |
| 1586 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1587 | |
| 1588 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1589 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | // Try to set-up another cookie and update the previous cookie. |
| 1593 | { |
| 1594 | TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE | |
| 1595 | TestCookiePolicy::ASYNC); |
| 1596 | context->set_cookie_policy(&cookie_policy); |
| 1597 | |
| 1598 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1599 | net::URLRequest req(test_server.GetURL( |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1600 | "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d); |
| 1601 | req.set_context(context); |
| 1602 | req.Start(); |
| 1603 | |
| 1604 | MessageLoop::current()->Run(); |
| 1605 | |
| 1606 | context->set_cookie_policy(NULL); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1607 | |
| 1608 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1609 | EXPECT_EQ(2, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | // Verify the cookies weren't saved or updated. |
| 1613 | { |
| 1614 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1615 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1616 | req.set_context(context); |
| 1617 | req.Start(); |
| 1618 | MessageLoop::current()->Run(); |
| 1619 | |
| 1620 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 1621 | == std::string::npos); |
| 1622 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 1623 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1624 | |
| 1625 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1626 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1630 | TEST_F(URLRequestTest, CancelTest_During_CookiePolicy) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1631 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1632 | ASSERT_TRUE(test_server.Start()); |
| 1633 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1634 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1635 | |
| 1636 | TestCookiePolicy cookie_policy(TestCookiePolicy::ASYNC); |
| 1637 | context->set_cookie_policy(&cookie_policy); |
| 1638 | |
| 1639 | // Set up a cookie. |
| 1640 | { |
| 1641 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1642 | net::URLRequest req(test_server.GetURL("set-cookie?A=1&B=2&C=3"), |
| 1643 | &d); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1644 | req.set_context(context); |
| 1645 | req.Start(); // Triggers an asynchronous cookie policy check. |
| 1646 | |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1647 | // But, now we cancel the request by letting it go out of scope. This |
| 1648 | // should not cause a crash. |
| 1649 | |
| 1650 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1651 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
| 1652 | } |
| 1653 | |
| 1654 | context->set_cookie_policy(NULL); |
| 1655 | |
| 1656 | // Let the cookie policy complete. Make sure it handles the destruction of |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1657 | // the net::URLRequest properly. |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1658 | MessageLoop::current()->RunAllPending(); |
| 1659 | } |
| 1660 | |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 1661 | TEST_F(URLRequestTest, CancelTest_During_OnGetCookies) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1662 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1663 | ASSERT_TRUE(test_server.Start()); |
| 1664 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1665 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1666 | |
| 1667 | TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES); |
| 1668 | context->set_cookie_policy(&cookie_policy); |
| 1669 | |
| 1670 | // Set up a cookie. |
| 1671 | { |
| 1672 | TestDelegate d; |
| 1673 | d.set_cancel_in_get_cookies_blocked(true); |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1674 | net::URLRequest req(test_server.GetURL("set-cookie?A=1&B=2&C=3"), |
| 1675 | &d); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1676 | req.set_context(context); |
| 1677 | req.Start(); // Triggers an asynchronous cookie policy check. |
| 1678 | |
| 1679 | MessageLoop::current()->Run(); |
| 1680 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1681 | EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1682 | |
| 1683 | EXPECT_EQ(1, d.blocked_get_cookies_count()); |
| 1684 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
| 1685 | } |
| 1686 | |
| 1687 | context->set_cookie_policy(NULL); |
| 1688 | } |
| 1689 | |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 1690 | TEST_F(URLRequestTest, CancelTest_During_OnSetCookie) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1691 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1692 | ASSERT_TRUE(test_server.Start()); |
| 1693 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1694 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1695 | |
| 1696 | TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE); |
| 1697 | context->set_cookie_policy(&cookie_policy); |
| 1698 | |
| 1699 | // Set up a cookie. |
| 1700 | { |
| 1701 | TestDelegate d; |
| 1702 | d.set_cancel_in_set_cookie_blocked(true); |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1703 | net::URLRequest req(test_server.GetURL("set-cookie?A=1&B=2&C=3"), |
| 1704 | &d); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1705 | req.set_context(context); |
| 1706 | req.Start(); // Triggers an asynchronous cookie policy check. |
| 1707 | |
| 1708 | MessageLoop::current()->Run(); |
| 1709 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1710 | EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1711 | |
| 1712 | // Even though the response will contain 3 set-cookie headers, we expect |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 1713 | // only one to be blocked as that first one will cause OnSetCookie to be |
| 1714 | // called, which will cancel the request. Once canceled, it should not |
| 1715 | // attempt to set further cookies. |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1716 | |
| 1717 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1718 | EXPECT_EQ(1, d.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | context->set_cookie_policy(NULL); |
| 1722 | } |
| 1723 | |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 1724 | TEST_F(URLRequestTest, CookiePolicy_ForceSession) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1725 | net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath()); |
| 1726 | ASSERT_TRUE(test_server.Start()); |
| 1727 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1728 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 1729 | |
| 1730 | TestCookiePolicy cookie_policy(TestCookiePolicy::FORCE_SESSION); |
| 1731 | context->set_cookie_policy(&cookie_policy); |
| 1732 | |
| 1733 | // Set up a cookie. |
| 1734 | { |
| 1735 | TestDelegate d; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1736 | net::URLRequest req(test_server.GetURL( |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 1737 | "set-cookie?A=1;expires=\"Fri, 05 Feb 2010 23:42:01 GMT\""), &d); |
| 1738 | req.set_context(context); |
| 1739 | req.Start(); // Triggers an asynchronous cookie policy check. |
| 1740 | |
| 1741 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 1742 | |
| 1743 | EXPECT_EQ(0, d.blocked_get_cookies_count()); |
| 1744 | EXPECT_EQ(0, d.blocked_set_cookie_count()); |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | // Now, check the cookie store. |
[email protected] | 7d80c54 | 2010-12-03 15:30:36 | [diff] [blame] | 1748 | net::CookieList cookies = |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 1749 | context->cookie_store()->GetCookieMonster()->GetAllCookies(); |
| 1750 | EXPECT_EQ(1U, cookies.size()); |
[email protected] | 65781e9 | 2010-07-21 15:29:57 | [diff] [blame] | 1751 | EXPECT_FALSE(cookies[0].IsPersistent()); |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 1752 | |
| 1753 | context->set_cookie_policy(NULL); |
| 1754 | } |
| 1755 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1756 | // In this test, we do a POST which the server will 302 redirect. |
| 1757 | // The subsequent transaction should use GET, and should not send the |
| 1758 | // Content-Type header. |
| 1759 | // http://code.google.com/p/chromium/issues/detail?id=843 |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1760 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1761 | ASSERT_TRUE(test_server_.Start()); |
| 1762 | |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1763 | const char kData[] = "hello world"; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1764 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1765 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1766 | TestURLRequest req(test_server_.GetURL("files/redirect-to-echoall"), &d); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1767 | req.set_method("POST"); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1768 | req.set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1769 | |
| 1770 | // Set headers (some of which are specific to the POST). |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 1771 | net::HttpRequestHeaders headers; |
| 1772 | headers.AddHeadersFromString( |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1773 | "Content-Type: multipart/form-data; " |
| 1774 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 1775 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 1776 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1777 | "Accept-Language: en-US,en\r\n" |
| 1778 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1779 | "Content-Length: 11\r\n" |
[email protected] | a86c97cc | 2009-06-24 21:26:27 | [diff] [blame] | 1780 | "Origin: http://localhost:1337/"); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 1781 | req.SetExtraRequestHeaders(headers); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1782 | req.Start(); |
| 1783 | MessageLoop::current()->Run(); |
| 1784 | |
| 1785 | std::string mime_type; |
| 1786 | req.GetMimeType(&mime_type); |
| 1787 | EXPECT_EQ("text/html", mime_type); |
| 1788 | |
| 1789 | const std::string& data = d.data_received(); |
| 1790 | |
| 1791 | // Check that the post-specific headers were stripped: |
| 1792 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 1793 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 1794 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 1795 | |
| 1796 | // These extra request headers should not have been stripped. |
| 1797 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 1798 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 1799 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 1800 | } |
| 1801 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1802 | TEST_F(URLRequestTestHTTP, Post307RedirectPost) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1803 | ASSERT_TRUE(test_server_.Start()); |
| 1804 | |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1805 | const char kData[] = "hello world"; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1806 | |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1807 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1808 | TestURLRequest req(test_server_.GetURL("files/redirect307-to-echo"), |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1809 | &d); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1810 | req.set_method("POST"); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1811 | req.set_upload(CreateSimpleUploadData(kData).get()); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 1812 | net::HttpRequestHeaders headers; |
| 1813 | headers.SetHeader(net::HttpRequestHeaders::kContentLength, |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 1814 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 1815 | req.SetExtraRequestHeaders(headers); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1816 | req.Start(); |
| 1817 | MessageLoop::current()->Run(); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 1818 | EXPECT_EQ("POST", req.method()); |
| 1819 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1820 | } |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1821 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1822 | // Custom URLRequestJobs for use with interceptor tests |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1823 | class RestartTestJob : public net::URLRequestTestJob { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1824 | public: |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1825 | explicit RestartTestJob(net::URLRequest* request) |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1826 | : net::URLRequestTestJob(request, true) {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1827 | protected: |
| 1828 | virtual void StartAsync() { |
| 1829 | this->NotifyRestartRequired(); |
| 1830 | } |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 1831 | private: |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1832 | ~RestartTestJob() {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1833 | }; |
| 1834 | |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1835 | class CancelTestJob : public net::URLRequestTestJob { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1836 | public: |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1837 | explicit CancelTestJob(net::URLRequest* request) |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1838 | : net::URLRequestTestJob(request, true) {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1839 | protected: |
| 1840 | virtual void StartAsync() { |
| 1841 | request_->Cancel(); |
| 1842 | } |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 1843 | private: |
| 1844 | ~CancelTestJob() {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1845 | }; |
| 1846 | |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1847 | class CancelThenRestartTestJob : public net::URLRequestTestJob { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1848 | public: |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1849 | explicit CancelThenRestartTestJob(net::URLRequest* request) |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1850 | : net::URLRequestTestJob(request, true) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1851 | } |
| 1852 | protected: |
| 1853 | virtual void StartAsync() { |
| 1854 | request_->Cancel(); |
| 1855 | this->NotifyRestartRequired(); |
| 1856 | } |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 1857 | private: |
| 1858 | ~CancelThenRestartTestJob() {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1859 | }; |
| 1860 | |
| 1861 | // An Interceptor for use with interceptor tests |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1862 | class TestInterceptor : net::URLRequest::Interceptor { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1863 | public: |
| 1864 | TestInterceptor() |
| 1865 | : intercept_main_request_(false), restart_main_request_(false), |
| 1866 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1867 | simulate_main_network_error_(false), |
| 1868 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1869 | intercept_final_response_(false), cancel_final_request_(false), |
| 1870 | did_intercept_main_(false), did_restart_main_(false), |
| 1871 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1872 | did_simulate_error_main_(false), |
| 1873 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1874 | did_intercept_final_(false), did_cancel_final_(false) { |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1875 | net::URLRequest::RegisterRequestInterceptor(this); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | ~TestInterceptor() { |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 1879 | net::URLRequest::UnregisterRequestInterceptor(this); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1880 | } |
| 1881 | |
[email protected] | 9acd869e | 2010-12-11 10:18:59 | [diff] [blame] | 1882 | virtual net::URLRequestJob* MaybeIntercept(net::URLRequest* request) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1883 | if (restart_main_request_) { |
| 1884 | restart_main_request_ = false; |
| 1885 | did_restart_main_ = true; |
| 1886 | return new RestartTestJob(request); |
| 1887 | } |
| 1888 | if (cancel_main_request_) { |
| 1889 | cancel_main_request_ = false; |
| 1890 | did_cancel_main_ = true; |
| 1891 | return new CancelTestJob(request); |
| 1892 | } |
| 1893 | if (cancel_then_restart_main_request_) { |
| 1894 | cancel_then_restart_main_request_ = false; |
| 1895 | did_cancel_then_restart_main_ = true; |
| 1896 | return new CancelThenRestartTestJob(request); |
| 1897 | } |
| 1898 | if (simulate_main_network_error_) { |
| 1899 | simulate_main_network_error_ = false; |
| 1900 | did_simulate_error_main_ = true; |
| 1901 | // will error since the requeted url is not one of its canned urls |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1902 | return new net::URLRequestTestJob(request, true); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1903 | } |
| 1904 | if (!intercept_main_request_) |
| 1905 | return NULL; |
| 1906 | intercept_main_request_ = false; |
| 1907 | did_intercept_main_ = true; |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1908 | return new net::URLRequestTestJob(request, |
| 1909 | main_headers_, |
| 1910 | main_data_, |
| 1911 | true); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1912 | } |
| 1913 | |
[email protected] | 9acd869e | 2010-12-11 10:18:59 | [diff] [blame] | 1914 | virtual net::URLRequestJob* MaybeInterceptRedirect(net::URLRequest* request, |
| 1915 | const GURL& location) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1916 | if (cancel_redirect_request_) { |
| 1917 | cancel_redirect_request_ = false; |
| 1918 | did_cancel_redirect_ = true; |
| 1919 | return new CancelTestJob(request); |
| 1920 | } |
| 1921 | if (!intercept_redirect_) |
| 1922 | return NULL; |
| 1923 | intercept_redirect_ = false; |
| 1924 | did_intercept_redirect_ = true; |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1925 | return new net::URLRequestTestJob(request, |
| 1926 | redirect_headers_, |
| 1927 | redirect_data_, |
| 1928 | true); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1929 | } |
| 1930 | |
[email protected] | 9acd869e | 2010-12-11 10:18:59 | [diff] [blame] | 1931 | virtual net::URLRequestJob* MaybeInterceptResponse(net::URLRequest* request) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1932 | if (cancel_final_request_) { |
| 1933 | cancel_final_request_ = false; |
| 1934 | did_cancel_final_ = true; |
| 1935 | return new CancelTestJob(request); |
| 1936 | } |
| 1937 | if (!intercept_final_response_) |
| 1938 | return NULL; |
| 1939 | intercept_final_response_ = false; |
| 1940 | did_intercept_final_ = true; |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1941 | return new net::URLRequestTestJob(request, |
| 1942 | final_headers_, |
| 1943 | final_data_, |
| 1944 | true); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | // Whether to intercept the main request, and if so the response to return. |
| 1948 | bool intercept_main_request_; |
| 1949 | std::string main_headers_; |
| 1950 | std::string main_data_; |
| 1951 | |
| 1952 | // Other actions we take at MaybeIntercept time |
| 1953 | bool restart_main_request_; |
| 1954 | bool cancel_main_request_; |
| 1955 | bool cancel_then_restart_main_request_; |
| 1956 | bool simulate_main_network_error_; |
| 1957 | |
| 1958 | // Whether to intercept redirects, and if so the response to return. |
| 1959 | bool intercept_redirect_; |
| 1960 | std::string redirect_headers_; |
| 1961 | std::string redirect_data_; |
| 1962 | |
| 1963 | // Other actions we can take at MaybeInterceptRedirect time |
| 1964 | bool cancel_redirect_request_; |
| 1965 | |
| 1966 | // Whether to intercept final response, and if so the response to return. |
| 1967 | bool intercept_final_response_; |
| 1968 | std::string final_headers_; |
| 1969 | std::string final_data_; |
| 1970 | |
| 1971 | // Other actions we can take at MaybeInterceptResponse time |
| 1972 | bool cancel_final_request_; |
| 1973 | |
| 1974 | // If we did something or not |
| 1975 | bool did_intercept_main_; |
| 1976 | bool did_restart_main_; |
| 1977 | bool did_cancel_main_; |
| 1978 | bool did_cancel_then_restart_main_; |
| 1979 | bool did_simulate_error_main_; |
| 1980 | bool did_intercept_redirect_; |
| 1981 | bool did_cancel_redirect_; |
| 1982 | bool did_intercept_final_; |
| 1983 | bool did_cancel_final_; |
| 1984 | |
| 1985 | // Static getters for canned response header and data strings |
| 1986 | |
| 1987 | static std::string ok_data() { |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1988 | return net::URLRequestTestJob::test_data_1(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | static std::string ok_headers() { |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 1992 | return net::URLRequestTestJob::test_headers(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | static std::string redirect_data() { |
| 1996 | return std::string(); |
| 1997 | } |
| 1998 | |
| 1999 | static std::string redirect_headers() { |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 2000 | return net::URLRequestTestJob::test_redirect_headers(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | static std::string error_data() { |
| 2004 | return std::string("ohhh nooooo mr. bill!"); |
| 2005 | } |
| 2006 | |
| 2007 | static std::string error_headers() { |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 2008 | return net::URLRequestTestJob::test_error_headers(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 2009 | } |
| 2010 | }; |
| 2011 | |
| 2012 | TEST_F(URLRequestTest, Intercept) { |
| 2013 | TestInterceptor interceptor; |
| 2014 | |
| 2015 | // intercept the main request and respond with a simple response |
| 2016 | interceptor.intercept_main_request_ = true; |
| 2017 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 2018 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 2019 | |
| 2020 | TestDelegate d; |
| 2021 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 2022 | net::URLRequest::UserData* user_data0 = new net::URLRequest::UserData(); |
| 2023 | net::URLRequest::UserData* user_data1 = new net::URLRequest::UserData(); |
| 2024 | net::URLRequest::UserData* user_data2 = new net::URLRequest::UserData(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 2025 | req.SetUserData(NULL, user_data0); |
| 2026 | req.SetUserData(&user_data1, user_data1); |
| 2027 | req.SetUserData(&user_data2, user_data2); |
| 2028 | req.set_method("GET"); |
| 2029 | req.Start(); |
| 2030 | MessageLoop::current()->Run(); |
| 2031 | |
| 2032 | // Make sure we can retrieve our specific user data |
| 2033 | EXPECT_EQ(user_data0, req.GetUserData(NULL)); |
| 2034 | EXPECT_EQ(user_data1, req.GetUserData(&user_data1)); |
| 2035 | EXPECT_EQ(user_data2, req.GetUserData(&user_data2)); |
| 2036 | |
| 2037 | // Check the interceptor got called as expected |
| 2038 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 2039 | |
| 2040 | // Check we got one good response |
| 2041 | EXPECT_TRUE(req.status().is_success()); |
| 2042 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 2043 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 2044 | EXPECT_EQ(1, d.response_started_count()); |
| 2045 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2046 | } |
| 2047 | |
| 2048 | TEST_F(URLRequestTest, InterceptRedirect) { |
| 2049 | TestInterceptor interceptor; |
| 2050 | |
| 2051 | // intercept the main request and respond with a redirect |
| 2052 | interceptor.intercept_main_request_ = true; |
| 2053 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 2054 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 2055 | |
| 2056 | // intercept that redirect and respond a final OK response |
| 2057 | interceptor.intercept_redirect_ = true; |
| 2058 | interceptor.redirect_headers_ = TestInterceptor::ok_headers(); |
| 2059 | interceptor.redirect_data_ = TestInterceptor::ok_data(); |
| 2060 | |
| 2061 | TestDelegate d; |
| 2062 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 2063 | req.set_method("GET"); |
| 2064 | req.Start(); |
| 2065 | MessageLoop::current()->Run(); |
| 2066 | |
| 2067 | // Check the interceptor got called as expected |
| 2068 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 2069 | EXPECT_TRUE(interceptor.did_intercept_redirect_); |
| 2070 | |
| 2071 | // Check we got one good response |
| 2072 | EXPECT_TRUE(req.status().is_success()); |
[email protected] | 4463782 | 2009-08-27 17:01:11 | [diff] [blame] | 2073 | if (req.status().is_success()) { |
| 2074 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 2075 | } |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 2076 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 2077 | EXPECT_EQ(1, d.response_started_count()); |
| 2078 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2079 | } |
| 2080 | |
| 2081 | TEST_F(URLRequestTest, InterceptServerError) { |
| 2082 | TestInterceptor interceptor; |
| 2083 | |
| 2084 | // intercept the main request to generate a server error response |
| 2085 | interceptor.intercept_main_request_ = true; |
| 2086 | interceptor.main_headers_ = TestInterceptor::error_headers(); |
| 2087 | interceptor.main_data_ = TestInterceptor::error_data(); |
| 2088 | |
| 2089 | // intercept that error and respond with an OK response |
| 2090 | interceptor.intercept_final_response_ = true; |
| 2091 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 2092 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 2093 | |
| 2094 | TestDelegate d; |
| 2095 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 2096 | req.set_method("GET"); |
| 2097 | req.Start(); |
| 2098 | MessageLoop::current()->Run(); |
| 2099 | |
| 2100 | // Check the interceptor got called as expected |
| 2101 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 2102 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 2103 | |
| 2104 | // Check we got one good response |
| 2105 | EXPECT_TRUE(req.status().is_success()); |
| 2106 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 2107 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 2108 | EXPECT_EQ(1, d.response_started_count()); |
| 2109 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2110 | } |
| 2111 | |
| 2112 | TEST_F(URLRequestTest, InterceptNetworkError) { |
| 2113 | TestInterceptor interceptor; |
| 2114 | |
| 2115 | // intercept the main request to simulate a network error |
| 2116 | interceptor.simulate_main_network_error_ = true; |
| 2117 | |
| 2118 | // intercept that error and respond with an OK response |
| 2119 | interceptor.intercept_final_response_ = true; |
| 2120 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 2121 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 2122 | |
| 2123 | TestDelegate d; |
| 2124 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 2125 | req.set_method("GET"); |
| 2126 | req.Start(); |
| 2127 | MessageLoop::current()->Run(); |
| 2128 | |
| 2129 | // Check the interceptor got called as expected |
| 2130 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 2131 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 2132 | |
| 2133 | // Check we received one good response |
| 2134 | EXPECT_TRUE(req.status().is_success()); |
| 2135 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 2136 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 2137 | EXPECT_EQ(1, d.response_started_count()); |
| 2138 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2139 | } |
| 2140 | |
| 2141 | TEST_F(URLRequestTest, InterceptRestartRequired) { |
| 2142 | TestInterceptor interceptor; |
| 2143 | |
| 2144 | // restart the main request |
| 2145 | interceptor.restart_main_request_ = true; |
| 2146 | |
| 2147 | // then intercept the new main request and respond with an OK response |
| 2148 | interceptor.intercept_main_request_ = true; |
| 2149 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 2150 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 2151 | |
| 2152 | TestDelegate d; |
| 2153 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 2154 | req.set_method("GET"); |
| 2155 | req.Start(); |
| 2156 | MessageLoop::current()->Run(); |
| 2157 | |
| 2158 | // Check the interceptor got called as expected |
| 2159 | EXPECT_TRUE(interceptor.did_restart_main_); |
| 2160 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 2161 | |
| 2162 | // Check we received one good response |
| 2163 | EXPECT_TRUE(req.status().is_success()); |
[email protected] | 4463782 | 2009-08-27 17:01:11 | [diff] [blame] | 2164 | if (req.status().is_success()) { |
| 2165 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 2166 | } |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 2167 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 2168 | EXPECT_EQ(1, d.response_started_count()); |
| 2169 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2170 | } |
| 2171 | |
| 2172 | TEST_F(URLRequestTest, InterceptRespectsCancelMain) { |
| 2173 | TestInterceptor interceptor; |
| 2174 | |
| 2175 | // intercept the main request and cancel from within the restarted job |
| 2176 | interceptor.cancel_main_request_ = true; |
| 2177 | |
| 2178 | // setup to intercept final response and override it with an OK response |
| 2179 | interceptor.intercept_final_response_ = true; |
| 2180 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 2181 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 2182 | |
| 2183 | TestDelegate d; |
| 2184 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 2185 | req.set_method("GET"); |
| 2186 | req.Start(); |
| 2187 | MessageLoop::current()->Run(); |
| 2188 | |
| 2189 | // Check the interceptor got called as expected |
| 2190 | EXPECT_TRUE(interceptor.did_cancel_main_); |
| 2191 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 2192 | |
| 2193 | // Check we see a canceled request |
| 2194 | EXPECT_FALSE(req.status().is_success()); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 2195 | EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) { |
| 2199 | TestInterceptor interceptor; |
| 2200 | |
| 2201 | // intercept the main request and respond with a redirect |
| 2202 | interceptor.intercept_main_request_ = true; |
| 2203 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 2204 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 2205 | |
| 2206 | // intercept the redirect and cancel from within that job |
| 2207 | interceptor.cancel_redirect_request_ = true; |
| 2208 | |
| 2209 | // setup to intercept final response and override it with an OK response |
| 2210 | interceptor.intercept_final_response_ = true; |
| 2211 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 2212 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 2213 | |
| 2214 | TestDelegate d; |
| 2215 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 2216 | req.set_method("GET"); |
| 2217 | req.Start(); |
| 2218 | MessageLoop::current()->Run(); |
| 2219 | |
| 2220 | // Check the interceptor got called as expected |
| 2221 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 2222 | EXPECT_TRUE(interceptor.did_cancel_redirect_); |
| 2223 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 2224 | |
| 2225 | // Check we see a canceled request |
| 2226 | EXPECT_FALSE(req.status().is_success()); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 2227 | EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | TEST_F(URLRequestTest, InterceptRespectsCancelFinal) { |
| 2231 | TestInterceptor interceptor; |
| 2232 | |
| 2233 | // intercept the main request to simulate a network error |
| 2234 | interceptor.simulate_main_network_error_ = true; |
| 2235 | |
| 2236 | // setup to intercept final response and cancel from within that job |
| 2237 | interceptor.cancel_final_request_ = true; |
| 2238 | |
| 2239 | TestDelegate d; |
| 2240 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 2241 | req.set_method("GET"); |
| 2242 | req.Start(); |
| 2243 | MessageLoop::current()->Run(); |
| 2244 | |
| 2245 | // Check the interceptor got called as expected |
| 2246 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 2247 | EXPECT_TRUE(interceptor.did_cancel_final_); |
| 2248 | |
| 2249 | // Check we see a canceled request |
| 2250 | EXPECT_FALSE(req.status().is_success()); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 2251 | EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) { |
| 2255 | TestInterceptor interceptor; |
| 2256 | |
| 2257 | // intercept the main request and cancel then restart from within that job |
| 2258 | interceptor.cancel_then_restart_main_request_ = true; |
| 2259 | |
| 2260 | // setup to intercept final response and override it with an OK response |
| 2261 | interceptor.intercept_final_response_ = true; |
| 2262 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 2263 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 2264 | |
| 2265 | TestDelegate d; |
| 2266 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
| 2267 | req.set_method("GET"); |
| 2268 | req.Start(); |
| 2269 | MessageLoop::current()->Run(); |
| 2270 | |
| 2271 | // Check the interceptor got called as expected |
| 2272 | EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); |
| 2273 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 2274 | |
| 2275 | // Check we see a canceled request |
| 2276 | EXPECT_FALSE(req.status().is_success()); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 2277 | EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 2278 | } |
| 2279 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 2280 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2281 | public: |
| 2282 | URLRequestTestFTP() : test_server_(net::TestServer::TYPE_FTP, FilePath()) { |
| 2283 | } |
| 2284 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 2285 | protected: |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2286 | net::TestServer test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 2287 | }; |
| 2288 | |
[email protected] | 5accf733 | 2009-11-24 03:41:38 | [diff] [blame] | 2289 | // Flaky, see http://crbug.com/25045. |
| 2290 | TEST_F(URLRequestTestFTP, FLAKY_FTPDirectoryListing) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2291 | ASSERT_TRUE(test_server_.Start()); |
| 2292 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 2293 | TestDelegate d; |
| 2294 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2295 | TestURLRequest r(test_server_.GetURL("/"), &d); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 2296 | r.Start(); |
| 2297 | EXPECT_TRUE(r.is_pending()); |
| 2298 | |
| 2299 | MessageLoop::current()->Run(); |
| 2300 | |
| 2301 | EXPECT_FALSE(r.is_pending()); |
| 2302 | EXPECT_EQ(1, d.response_started_count()); |
| 2303 | EXPECT_FALSE(d.received_data_before_response()); |
| 2304 | EXPECT_LT(0, d.bytes_received()); |
| 2305 | } |
| 2306 | } |
| 2307 | |
[email protected] | 7df7001 | 2010-02-04 00:09:55 | [diff] [blame] | 2308 | // Flaky, see http://crbug.com/25045. |
| 2309 | TEST_F(URLRequestTestFTP, FLAKY_FTPGetTestAnonymous) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2310 | ASSERT_TRUE(test_server_.Start()); |
| 2311 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2312 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2313 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2314 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2315 | TestDelegate d; |
| 2316 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2317 | TestURLRequest r(test_server_.GetURL("/LICENSE"), &d); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2318 | r.Start(); |
| 2319 | EXPECT_TRUE(r.is_pending()); |
| 2320 | |
| 2321 | MessageLoop::current()->Run(); |
| 2322 | |
| 2323 | int64 file_size = 0; |
| 2324 | file_util::GetFileSize(app_path, &file_size); |
| 2325 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 2326 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2327 | EXPECT_EQ(1, d.response_started_count()); |
| 2328 | EXPECT_FALSE(d.received_data_before_response()); |
| 2329 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 2330 | } |
| 2331 | } |
| 2332 | |
[email protected] | e9ecbd1 | 2009-12-20 18:44:40 | [diff] [blame] | 2333 | // Flaky, see http://crbug.com/25045. |
| 2334 | TEST_F(URLRequestTestFTP, FLAKY_FTPGetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2335 | ASSERT_TRUE(test_server_.Start()); |
| 2336 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2337 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2338 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2339 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2340 | TestDelegate d; |
| 2341 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2342 | TestURLRequest r( |
| 2343 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
| 2344 | &d); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2345 | r.Start(); |
| 2346 | EXPECT_TRUE(r.is_pending()); |
| 2347 | |
| 2348 | MessageLoop::current()->Run(); |
| 2349 | |
| 2350 | int64 file_size = 0; |
| 2351 | file_util::GetFileSize(app_path, &file_size); |
| 2352 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 2353 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2354 | EXPECT_EQ(1, d.response_started_count()); |
| 2355 | EXPECT_FALSE(d.received_data_before_response()); |
| 2356 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 2357 | } |
| 2358 | } |
| 2359 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 2360 | // Flaky, see http://crbug.com/25045. |
| 2361 | TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPassword) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2362 | ASSERT_TRUE(test_server_.Start()); |
| 2363 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2364 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2365 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2366 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2367 | TestDelegate d; |
| 2368 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2369 | TestURLRequest r( |
| 2370 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 2371 | "chrome", |
| 2372 | "wrong_password"), |
| 2373 | &d); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2374 | r.Start(); |
| 2375 | EXPECT_TRUE(r.is_pending()); |
| 2376 | |
| 2377 | MessageLoop::current()->Run(); |
| 2378 | |
| 2379 | int64 file_size = 0; |
| 2380 | file_util::GetFileSize(app_path, &file_size); |
| 2381 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 2382 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2383 | EXPECT_EQ(1, d.response_started_count()); |
| 2384 | EXPECT_FALSE(d.received_data_before_response()); |
| 2385 | EXPECT_EQ(d.bytes_received(), 0); |
| 2386 | } |
| 2387 | } |
| 2388 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 2389 | // Flaky, see http://crbug.com/25045. |
| 2390 | TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPasswordRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2391 | ASSERT_TRUE(test_server_.Start()); |
| 2392 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2393 | FilePath app_path; |
| 2394 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2395 | app_path = app_path.AppendASCII("LICENSE"); |
| 2396 | TestDelegate d; |
| 2397 | // Set correct login credentials. The delegate will be asked for them when |
| 2398 | // the initial login with wrong credentials will fail. |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2399 | d.set_username(kChrome); |
| 2400 | d.set_password(kChrome); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2401 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2402 | TestURLRequest r( |
| 2403 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 2404 | "chrome", |
| 2405 | "wrong_password"), |
| 2406 | &d); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2407 | r.Start(); |
| 2408 | EXPECT_TRUE(r.is_pending()); |
| 2409 | |
| 2410 | MessageLoop::current()->Run(); |
| 2411 | |
| 2412 | int64 file_size = 0; |
| 2413 | file_util::GetFileSize(app_path, &file_size); |
| 2414 | |
| 2415 | EXPECT_FALSE(r.is_pending()); |
| 2416 | EXPECT_EQ(1, d.response_started_count()); |
| 2417 | EXPECT_FALSE(d.received_data_before_response()); |
| 2418 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 2419 | } |
| 2420 | } |
| 2421 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 2422 | // Flaky, see http://crbug.com/25045. |
| 2423 | TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUser) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2424 | ASSERT_TRUE(test_server_.Start()); |
| 2425 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2426 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2427 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2428 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2429 | TestDelegate d; |
| 2430 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2431 | TestURLRequest r( |
| 2432 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 2433 | "wrong_user", |
| 2434 | "chrome"), |
| 2435 | &d); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2436 | r.Start(); |
| 2437 | EXPECT_TRUE(r.is_pending()); |
| 2438 | |
| 2439 | MessageLoop::current()->Run(); |
| 2440 | |
| 2441 | int64 file_size = 0; |
| 2442 | file_util::GetFileSize(app_path, &file_size); |
| 2443 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 2444 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2445 | EXPECT_EQ(1, d.response_started_count()); |
| 2446 | EXPECT_FALSE(d.received_data_before_response()); |
| 2447 | EXPECT_EQ(d.bytes_received(), 0); |
| 2448 | } |
| 2449 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2450 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 2451 | // Flaky, see http://crbug.com/25045. |
| 2452 | TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUserRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2453 | ASSERT_TRUE(test_server_.Start()); |
| 2454 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2455 | FilePath app_path; |
| 2456 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2457 | app_path = app_path.AppendASCII("LICENSE"); |
| 2458 | TestDelegate d; |
| 2459 | // Set correct login credentials. The delegate will be asked for them when |
| 2460 | // the initial login with wrong credentials will fail. |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2461 | d.set_username(kChrome); |
| 2462 | d.set_password(kChrome); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2463 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2464 | TestURLRequest r( |
| 2465 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 2466 | "wrong_user", |
| 2467 | "chrome"), |
| 2468 | &d); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 2469 | r.Start(); |
| 2470 | EXPECT_TRUE(r.is_pending()); |
| 2471 | |
| 2472 | MessageLoop::current()->Run(); |
| 2473 | |
| 2474 | int64 file_size = 0; |
| 2475 | file_util::GetFileSize(app_path, &file_size); |
| 2476 | |
| 2477 | EXPECT_FALSE(r.is_pending()); |
| 2478 | EXPECT_EQ(1, d.response_started_count()); |
| 2479 | EXPECT_FALSE(d.received_data_before_response()); |
| 2480 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 2481 | } |
| 2482 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2483 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 2484 | // Flaky, see http://crbug.com/25045. |
| 2485 | TEST_F(URLRequestTestFTP, FLAKY_FTPCacheURLCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2486 | ASSERT_TRUE(test_server_.Start()); |
| 2487 | |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2488 | FilePath app_path; |
| 2489 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2490 | app_path = app_path.AppendASCII("LICENSE"); |
| 2491 | |
| 2492 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 2493 | { |
| 2494 | // Pass correct login identity in the URL. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2495 | TestURLRequest r( |
| 2496 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 2497 | "chrome", |
| 2498 | "chrome"), |
| 2499 | d.get()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2500 | r.Start(); |
| 2501 | EXPECT_TRUE(r.is_pending()); |
| 2502 | |
| 2503 | MessageLoop::current()->Run(); |
| 2504 | |
| 2505 | int64 file_size = 0; |
| 2506 | file_util::GetFileSize(app_path, &file_size); |
| 2507 | |
| 2508 | EXPECT_FALSE(r.is_pending()); |
| 2509 | EXPECT_EQ(1, d->response_started_count()); |
| 2510 | EXPECT_FALSE(d->received_data_before_response()); |
| 2511 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 2512 | } |
| 2513 | |
| 2514 | d.reset(new TestDelegate); |
| 2515 | { |
| 2516 | // This request should use cached identity from previous request. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2517 | TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2518 | r.Start(); |
| 2519 | EXPECT_TRUE(r.is_pending()); |
| 2520 | |
| 2521 | MessageLoop::current()->Run(); |
| 2522 | |
| 2523 | int64 file_size = 0; |
| 2524 | file_util::GetFileSize(app_path, &file_size); |
| 2525 | |
| 2526 | EXPECT_FALSE(r.is_pending()); |
| 2527 | EXPECT_EQ(1, d->response_started_count()); |
| 2528 | EXPECT_FALSE(d->received_data_before_response()); |
| 2529 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 2530 | } |
| 2531 | } |
| 2532 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 2533 | // Flaky, see http://crbug.com/25045. |
| 2534 | TEST_F(URLRequestTestFTP, FLAKY_FTPCacheLoginBoxCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2535 | ASSERT_TRUE(test_server_.Start()); |
| 2536 | |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2537 | FilePath app_path; |
| 2538 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 2539 | app_path = app_path.AppendASCII("LICENSE"); |
| 2540 | |
| 2541 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 2542 | // Set correct login credentials. The delegate will be asked for them when |
| 2543 | // the initial login with wrong credentials will fail. |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2544 | d->set_username(kChrome); |
| 2545 | d->set_password(kChrome); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2546 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2547 | TestURLRequest r( |
| 2548 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 2549 | "chrome", |
| 2550 | "wrong_password"), |
| 2551 | d.get()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2552 | r.Start(); |
| 2553 | EXPECT_TRUE(r.is_pending()); |
| 2554 | |
| 2555 | MessageLoop::current()->Run(); |
| 2556 | |
| 2557 | int64 file_size = 0; |
| 2558 | file_util::GetFileSize(app_path, &file_size); |
| 2559 | |
| 2560 | EXPECT_FALSE(r.is_pending()); |
| 2561 | EXPECT_EQ(1, d->response_started_count()); |
| 2562 | EXPECT_FALSE(d->received_data_before_response()); |
| 2563 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 2564 | } |
| 2565 | |
| 2566 | // Use a new delegate without explicit credentials. The cached ones should be |
| 2567 | // used. |
| 2568 | d.reset(new TestDelegate); |
| 2569 | { |
| 2570 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 2571 | // ones. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2572 | TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 2573 | r.Start(); |
| 2574 | EXPECT_TRUE(r.is_pending()); |
| 2575 | |
| 2576 | MessageLoop::current()->Run(); |
| 2577 | |
| 2578 | int64 file_size = 0; |
| 2579 | file_util::GetFileSize(app_path, &file_size); |
| 2580 | |
| 2581 | EXPECT_FALSE(r.is_pending()); |
| 2582 | EXPECT_EQ(1, d->response_started_count()); |
| 2583 | EXPECT_FALSE(d->received_data_before_response()); |
| 2584 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 2585 | } |
| 2586 | } |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2587 | |
| 2588 | // Check that default A-L header is sent. |
| 2589 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2590 | ASSERT_TRUE(test_server_.Start()); |
| 2591 | |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2592 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2593 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d); |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 2594 | req.set_context(new TestURLRequestContext()); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2595 | req.Start(); |
| 2596 | MessageLoop::current()->Run(); |
| 2597 | EXPECT_EQ(req.context()->accept_language(), d.data_received()); |
| 2598 | } |
| 2599 | |
| 2600 | // Check that if request overrides the A-L header, the default is not appended. |
| 2601 | // See http://crbug.com/20894 |
| 2602 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2603 | ASSERT_TRUE(test_server_.Start()); |
| 2604 | |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2605 | TestDelegate d; |
[email protected] | eb834fb | 2009-10-23 16:09:41 | [diff] [blame] | 2606 | TestURLRequest |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2607 | req(test_server_.GetURL("echoheaderoverride?Accept-Language"), &d); |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 2608 | req.set_context(new TestURLRequestContext()); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 2609 | net::HttpRequestHeaders headers; |
| 2610 | headers.SetHeader(net::HttpRequestHeaders::kAcceptLanguage, "ru"); |
| 2611 | req.SetExtraRequestHeaders(headers); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2612 | req.Start(); |
| 2613 | MessageLoop::current()->Run(); |
| 2614 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 2615 | } |
| 2616 | |
| 2617 | // Check that default A-C header is sent. |
| 2618 | TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2619 | ASSERT_TRUE(test_server_.Start()); |
| 2620 | |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2621 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2622 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d); |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 2623 | req.set_context(new TestURLRequestContext()); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2624 | req.Start(); |
| 2625 | MessageLoop::current()->Run(); |
| 2626 | EXPECT_EQ(req.context()->accept_charset(), d.data_received()); |
| 2627 | } |
| 2628 | |
| 2629 | // Check that if request overrides the A-C header, the default is not appended. |
| 2630 | // See http://crbug.com/20894 |
| 2631 | TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2632 | ASSERT_TRUE(test_server_.Start()); |
| 2633 | |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2634 | TestDelegate d; |
[email protected] | eb834fb | 2009-10-23 16:09:41 | [diff] [blame] | 2635 | TestURLRequest |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2636 | req(test_server_.GetURL("echoheaderoverride?Accept-Charset"), &d); |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 2637 | req.set_context(new TestURLRequestContext()); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 2638 | net::HttpRequestHeaders headers; |
| 2639 | headers.SetHeader(net::HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
| 2640 | req.SetExtraRequestHeaders(headers); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 2641 | req.Start(); |
| 2642 | MessageLoop::current()->Run(); |
| 2643 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 2644 | } |
[email protected] | 50ba404b | 2011-01-21 13:38:45 | [diff] [blame^] | 2645 | |
| 2646 | // Check that default User-Agent header is sent. |
| 2647 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
| 2648 | ASSERT_TRUE(test_server_.Start()); |
| 2649 | |
| 2650 | TestDelegate d; |
| 2651 | TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d); |
| 2652 | req.set_context(new TestURLRequestContext()); |
| 2653 | req.Start(); |
| 2654 | MessageLoop::current()->Run(); |
| 2655 | EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received()); |
| 2656 | } |
| 2657 | |
| 2658 | // Check that if request overrides the User-Agent header, |
| 2659 | // the default is not appended. |
| 2660 | TEST_F(URLRequestTestHTTP, OverrideUserAgent) { |
| 2661 | ASSERT_TRUE(test_server_.Start()); |
| 2662 | |
| 2663 | TestDelegate d; |
| 2664 | TestURLRequest |
| 2665 | req(test_server_.GetURL("echoheaderoverride?User-Agent"), &d); |
| 2666 | req.set_context(new TestURLRequestContext()); |
| 2667 | net::HttpRequestHeaders headers; |
| 2668 | headers.SetHeader(net::HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
| 2669 | req.SetExtraRequestHeaders(headers); |
| 2670 | req.Start(); |
| 2671 | MessageLoop::current()->Run(); |
| 2672 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
| 2673 | } |