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