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