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