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