license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 5 | #include "net/url_request/url_request_unittest.h" |
| 6 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7 | #include "build/build_config.h" |
| 8 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 9 | #if defined(OS_WIN) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 10 | #include <windows.h> |
| 11 | #include <shlobj.h> |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 12 | #elif defined(OS_LINUX) |
| 13 | #include "base/nss_init.h" |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 14 | #endif |
| 15 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <string> |
| 18 | |
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] | 73f5d66 | 2008-11-20 01:08:17 | [diff] [blame] | 22 | #include "base/string_piece.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | #include "base/string_util.h" |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 24 | #include "net/base/cookie_monster.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 25 | #include "net/base/load_flags.h" |
| 26 | #include "net/base/net_errors.h" |
| 27 | #include "net/base/net_module.h" |
| 28 | #include "net/base/net_util.h" |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 29 | #include "net/base/ssl_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 30 | #include "net/disk_cache/disk_cache.h" |
| 31 | #include "net/http/http_cache.h" |
| 32 | #include "net/http/http_network_layer.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 33 | #include "net/http/http_response_headers.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 34 | #include "net/proxy/proxy_service.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | #include "net/url_request/url_request.h" |
| 36 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 37 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 39 | using base::Time; |
| 40 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 41 | namespace { |
| 42 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | class URLRequestHttpCacheContext : public URLRequestContext { |
| 44 | public: |
| 45 | URLRequestHttpCacheContext() { |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 46 | proxy_service_ = net::ProxyService::CreateNull(); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 47 | http_transaction_factory_ = |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 48 | new net::HttpCache(net::HttpNetworkLayer::CreateFactory(proxy_service_), |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 49 | disk_cache::CreateInMemoryCacheBackend(0)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 50 | // In-memory cookie store. |
| 51 | cookie_store_ = new net::CookieMonster(); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | virtual ~URLRequestHttpCacheContext() { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 55 | delete cookie_store_; |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 56 | delete http_transaction_factory_; |
| 57 | delete proxy_service_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
| 61 | class TestURLRequest : public URLRequest { |
| 62 | public: |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 63 | TestURLRequest(const GURL& url, Delegate* delegate) |
| 64 | : URLRequest(url, delegate) { |
| 65 | set_context(new URLRequestHttpCacheContext()); |
| 66 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 67 | }; |
| 68 | |
[email protected] | 73f5d66 | 2008-11-20 01:08:17 | [diff] [blame] | 69 | StringPiece TestNetResourceProvider(int key) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 70 | return "header"; |
| 71 | } |
| 72 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 73 | // Do a case-insensitive search through |haystack| for |needle|. |
| 74 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 75 | std::string::const_iterator it = |
| 76 | std::search(haystack.begin(), |
| 77 | haystack.end(), |
| 78 | needle, |
| 79 | needle + strlen(needle), |
| 80 | CaseInsensitiveCompare<char>()); |
| 81 | return it != haystack.end(); |
| 82 | } |
| 83 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 84 | } // namespace |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 85 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 86 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |
| 87 | class URLRequestTest : public PlatformTest { |
| 88 | }; |
| 89 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 90 | TEST_F(URLRequestTest, ProxyTunnelRedirectTest) { |
| 91 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 92 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 93 | // The HTTPTestServer will return a 302 response, which we should not |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 94 | // follow. |
| 95 | scoped_refptr<HTTPTestServer> server = |
| 96 | HTTPTestServer::CreateServer(L"", NULL); |
| 97 | ASSERT_TRUE(NULL != server.get()); |
| 98 | TestDelegate d; |
| 99 | { |
| 100 | URLRequest r(GURL("https://www.redirect.com/"), &d); |
| 101 | std::string proxy("localhost:"); |
| 102 | proxy.append(IntToString(kHTTPDefaultPort)); |
| 103 | r.set_context(new TestURLRequestContext(proxy)); |
| 104 | |
| 105 | r.Start(); |
| 106 | EXPECT_TRUE(r.is_pending()); |
| 107 | |
| 108 | MessageLoop::current()->Run(); |
| 109 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 110 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
| 111 | EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 112 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 113 | // We should not have followed the redirect. |
| 114 | EXPECT_EQ(0, d.received_redirect_count()); |
| 115 | } |
| 116 | } |
| 117 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 118 | TEST_F(URLRequestTest, UnexpectedServerAuthTest) { |
| 119 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 120 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 121 | // The HTTPTestServer will return a 401 response, which we should balk at. |
| 122 | scoped_refptr<HTTPTestServer> server = |
| 123 | HTTPTestServer::CreateServer(L"", NULL); |
| 124 | ASSERT_TRUE(NULL != server.get()); |
| 125 | TestDelegate d; |
| 126 | { |
| 127 | URLRequest r(GURL("https://www.server-auth.com/"), &d); |
| 128 | std::string proxy("localhost:"); |
| 129 | proxy.append(IntToString(kHTTPDefaultPort)); |
| 130 | r.set_context(new TestURLRequestContext(proxy)); |
| 131 | |
| 132 | r.Start(); |
| 133 | EXPECT_TRUE(r.is_pending()); |
| 134 | |
| 135 | MessageLoop::current()->Run(); |
| 136 | |
| 137 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 138 | EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 142 | TEST_F(URLRequestTest, GetTest_NoCache) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 143 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 144 | HTTPTestServer::CreateServer(L"", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 145 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 146 | TestDelegate d; |
| 147 | { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 148 | TestURLRequest r(server->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 149 | |
| 150 | r.Start(); |
| 151 | EXPECT_TRUE(r.is_pending()); |
| 152 | |
| 153 | MessageLoop::current()->Run(); |
| 154 | |
| 155 | EXPECT_EQ(1, d.response_started_count()); |
| 156 | EXPECT_FALSE(d.received_data_before_response()); |
| 157 | EXPECT_NE(0, d.bytes_received()); |
| 158 | } |
| 159 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 160 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 161 | #endif |
| 162 | } |
| 163 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 164 | TEST_F(URLRequestTest, GetTest) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 165 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 166 | HTTPTestServer::CreateServer(L"", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 167 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 168 | TestDelegate d; |
| 169 | { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 170 | TestURLRequest r(server->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 171 | |
| 172 | r.Start(); |
| 173 | EXPECT_TRUE(r.is_pending()); |
| 174 | |
| 175 | MessageLoop::current()->Run(); |
| 176 | |
| 177 | EXPECT_EQ(1, d.response_started_count()); |
| 178 | EXPECT_FALSE(d.received_data_before_response()); |
| 179 | EXPECT_NE(0, d.bytes_received()); |
| 180 | } |
| 181 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 182 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 183 | #endif |
| 184 | } |
| 185 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 186 | TEST_F(URLRequestTest, QuitTest) { |
| 187 | scoped_refptr<HTTPTestServer> server = |
| 188 | HTTPTestServer::CreateServer(L"", NULL); |
| 189 | ASSERT_TRUE(NULL != server.get()); |
| 190 | server->SendQuit(); |
| 191 | EXPECT_TRUE(server->WaitToFinish(20000)); |
[email protected] | ac4335c | 2009-02-19 00:48:06 | [diff] [blame] | 192 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 193 | #ifndef NDEBUG |
| 194 | DCHECK_EQ(url_request_metrics.object_count, 0); |
| 195 | #endif |
| 196 | } |
| 197 | |
| 198 | class HTTPSRequestTest : public testing::Test { |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | #if defined(OS_MACOSX) |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 202 | // ssl_client_socket_mac.cc crashes currently in GetSSLInfo |
| 203 | // when called on a connection with an unrecognized certificate |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame^] | 204 | #define MAYBE_HTTPSGetTest DISABLED_HTTPSGetTest |
| 205 | #define MAYBE_HTTPSMismatchedTest DISABLED_HTTPSMismatchedTest |
| 206 | #define MAYBE_HTTPSExpiredTest DISABLED_HTTPSExpiredTest |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 207 | #else |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame^] | 208 | #define MAYBE_HTTPSGetTest HTTPSGetTest |
| 209 | #define MAYBE_HTTPSMismatchedTest HTTPSMismatchedTest |
| 210 | #define MAYBE_HTTPSExpiredTest HTTPSExpiredTest |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 211 | #endif |
| 212 | |
| 213 | TEST_F(HTTPSRequestTest, MAYBE_HTTPSGetTest) { |
| 214 | // Note: tools/testserver/testserver.py does not need |
| 215 | // a working document root to server the pages / and /hello.html, |
| 216 | // so this test doesn't really need to specify a document root. |
| 217 | // But if it did, a good one would be net/data/ssl. |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 218 | scoped_refptr<HTTPSTestServer> server = |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 219 | HTTPSTestServer::CreateGoodServer(L"net/data/ssl"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 220 | ASSERT_TRUE(NULL != server.get()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 221 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 222 | TestDelegate d; |
| 223 | { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 224 | TestURLRequest r(server->TestServerPage(""), &d); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 225 | |
| 226 | r.Start(); |
| 227 | EXPECT_TRUE(r.is_pending()); |
| 228 | |
| 229 | MessageLoop::current()->Run(); |
| 230 | |
| 231 | EXPECT_EQ(1, d.response_started_count()); |
| 232 | EXPECT_FALSE(d.received_data_before_response()); |
| 233 | EXPECT_NE(0, d.bytes_received()); |
| 234 | } |
| 235 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 236 | DCHECK_EQ(url_request_metrics.object_count, 0); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 237 | #endif |
| 238 | } |
| 239 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame^] | 240 | TEST_F(HTTPSRequestTest, MAYBE_HTTPSMismatchedTest) { |
| 241 | scoped_refptr<HTTPSTestServer> server = |
| 242 | HTTPSTestServer::CreateMismatchedServer(L"net/data/ssl"); |
| 243 | ASSERT_TRUE(NULL != server.get()); |
| 244 | |
| 245 | bool err_allowed = true; |
| 246 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 247 | TestDelegate d; |
| 248 | { |
| 249 | d.set_allow_certificate_errors(err_allowed); |
| 250 | TestURLRequest r(server->TestServerPage(""), &d); |
| 251 | |
| 252 | r.Start(); |
| 253 | EXPECT_TRUE(r.is_pending()); |
| 254 | |
| 255 | MessageLoop::current()->Run(); |
| 256 | |
| 257 | EXPECT_EQ(1, d.response_started_count()); |
| 258 | EXPECT_FALSE(d.received_data_before_response()); |
| 259 | EXPECT_TRUE(d.have_certificate_errors()); |
| 260 | if (err_allowed) |
| 261 | EXPECT_NE(0, d.bytes_received()); |
| 262 | else |
| 263 | EXPECT_EQ(0, d.bytes_received()); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | TEST_F(HTTPSRequestTest, MAYBE_HTTPSExpiredTest) { |
| 269 | scoped_refptr<HTTPSTestServer> server = |
| 270 | HTTPSTestServer::CreateExpiredServer(L"net/data/ssl"); |
| 271 | ASSERT_TRUE(NULL != server.get()); |
| 272 | |
| 273 | // Iterate from false to true, just so that we do the opposite of the |
| 274 | // previous test in order to increase test coverage. |
| 275 | bool err_allowed = false; |
| 276 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 277 | TestDelegate d; |
| 278 | { |
| 279 | d.set_allow_certificate_errors(err_allowed); |
| 280 | TestURLRequest r(server->TestServerPage(""), &d); |
| 281 | |
| 282 | r.Start(); |
| 283 | EXPECT_TRUE(r.is_pending()); |
| 284 | |
| 285 | MessageLoop::current()->Run(); |
| 286 | |
| 287 | EXPECT_EQ(1, d.response_started_count()); |
| 288 | EXPECT_FALSE(d.received_data_before_response()); |
| 289 | EXPECT_TRUE(d.have_certificate_errors()); |
| 290 | if (err_allowed) |
| 291 | EXPECT_NE(0, d.bytes_received()); |
| 292 | else |
| 293 | EXPECT_EQ(0, d.bytes_received()); |
| 294 | } |
| 295 | } |
| 296 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 297 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 298 | TEST_F(URLRequestTest, CancelTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 299 | TestDelegate d; |
| 300 | { |
| 301 | TestURLRequest r(GURL("http://www.google.com/"), &d); |
| 302 | |
| 303 | r.Start(); |
| 304 | EXPECT_TRUE(r.is_pending()); |
| 305 | |
| 306 | r.Cancel(); |
| 307 | |
| 308 | MessageLoop::current()->Run(); |
| 309 | |
| 310 | // We expect to receive OnResponseStarted even though the request has been |
| 311 | // cancelled. |
| 312 | EXPECT_EQ(1, d.response_started_count()); |
| 313 | EXPECT_EQ(0, d.bytes_received()); |
| 314 | EXPECT_FALSE(d.received_data_before_response()); |
| 315 | } |
| 316 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 317 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 318 | #endif |
| 319 | } |
| 320 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 321 | TEST_F(URLRequestTest, CancelTest2) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 322 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 323 | HTTPTestServer::CreateServer(L"", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 324 | ASSERT_TRUE(NULL != server.get()); |
| 325 | |
| 326 | // error C2446: '!=' : no conversion from 'HTTPTestServer *const ' |
| 327 | // to 'const int' |
| 328 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 329 | TestDelegate d; |
| 330 | { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 331 | TestURLRequest r(server->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 332 | |
| 333 | d.set_cancel_in_response_started(true); |
| 334 | |
| 335 | r.Start(); |
| 336 | EXPECT_TRUE(r.is_pending()); |
| 337 | |
| 338 | MessageLoop::current()->Run(); |
| 339 | |
| 340 | EXPECT_EQ(1, d.response_started_count()); |
| 341 | EXPECT_EQ(0, d.bytes_received()); |
| 342 | EXPECT_FALSE(d.received_data_before_response()); |
| 343 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 344 | } |
| 345 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 346 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 347 | #endif |
| 348 | } |
| 349 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 350 | TEST_F(URLRequestTest, CancelTest3) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 351 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 352 | HTTPTestServer::CreateServer(L"", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 353 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 354 | TestDelegate d; |
| 355 | { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 356 | TestURLRequest r(server->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 357 | |
| 358 | d.set_cancel_in_received_data(true); |
| 359 | |
| 360 | r.Start(); |
| 361 | EXPECT_TRUE(r.is_pending()); |
| 362 | |
| 363 | MessageLoop::current()->Run(); |
| 364 | |
| 365 | EXPECT_EQ(1, d.response_started_count()); |
| 366 | // There is no guarantee about how much data was received |
| 367 | // before the cancel was issued. It could have been 0 bytes, |
| 368 | // or it could have been all the bytes. |
| 369 | // EXPECT_EQ(0, d.bytes_received()); |
| 370 | EXPECT_FALSE(d.received_data_before_response()); |
| 371 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 372 | } |
| 373 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 374 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 375 | #endif |
| 376 | } |
| 377 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 378 | TEST_F(URLRequestTest, CancelTest4) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 379 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 380 | HTTPTestServer::CreateServer(L"", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 381 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 382 | TestDelegate d; |
| 383 | { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 384 | TestURLRequest r(server->TestServerPage(""), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 385 | |
| 386 | r.Start(); |
| 387 | EXPECT_TRUE(r.is_pending()); |
| 388 | |
| 389 | // The request will be implicitly canceled when it is destroyed. The |
| 390 | // test delegate must not post a quit message when this happens because |
| 391 | // this test doesn't actually have a message loop. The quit message would |
| 392 | // get put on this thread's message queue and the next test would exit |
| 393 | // early, causing problems. |
| 394 | d.set_quit_on_complete(false); |
| 395 | } |
| 396 | // expect things to just cleanup properly. |
| 397 | |
| 398 | // we won't actually get a received reponse here because we've never run the |
| 399 | // message loop |
| 400 | EXPECT_FALSE(d.received_data_before_response()); |
| 401 | EXPECT_EQ(0, d.bytes_received()); |
| 402 | } |
| 403 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 404 | TEST_F(URLRequestTest, CancelTest5) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 405 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 406 | HTTPTestServer::CreateServer(L"", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 407 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 408 | scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
| 409 | |
| 410 | // populate cache |
| 411 | { |
| 412 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 413 | URLRequest r(server->TestServerPage("cachetime"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 414 | r.set_context(context); |
| 415 | r.Start(); |
| 416 | MessageLoop::current()->Run(); |
| 417 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 418 | } |
| 419 | |
| 420 | // cancel read from cache (see bug 990242) |
| 421 | { |
| 422 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 423 | URLRequest r(server->TestServerPage("cachetime"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 424 | r.set_context(context); |
| 425 | r.Start(); |
| 426 | r.Cancel(); |
| 427 | MessageLoop::current()->Run(); |
| 428 | |
| 429 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 430 | EXPECT_EQ(1, d.response_started_count()); |
| 431 | EXPECT_EQ(0, d.bytes_received()); |
| 432 | EXPECT_FALSE(d.received_data_before_response()); |
| 433 | } |
| 434 | |
| 435 | #ifndef NDEBUG |
| 436 | DCHECK_EQ(url_request_metrics.object_count, 0); |
| 437 | #endif |
| 438 | } |
| 439 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 440 | TEST_F(URLRequestTest, PostTest) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 441 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 442 | HTTPTestServer::CreateServer(L"net/data", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 443 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 444 | const int kMsgSize = 20000; // multiple of 10 |
| 445 | const int kIterations = 50; |
| 446 | char *uploadBytes = new char[kMsgSize+1]; |
| 447 | char *ptr = uploadBytes; |
| 448 | char marker = 'a'; |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 449 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 450 | memcpy(ptr, "----------", 10); |
| 451 | ptr += 10; |
| 452 | if (idx % 100 == 0) { |
| 453 | ptr--; |
| 454 | *ptr++ = marker; |
| 455 | if (++marker > 'z') |
| 456 | marker = 'a'; |
| 457 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 458 | } |
| 459 | uploadBytes[kMsgSize] = '\0'; |
| 460 | |
| 461 | scoped_refptr<URLRequestContext> context = |
| 462 | new URLRequestHttpCacheContext(); |
| 463 | |
| 464 | for (int i = 0; i < kIterations; ++i) { |
| 465 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 466 | URLRequest r(server->TestServerPage("echo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 467 | r.set_context(context); |
| 468 | r.set_method("POST"); |
| 469 | |
| 470 | r.AppendBytesToUpload(uploadBytes, kMsgSize); |
| 471 | |
| 472 | r.Start(); |
| 473 | EXPECT_TRUE(r.is_pending()); |
| 474 | |
| 475 | MessageLoop::current()->Run(); |
| 476 | |
| 477 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 478 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 479 | |
| 480 | EXPECT_FALSE(d.received_data_before_response()); |
| 481 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 482 | EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 483 | EXPECT_EQ(d.data_received().compare(uploadBytes), 0); |
| 484 | } |
| 485 | delete[] uploadBytes; |
| 486 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 487 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 488 | #endif |
| 489 | } |
| 490 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 491 | TEST_F(URLRequestTest, PostEmptyTest) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 492 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 493 | HTTPTestServer::CreateServer(L"net/data", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 494 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 495 | TestDelegate d; |
| 496 | { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 497 | TestURLRequest r(server->TestServerPage("echo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 498 | r.set_method("POST"); |
| 499 | |
| 500 | r.Start(); |
| 501 | EXPECT_TRUE(r.is_pending()); |
| 502 | |
| 503 | MessageLoop::current()->Run(); |
| 504 | |
| 505 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 506 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 507 | |
| 508 | EXPECT_FALSE(d.received_data_before_response()); |
| 509 | EXPECT_TRUE(d.data_received().empty()); |
| 510 | } |
| 511 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 512 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 513 | #endif |
| 514 | } |
| 515 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 516 | TEST_F(URLRequestTest, PostFileTest) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 517 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 518 | HTTPTestServer::CreateServer(L"net/data", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 519 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 520 | TestDelegate d; |
| 521 | { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 522 | TestURLRequest r(server->TestServerPage("echo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 523 | r.set_method("POST"); |
| 524 | |
| 525 | std::wstring dir; |
| 526 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 527 | file_util::SetCurrentDirectory(dir); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 528 | |
| 529 | std::wstring path; |
| 530 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 531 | file_util::AppendToPath(&path, L"net"); |
| 532 | file_util::AppendToPath(&path, L"data"); |
| 533 | file_util::AppendToPath(&path, L"url_request_unittest"); |
| 534 | file_util::AppendToPath(&path, L"with-headers.html"); |
| 535 | r.AppendFileToUpload(path); |
| 536 | |
| 537 | // This file should just be ignored in the upload stream. |
| 538 | r.AppendFileToUpload(L"c:\\path\\to\\non\\existant\\file.randomness.12345"); |
| 539 | |
| 540 | r.Start(); |
| 541 | EXPECT_TRUE(r.is_pending()); |
| 542 | |
| 543 | MessageLoop::current()->Run(); |
| 544 | |
[email protected] | 10a1fe9 | 2008-11-04 21:47:02 | [diff] [blame] | 545 | int64 longsize; |
| 546 | ASSERT_EQ(true, file_util::GetFileSize(path, &longsize)); |
| 547 | int size = static_cast<int>(longsize); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 548 | scoped_array<char> buf(new char[size]); |
| 549 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 550 | int size_read = static_cast<int>(file_util::ReadFile(path, |
| 551 | buf.get(), size)); |
[email protected] | eac0709a | 2008-11-04 21:00:46 | [diff] [blame] | 552 | ASSERT_EQ(size, size_read); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 553 | |
| 554 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 555 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 556 | |
| 557 | EXPECT_FALSE(d.received_data_before_response()); |
| 558 | |
| 559 | ASSERT_EQ(size, d.bytes_received()); |
| 560 | EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size)); |
| 561 | } |
| 562 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 563 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 564 | #endif |
| 565 | } |
| 566 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 567 | TEST_F(URLRequestTest, AboutBlankTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 568 | TestDelegate d; |
| 569 | { |
| 570 | TestURLRequest r(GURL("about:blank"), &d); |
| 571 | |
| 572 | r.Start(); |
| 573 | EXPECT_TRUE(r.is_pending()); |
| 574 | |
| 575 | MessageLoop::current()->Run(); |
| 576 | |
| 577 | EXPECT_TRUE(!r.is_pending()); |
| 578 | EXPECT_FALSE(d.received_data_before_response()); |
| 579 | EXPECT_EQ(d.bytes_received(), 0); |
| 580 | } |
| 581 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 582 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 583 | #endif |
| 584 | } |
| 585 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 586 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 587 | FilePath app_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 588 | PathService::Get(base::FILE_EXE, &app_path); |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 589 | GURL app_url = net::FilePathToFileURL(app_path.ToWStringHack()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 590 | |
| 591 | TestDelegate d; |
| 592 | { |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 593 | TestURLRequest r(app_url, &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 594 | |
| 595 | r.Start(); |
| 596 | EXPECT_TRUE(r.is_pending()); |
| 597 | |
| 598 | MessageLoop::current()->Run(); |
| 599 | |
[email protected] | b77280c | 2009-03-20 17:27:47 | [diff] [blame] | 600 | int64 file_size = -1; |
| 601 | EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 602 | |
| 603 | EXPECT_TRUE(!r.is_pending()); |
| 604 | EXPECT_EQ(1, d.response_started_count()); |
| 605 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 606 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 607 | } |
| 608 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 609 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 610 | #endif |
| 611 | } |
| 612 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 613 | TEST_F(URLRequestTest, InvalidUrlTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 614 | TestDelegate d; |
| 615 | { |
| 616 | TestURLRequest r(GURL("invalid url"), &d); |
| 617 | |
| 618 | r.Start(); |
| 619 | EXPECT_TRUE(r.is_pending()); |
| 620 | |
| 621 | MessageLoop::current()->Run(); |
| 622 | EXPECT_TRUE(d.request_failed()); |
| 623 | } |
| 624 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 625 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 626 | #endif |
| 627 | } |
| 628 | |
[email protected] | 6412d32 | 2008-11-21 23:49:30 | [diff] [blame] | 629 | // This test is disabled because it fails on some computers due to proxies |
| 630 | // returning a page in response to this request rather than reporting failure. |
| 631 | TEST_F(URLRequestTest, DISABLED_DnsFailureTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 632 | TestDelegate d; |
| 633 | { |
| 634 | URLRequest r(GURL("http://thisisnotavalidurl0123456789foo.com/"), &d); |
| 635 | |
| 636 | r.Start(); |
| 637 | EXPECT_TRUE(r.is_pending()); |
| 638 | |
| 639 | MessageLoop::current()->Run(); |
| 640 | EXPECT_TRUE(d.request_failed()); |
| 641 | } |
| 642 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 643 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 644 | #endif |
| 645 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 646 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 647 | TEST_F(URLRequestTest, ResponseHeadersTest) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 648 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 649 | HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 650 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 651 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 652 | TestURLRequest req(server->TestServerPage("files/with-headers.html"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 653 | req.Start(); |
| 654 | MessageLoop::current()->Run(); |
| 655 | |
| 656 | const net::HttpResponseHeaders* headers = req.response_headers(); |
| 657 | std::string header; |
| 658 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 659 | EXPECT_EQ("private", header); |
| 660 | |
| 661 | header.clear(); |
| 662 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 663 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 664 | |
| 665 | // The response has two "X-Multiple-Entries" headers. |
| 666 | // This verfies our output has them concatenated together. |
| 667 | header.clear(); |
| 668 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 669 | EXPECT_EQ("a, b", header); |
| 670 | } |
| 671 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 672 | TEST_F(URLRequestTest, BZip2ContentTest) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 673 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 674 | HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 675 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 676 | |
| 677 | // for localhost domain, we also should support bzip2 encoding |
| 678 | // first, get the original file |
| 679 | TestDelegate d1; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 680 | TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 681 | req1.Start(); |
| 682 | MessageLoop::current()->Run(); |
| 683 | |
| 684 | const std::string& got_content = d1.data_received(); |
| 685 | |
| 686 | // second, get bzip2 content |
| 687 | TestDelegate d2; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 688 | TestURLRequest req2(server->TestServerPage("realbz2files/google.txt"), &d2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 689 | req2.Start(); |
| 690 | MessageLoop::current()->Run(); |
| 691 | |
| 692 | const std::string& got_bz2_content = d2.data_received(); |
| 693 | |
| 694 | // compare those two results |
[email protected] | 24cfabd | 2008-08-15 00:05:39 | [diff] [blame] | 695 | EXPECT_EQ(got_content, got_bz2_content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 696 | } |
| 697 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 698 | TEST_F(URLRequestTest, BZip2ContentTest_IncrementalHeader) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 699 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 700 | HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 701 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 702 | |
| 703 | // for localhost domain, we also should support bzip2 encoding |
| 704 | // first, get the original file |
| 705 | TestDelegate d1; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 706 | TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 707 | req1.Start(); |
| 708 | MessageLoop::current()->Run(); |
| 709 | |
| 710 | const std::string& got_content = d1.data_received(); |
| 711 | |
| 712 | // second, get bzip2 content. ask the testserver to send the BZ2 header in |
| 713 | // two chunks with a delay between them. this tests our fix for bug 867161. |
| 714 | TestDelegate d2; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 715 | TestURLRequest req2(server->TestServerPage( |
| 716 | "realbz2files/google.txt?incremental-header"), &d2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 717 | req2.Start(); |
| 718 | MessageLoop::current()->Run(); |
| 719 | |
| 720 | const std::string& got_bz2_content = d2.data_received(); |
| 721 | |
| 722 | // compare those two results |
[email protected] | 24cfabd | 2008-08-15 00:05:39 | [diff] [blame] | 723 | EXPECT_EQ(got_content, got_bz2_content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 724 | } |
| 725 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 726 | #if defined(OS_WIN) |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 727 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 728 | std::wstring app_path; |
| 729 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 730 | file_util::AppendToPath(&app_path, L"net"); |
| 731 | file_util::AppendToPath(&app_path, L"data"); |
| 732 | file_util::AppendToPath(&app_path, L"url_request_unittest"); |
| 733 | file_util::AppendToPath(&app_path, L"with-headers.html"); |
| 734 | |
| 735 | std::wstring lnk_path = app_path + L".lnk"; |
| 736 | |
| 737 | HRESULT result; |
| 738 | IShellLink *shell = NULL; |
| 739 | IPersistFile *persist = NULL; |
| 740 | |
| 741 | CoInitialize(NULL); |
| 742 | // Temporarily create a shortcut for test |
| 743 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
| 744 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 745 | reinterpret_cast<LPVOID*>(&shell)); |
| 746 | EXPECT_TRUE(SUCCEEDED(result)); |
| 747 | result = shell->QueryInterface(IID_IPersistFile, |
| 748 | reinterpret_cast<LPVOID*>(&persist)); |
| 749 | EXPECT_TRUE(SUCCEEDED(result)); |
| 750 | result = shell->SetPath(app_path.c_str()); |
| 751 | EXPECT_TRUE(SUCCEEDED(result)); |
| 752 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 753 | EXPECT_TRUE(SUCCEEDED(result)); |
| 754 | result = persist->Save(lnk_path.c_str(), TRUE); |
| 755 | EXPECT_TRUE(SUCCEEDED(result)); |
| 756 | if (persist) |
| 757 | persist->Release(); |
| 758 | if (shell) |
| 759 | shell->Release(); |
| 760 | |
| 761 | TestDelegate d; |
| 762 | { |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 763 | TestURLRequest r(net::FilePathToFileURL(lnk_path), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 764 | |
| 765 | r.Start(); |
| 766 | EXPECT_TRUE(r.is_pending()); |
| 767 | |
| 768 | MessageLoop::current()->Run(); |
| 769 | |
| 770 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 771 | GetFileAttributesEx(app_path.c_str(), GetFileExInfoStandard, &data); |
| 772 | HANDLE file = CreateFile(app_path.c_str(), GENERIC_READ, |
| 773 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 774 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 775 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 776 | scoped_array<char> buffer(new char[data.nFileSizeLow]); |
| 777 | DWORD read_size; |
| 778 | BOOL result; |
| 779 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 780 | &read_size, NULL); |
| 781 | std::string content(buffer.get(), read_size); |
| 782 | CloseHandle(file); |
| 783 | |
| 784 | EXPECT_TRUE(!r.is_pending()); |
| 785 | EXPECT_EQ(1, d.received_redirect_count()); |
| 786 | EXPECT_EQ(content, d.data_received()); |
| 787 | } |
| 788 | |
| 789 | // Clean the shortcut |
| 790 | DeleteFile(lnk_path.c_str()); |
| 791 | CoUninitialize(); |
| 792 | |
| 793 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 794 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 795 | #endif |
| 796 | } |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 797 | #endif // defined(OS_WIN) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 798 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 799 | TEST_F(URLRequestTest, ContentTypeNormalizationTest) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 800 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 801 | HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 802 | ASSERT_TRUE(NULL != server.get()); |
| 803 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 804 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 805 | TestURLRequest req(server->TestServerPage( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 806 | "files/content-type-normalization.html"), &d); |
| 807 | req.Start(); |
| 808 | MessageLoop::current()->Run(); |
| 809 | |
| 810 | std::string mime_type; |
| 811 | req.GetMimeType(&mime_type); |
| 812 | EXPECT_EQ("text/html", mime_type); |
| 813 | |
| 814 | std::string charset; |
| 815 | req.GetCharset(&charset); |
| 816 | EXPECT_EQ("utf-8", charset); |
| 817 | req.Cancel(); |
| 818 | } |
| 819 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 820 | TEST_F(URLRequestTest, FileDirCancelTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 821 | // Put in mock resource provider. |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 822 | net::NetModule::SetResourceProvider(TestNetResourceProvider); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 823 | |
| 824 | TestDelegate d; |
| 825 | { |
| 826 | std::wstring file_path; |
| 827 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 828 | file_util::AppendToPath(&file_path, L"net"); |
| 829 | file_util::AppendToPath(&file_path, L"data"); |
| 830 | file_util::AppendToPath(&file_path, L""); |
| 831 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 832 | TestURLRequest req(net::FilePathToFileURL(file_path), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 833 | req.Start(); |
| 834 | EXPECT_TRUE(req.is_pending()); |
| 835 | |
| 836 | d.set_cancel_in_received_data_pending(true); |
| 837 | |
| 838 | MessageLoop::current()->Run(); |
| 839 | } |
| 840 | #ifndef NDEBUG |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 841 | DCHECK_EQ(url_request_metrics.object_count, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 842 | #endif |
| 843 | |
| 844 | // Take out mock resource provider. |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 845 | net::NetModule::SetResourceProvider(NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 846 | } |
| 847 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 848 | TEST_F(URLRequestTest, RestrictRedirects) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 849 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 850 | HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 851 | ASSERT_TRUE(NULL != server.get()); |
| 852 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 853 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 854 | TestURLRequest req(server->TestServerPage( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 855 | "files/redirect-to-file.html"), &d); |
| 856 | req.Start(); |
| 857 | MessageLoop::current()->Run(); |
| 858 | |
| 859 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 860 | EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error()); |
| 861 | } |
| 862 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 863 | TEST_F(URLRequestTest, NoUserPassInReferrer) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 864 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 865 | HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 866 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 867 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 868 | TestURLRequest req(server->TestServerPage( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 869 | "echoheader?Referer"), &d); |
| 870 | req.set_referrer("http://user:[email protected]/"); |
| 871 | req.Start(); |
| 872 | MessageLoop::current()->Run(); |
| 873 | |
| 874 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 875 | } |
| 876 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 877 | TEST_F(URLRequestTest, CancelRedirect) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 878 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 879 | HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 880 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 881 | TestDelegate d; |
| 882 | { |
| 883 | d.set_cancel_in_received_redirect(true); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 884 | TestURLRequest req(server->TestServerPage( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 885 | "files/redirect-test.html"), &d); |
| 886 | req.Start(); |
| 887 | MessageLoop::current()->Run(); |
| 888 | |
| 889 | EXPECT_EQ(1, d.response_started_count()); |
| 890 | EXPECT_EQ(0, d.bytes_received()); |
| 891 | EXPECT_FALSE(d.received_data_before_response()); |
| 892 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 893 | } |
| 894 | } |
| 895 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 896 | TEST_F(URLRequestTest, VaryHeader) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 897 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 898 | HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 899 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 900 | |
| 901 | scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
| 902 | |
| 903 | Time response_time; |
| 904 | |
| 905 | // populate the cache |
| 906 | { |
| 907 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 908 | URLRequest req(server->TestServerPage("echoheader?foo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 909 | req.set_context(context); |
| 910 | req.SetExtraRequestHeaders("foo:1"); |
| 911 | req.Start(); |
| 912 | MessageLoop::current()->Run(); |
| 913 | |
| 914 | response_time = req.response_time(); |
| 915 | } |
| 916 | |
| 917 | // Make sure that the response time of a future response will be in the |
| 918 | // future! |
[email protected] | 82fc7c7 | 2008-11-03 23:17:33 | [diff] [blame] | 919 | PlatformThread::Sleep(10); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 920 | |
| 921 | // expect a cache hit |
| 922 | { |
| 923 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 924 | URLRequest req(server->TestServerPage("echoheader?foo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 925 | req.set_context(context); |
| 926 | req.SetExtraRequestHeaders("foo:1"); |
| 927 | req.Start(); |
| 928 | MessageLoop::current()->Run(); |
| 929 | |
| 930 | EXPECT_TRUE(req.response_time() == response_time); |
| 931 | } |
| 932 | |
| 933 | // expect a cache miss |
| 934 | { |
| 935 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 936 | URLRequest req(server->TestServerPage("echoheader?foo"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 937 | req.set_context(context); |
| 938 | req.SetExtraRequestHeaders("foo:2"); |
| 939 | req.Start(); |
| 940 | MessageLoop::current()->Run(); |
| 941 | |
| 942 | EXPECT_FALSE(req.response_time() == response_time); |
| 943 | } |
| 944 | } |
| 945 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 946 | TEST_F(URLRequestTest, BasicAuth) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 947 | scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 948 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 949 | HTTPTestServer::CreateServer(L"", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 950 | ASSERT_TRUE(NULL != server.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 951 | |
| 952 | Time response_time; |
| 953 | |
| 954 | // populate the cache |
| 955 | { |
| 956 | TestDelegate d; |
| 957 | d.set_username(L"user"); |
| 958 | d.set_password(L"secret"); |
| 959 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 960 | URLRequest r(server->TestServerPage("auth-basic"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 961 | r.set_context(context); |
| 962 | r.Start(); |
| 963 | |
| 964 | MessageLoop::current()->Run(); |
| 965 | |
| 966 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 967 | |
| 968 | response_time = r.response_time(); |
| 969 | } |
| 970 | |
| 971 | // Let some time pass so we can ensure that a future response will have a |
| 972 | // response time value in the future. |
[email protected] | 82fc7c7 | 2008-11-03 23:17:33 | [diff] [blame] | 973 | PlatformThread::Sleep(10 /* milliseconds */); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 974 | |
| 975 | // repeat request with end-to-end validation. since auth-basic results in a |
| 976 | // cachable page, we expect this test to result in a 304. in which case, the |
| 977 | // response should be fetched from the cache. |
| 978 | { |
| 979 | TestDelegate d; |
| 980 | d.set_username(L"user"); |
| 981 | d.set_password(L"secret"); |
| 982 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 983 | URLRequest r(server->TestServerPage("auth-basic"), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 984 | r.set_context(context); |
| 985 | r.set_load_flags(net::LOAD_VALIDATE_CACHE); |
| 986 | r.Start(); |
| 987 | |
| 988 | MessageLoop::current()->Run(); |
| 989 | |
| 990 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 991 | |
| 992 | // Should be the same cached document, which means that the response time |
| 993 | // should not have changed. |
| 994 | EXPECT_TRUE(response_time == r.response_time()); |
| 995 | } |
| 996 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 997 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 998 | // Check that Set-Cookie headers in 401 responses are respected. |
| 999 | // http://crbug.com/6450 |
| 1000 | TEST_F(URLRequestTest, BasicAuthWithCookies) { |
| 1001 | scoped_refptr<HTTPTestServer> server = |
| 1002 | HTTPTestServer::CreateServer(L"", NULL); |
| 1003 | ASSERT_TRUE(NULL != server.get()); |
| 1004 | |
| 1005 | GURL url_requiring_auth = |
| 1006 | server->TestServerPage("auth-basic?set-cookie-if-challenged"); |
| 1007 | |
| 1008 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 1009 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 1010 | { |
| 1011 | scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
| 1012 | TestDelegate d; |
| 1013 | d.set_username(L"user"); |
| 1014 | d.set_password(L"secret"); |
| 1015 | |
| 1016 | URLRequest r(url_requiring_auth, &d); |
| 1017 | r.set_context(context); |
| 1018 | r.Start(); |
| 1019 | |
| 1020 | MessageLoop::current()->Run(); |
| 1021 | |
| 1022 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 1023 | |
| 1024 | // Make sure we sent the cookie in the restarted transaction. |
| 1025 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 1026 | != std::string::npos); |
| 1027 | } |
| 1028 | |
| 1029 | // Same test as above, except this time the restart is initiated earlier |
| 1030 | // (without user intervention since identity is embedded in the URL). |
| 1031 | { |
| 1032 | scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
| 1033 | TestDelegate d; |
| 1034 | |
| 1035 | GURL::Replacements replacements; |
| 1036 | std::string username("user2"); |
| 1037 | std::string password("secret"); |
| 1038 | replacements.SetUsernameStr(username); |
| 1039 | replacements.SetPasswordStr(password); |
| 1040 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 1041 | |
| 1042 | URLRequest r(url_with_identity, &d); |
| 1043 | r.set_context(context); |
| 1044 | r.Start(); |
| 1045 | |
| 1046 | MessageLoop::current()->Run(); |
| 1047 | |
| 1048 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 1049 | |
| 1050 | // Make sure we sent the cookie in the restarted transaction. |
| 1051 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 1052 | != std::string::npos); |
| 1053 | } |
| 1054 | } |
| 1055 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1056 | // In this test, we do a POST which the server will 302 redirect. |
| 1057 | // The subsequent transaction should use GET, and should not send the |
| 1058 | // Content-Type header. |
| 1059 | // http://code.google.com/p/chromium/issues/detail?id=843 |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 1060 | TEST_F(URLRequestTest, Post302RedirectGet) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1061 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 1062 | HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1063 | ASSERT_TRUE(NULL != server.get()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1064 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1065 | TestURLRequest req(server->TestServerPage("files/redirect-to-echoall"), &d); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1066 | req.set_method("POST"); |
| 1067 | |
| 1068 | // Set headers (some of which are specific to the POST). |
| 1069 | // ("Content-Length: 10" is just a junk value to make sure it gets stripped). |
| 1070 | req.SetExtraRequestHeaders( |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1071 | "Content-Type: multipart/form-data; " |
| 1072 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 1073 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 1074 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 1075 | "Accept-Language: en-US,en\r\n" |
| 1076 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 1077 | "Content-Length: 10\r\n" |
| 1078 | "Origin: http://localhost:1337/" |
| 1079 | ); |
| 1080 | req.Start(); |
| 1081 | MessageLoop::current()->Run(); |
| 1082 | |
| 1083 | std::string mime_type; |
| 1084 | req.GetMimeType(&mime_type); |
| 1085 | EXPECT_EQ("text/html", mime_type); |
| 1086 | |
| 1087 | const std::string& data = d.data_received(); |
| 1088 | |
| 1089 | // Check that the post-specific headers were stripped: |
| 1090 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 1091 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 1092 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 1093 | |
| 1094 | // These extra request headers should not have been stripped. |
| 1095 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 1096 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 1097 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 1098 | } |
| 1099 | |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1100 | TEST_F(URLRequestTest, Post307RedirectPost) { |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1101 | scoped_refptr<HTTPTestServer> server = |
[email protected] | cad01431 | 2009-01-29 21:59:55 | [diff] [blame] | 1102 | HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1103 | ASSERT_TRUE(NULL != server.get()); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1104 | TestDelegate d; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1105 | TestURLRequest req(server->TestServerPage("files/redirect307-to-echoall"), |
| 1106 | &d); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 1107 | req.set_method("POST"); |
| 1108 | req.Start(); |
| 1109 | MessageLoop::current()->Run(); |
| 1110 | EXPECT_EQ(req.method(), "POST"); |
| 1111 | } |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1112 | |
[email protected] | 4261e5b | 2009-01-08 22:53:22 | [diff] [blame] | 1113 | // FTP tests appear to be hanging some of the time |
| 1114 | #if 1 // !defined(OS_WIN) |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1115 | #define MAYBE_FTPGetTestAnonymous DISABLED_FTPGetTestAnonymous |
| 1116 | #define MAYBE_FTPGetTest DISABLED_FTPGetTest |
| 1117 | #define MAYBE_FTPCheckWrongUser DISABLED_FTPCheckWrongUser |
| 1118 | #define MAYBE_FTPCheckWrongPassword DISABLED_FTPCheckWrongPassword |
| 1119 | #else |
| 1120 | #define MAYBE_FTPGetTestAnonymous FTPGetTestAnonymous |
| 1121 | #define MAYBE_FTPGetTest FTPGetTest |
| 1122 | #define MAYBE_FTPCheckWrongUser FTPCheckWrongUser |
| 1123 | #define MAYBE_FTPCheckWrongPassword FTPCheckWrongPassword |
| 1124 | #endif |
| 1125 | |
| 1126 | TEST_F(URLRequestTest, MAYBE_FTPGetTestAnonymous) { |
| 1127 | scoped_refptr<FTPTestServer> server = FTPTestServer::CreateServer(L""); |
| 1128 | ASSERT_TRUE(NULL != server.get()); |
| 1129 | std::wstring app_path; |
| 1130 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 1131 | app_path.append(L"\\LICENSE"); |
| 1132 | TestDelegate d; |
| 1133 | { |
| 1134 | TestURLRequest r(server->TestServerPage("/LICENSE"), &d); |
| 1135 | r.Start(); |
| 1136 | EXPECT_TRUE(r.is_pending()); |
| 1137 | |
| 1138 | MessageLoop::current()->Run(); |
| 1139 | |
| 1140 | int64 file_size = 0; |
| 1141 | file_util::GetFileSize(app_path, &file_size); |
| 1142 | |
| 1143 | EXPECT_TRUE(!r.is_pending()); |
| 1144 | EXPECT_EQ(1, d.response_started_count()); |
| 1145 | EXPECT_FALSE(d.received_data_before_response()); |
| 1146 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | TEST_F(URLRequestTest, MAYBE_FTPGetTest) { |
| 1151 | scoped_refptr<FTPTestServer> server = |
| 1152 | FTPTestServer::CreateServer(L"", "chrome", "chrome"); |
| 1153 | ASSERT_TRUE(NULL != server.get()); |
| 1154 | std::wstring app_path; |
| 1155 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 1156 | app_path.append(L"\\LICENSE"); |
| 1157 | TestDelegate d; |
| 1158 | { |
| 1159 | TestURLRequest r(server->TestServerPage("/LICENSE"), &d); |
| 1160 | r.Start(); |
| 1161 | EXPECT_TRUE(r.is_pending()); |
| 1162 | |
| 1163 | MessageLoop::current()->Run(); |
| 1164 | |
| 1165 | int64 file_size = 0; |
| 1166 | file_util::GetFileSize(app_path, &file_size); |
| 1167 | |
| 1168 | EXPECT_TRUE(!r.is_pending()); |
| 1169 | EXPECT_EQ(1, d.response_started_count()); |
| 1170 | EXPECT_FALSE(d.received_data_before_response()); |
| 1171 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | TEST_F(URLRequestTest, MAYBE_FTPCheckWrongPassword) { |
| 1176 | scoped_refptr<FTPTestServer> server = |
| 1177 | FTPTestServer::CreateServer(L"", "chrome", "wrong_password"); |
| 1178 | ASSERT_TRUE(NULL != server.get()); |
| 1179 | std::wstring app_path; |
| 1180 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 1181 | app_path.append(L"\\LICENSE"); |
| 1182 | TestDelegate d; |
| 1183 | { |
| 1184 | TestURLRequest r(server->TestServerPage("/LICENSE"), &d); |
| 1185 | r.Start(); |
| 1186 | EXPECT_TRUE(r.is_pending()); |
| 1187 | |
| 1188 | MessageLoop::current()->Run(); |
| 1189 | |
| 1190 | int64 file_size = 0; |
| 1191 | file_util::GetFileSize(app_path, &file_size); |
| 1192 | |
| 1193 | EXPECT_TRUE(!r.is_pending()); |
| 1194 | EXPECT_EQ(1, d.response_started_count()); |
| 1195 | EXPECT_FALSE(d.received_data_before_response()); |
| 1196 | EXPECT_EQ(d.bytes_received(), 0); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | TEST_F(URLRequestTest, MAYBE_FTPCheckWrongUser) { |
| 1201 | scoped_refptr<FTPTestServer> server = |
| 1202 | FTPTestServer::CreateServer(L"", "wrong_user", "chrome"); |
| 1203 | ASSERT_TRUE(NULL != server.get()); |
| 1204 | std::wstring app_path; |
| 1205 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 1206 | app_path.append(L"\\LICENSE"); |
| 1207 | TestDelegate d; |
| 1208 | { |
| 1209 | TestURLRequest r(server->TestServerPage("/LICENSE"), &d); |
| 1210 | r.Start(); |
| 1211 | EXPECT_TRUE(r.is_pending()); |
| 1212 | |
| 1213 | MessageLoop::current()->Run(); |
| 1214 | |
| 1215 | int64 file_size = 0; |
| 1216 | file_util::GetFileSize(app_path, &file_size); |
| 1217 | |
| 1218 | EXPECT_TRUE(!r.is_pending()); |
| 1219 | EXPECT_EQ(1, d.response_started_count()); |
| 1220 | EXPECT_FALSE(d.received_data_before_response()); |
| 1221 | EXPECT_EQ(d.bytes_received(), 0); |
| 1222 | } |
| 1223 | } |