blob: c8e28960d8770d9e1a8ea3c11f2f32c82b532c3d [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit586acc5fe2008-07-26 22:42:524
[email protected]9396b252008-09-29 17:29:385#include "net/url_request/url_request_unittest.h"
6
[email protected]ea224582008-12-07 20:25:467#include "build/build_config.h"
8
[email protected]9396b252008-09-29 17:29:389#if defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:5210#include <windows.h>
11#include <shlobj.h>
[email protected]ea224582008-12-07 20:25:4612#elif defined(OS_LINUX)
13#include "base/nss_init.h"
[email protected]9396b252008-09-29 17:29:3814#endif
15
initial.commit586acc5fe2008-07-26 22:42:5216#include <algorithm>
17#include <string>
18
initial.commit586acc5fe2008-07-26 22:42:5219#include "base/message_loop.h"
20#include "base/path_service.h"
21#include "base/process_util.h"
[email protected]73f5d662008-11-20 01:08:1722#include "base/string_piece.h"
initial.commit586acc5fe2008-07-26 22:42:5223#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]ea224582008-12-07 20:25:4628#include "net/base/ssl_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5229#include "net/disk_cache/disk_cache.h"
30#include "net/http/http_cache.h"
31#include "net/http/http_network_layer.h"
[email protected]319d9e6f2009-02-18 19:47:2132#include "net/http/http_response_headers.h"
[email protected]63de95b2008-12-10 04:11:2733#include "net/proxy/proxy_service.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/url_request/url_request.h"
35#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1536#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5237
[email protected]e1acf6f2008-10-27 20:43:3338using base::Time;
39
initial.commit586acc5fe2008-07-26 22:42:5240namespace {
41
initial.commit586acc5fe2008-07-26 22:42:5242class URLRequestHttpCacheContext : public URLRequestContext {
43 public:
44 URLRequestHttpCacheContext() {
[email protected]51fff29d2008-12-19 22:17:5345 proxy_service_ = net::ProxyService::CreateNull();
[email protected]db8f44c2008-12-13 04:52:0146 http_transaction_factory_ =
[email protected]63de95b2008-12-10 04:11:2747 new net::HttpCache(net::HttpNetworkLayer::CreateFactory(proxy_service_),
[email protected]db8f44c2008-12-13 04:52:0148 disk_cache::CreateInMemoryCacheBackend(0));
49 }
50
51 virtual ~URLRequestHttpCacheContext() {
52 delete http_transaction_factory_;
53 delete proxy_service_;
initial.commit586acc5fe2008-07-26 22:42:5254 }
55};
56
57class TestURLRequest : public URLRequest {
58 public:
[email protected]d1ec59082009-02-11 02:48:1559 TestURLRequest(const GURL& url, Delegate* delegate)
60 : URLRequest(url, delegate) {
61 set_context(new URLRequestHttpCacheContext());
62 }
initial.commit586acc5fe2008-07-26 22:42:5263};
64
[email protected]73f5d662008-11-20 01:08:1765StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:5266 return "header";
67}
68
[email protected]71c64f62008-11-15 04:36:5169// Do a case-insensitive search through |haystack| for |needle|.
70bool 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]9396b252008-09-29 17:29:3880} // namespace
initial.commit586acc5fe2008-07-26 22:42:5281
[email protected]7a0bb4bf2008-11-19 21:41:4882// Inherit PlatformTest since we require the autorelease pool on Mac OS X.f
83class URLRequestTest : public PlatformTest {
84};
85
[email protected]d1ec59082009-02-11 02:48:1586TEST_F(URLRequestTest, ProxyTunnelRedirectTest) {
87 // In this unit test, we're using the HTTPTestServer as a proxy server and
[email protected]dc651782009-02-14 01:45:0888 // 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]d1ec59082009-02-11 02:48:1590 // 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]c744cf22009-02-27 07:28:08106 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
107 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08108 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:15109 // We should not have followed the redirect.
110 EXPECT_EQ(0, d.received_redirect_count());
111 }
112}
113
[email protected]dc651782009-02-14 01:45:08114TEST_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]c744cf22009-02-27 07:28:08134 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08135 }
136}
137
[email protected]7a0bb4bf2008-11-19 21:41:48138TEST_F(URLRequestTest, GetTest_NoCache) {
[email protected]dd265012009-01-08 20:45:27139 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55140 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27141 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52142 TestDelegate d;
143 {
[email protected]dd265012009-01-08 20:45:27144 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52145
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]d1ec59082009-02-11 02:48:15156 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52157#endif
158}
159
[email protected]7a0bb4bf2008-11-19 21:41:48160TEST_F(URLRequestTest, GetTest) {
[email protected]dd265012009-01-08 20:45:27161 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55162 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27163 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52164 TestDelegate d;
165 {
[email protected]dd265012009-01-08 20:45:27166 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52167
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]d1ec59082009-02-11 02:48:15178 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52179#endif
180}
181
[email protected]73e0bba2009-02-19 22:57:09182TEST_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]ac4335c2009-02-19 00:48:06188
[email protected]73e0bba2009-02-19 22:57:09189#ifndef NDEBUG
190 DCHECK_EQ(url_request_metrics.object_count, 0);
191#endif
192}
193
194class HTTPSRequestTest : public testing::Test {
[email protected]ea224582008-12-07 20:25:46195};
196
197#if defined(OS_MACOSX)
[email protected]73e0bba2009-02-19 22:57:09198// 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]ea224582008-12-07 20:25:46201#else
[email protected]73e0bba2009-02-19 22:57:09202#define MAYBE_HTTPSGetTest HTTPSGetTest
[email protected]ea224582008-12-07 20:25:46203#endif
204
205TEST_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]dd265012009-01-08 20:45:27210 scoped_refptr<HTTPSTestServer> server =
[email protected]73e0bba2009-02-19 22:57:09211 HTTPSTestServer::CreateGoodServer(L"net/data/ssl");
[email protected]dd265012009-01-08 20:45:27212 ASSERT_TRUE(NULL != server.get());
[email protected]ea224582008-12-07 20:25:46213
[email protected]ea224582008-12-07 20:25:46214 TestDelegate d;
215 {
[email protected]dd265012009-01-08 20:45:27216 TestURLRequest r(server->TestServerPage(""), &d);
[email protected]ea224582008-12-07 20:25:46217
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]d1ec59082009-02-11 02:48:15228 DCHECK_EQ(url_request_metrics.object_count, 0);
[email protected]ea224582008-12-07 20:25:46229#endif
230}
231
[email protected]73e0bba2009-02-19 22:57:09232// TODO(dkegel): add test for expired and mismatched certificates here
233
[email protected]7a0bb4bf2008-11-19 21:41:48234TEST_F(URLRequestTest, CancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52235 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]d1ec59082009-02-11 02:48:15253 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52254#endif
255}
256
[email protected]7a0bb4bf2008-11-19 21:41:48257TEST_F(URLRequestTest, CancelTest2) {
[email protected]dd265012009-01-08 20:45:27258 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55259 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27260 ASSERT_TRUE(NULL != server.get());
261
262 // error C2446: '!=' : no conversion from 'HTTPTestServer *const '
263 // to 'const int'
264
initial.commit586acc5fe2008-07-26 22:42:52265 TestDelegate d;
266 {
[email protected]dd265012009-01-08 20:45:27267 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52268
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]d1ec59082009-02-11 02:48:15282 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52283#endif
284}
285
[email protected]7a0bb4bf2008-11-19 21:41:48286TEST_F(URLRequestTest, CancelTest3) {
[email protected]dd265012009-01-08 20:45:27287 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55288 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27289 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52290 TestDelegate d;
291 {
[email protected]dd265012009-01-08 20:45:27292 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52293
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]d1ec59082009-02-11 02:48:15310 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52311#endif
312}
313
[email protected]7a0bb4bf2008-11-19 21:41:48314TEST_F(URLRequestTest, CancelTest4) {
[email protected]dd265012009-01-08 20:45:27315 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55316 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27317 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52318 TestDelegate d;
319 {
[email protected]dd265012009-01-08 20:45:27320 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52321
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]7a0bb4bf2008-11-19 21:41:48340TEST_F(URLRequestTest, CancelTest5) {
[email protected]dd265012009-01-08 20:45:27341 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55342 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27343 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52344 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
345
346 // populate cache
347 {
348 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27349 URLRequest r(server->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52350 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]dd265012009-01-08 20:45:27359 URLRequest r(server->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52360 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]7a0bb4bf2008-11-19 21:41:48376TEST_F(URLRequestTest, PostTest) {
[email protected]dd265012009-01-08 20:45:27377 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55378 HTTPTestServer::CreateServer(L"net/data", NULL);
[email protected]dd265012009-01-08 20:45:27379 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52380 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]d1ec59082009-02-11 02:48:15385 for (int idx = 0; idx < kMsgSize/10; idx++) {
initial.commit586acc5fe2008-07-26 22:42:52386 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.commit586acc5fe2008-07-26 22:42:52394 }
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]dd265012009-01-08 20:45:27402 URLRequest r(server->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52403 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]d1ec59082009-02-11 02:48:15418 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0);
initial.commit586acc5fe2008-07-26 22:42:52419 EXPECT_EQ(d.data_received().compare(uploadBytes), 0);
420 }
421 delete[] uploadBytes;
422#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15423 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52424#endif
425}
426
[email protected]7a0bb4bf2008-11-19 21:41:48427TEST_F(URLRequestTest, PostEmptyTest) {
[email protected]dd265012009-01-08 20:45:27428 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55429 HTTPTestServer::CreateServer(L"net/data", NULL);
[email protected]dd265012009-01-08 20:45:27430 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52431 TestDelegate d;
432 {
[email protected]dd265012009-01-08 20:45:27433 TestURLRequest r(server->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52434 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]d1ec59082009-02-11 02:48:15448 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52449#endif
450}
451
[email protected]7a0bb4bf2008-11-19 21:41:48452TEST_F(URLRequestTest, PostFileTest) {
[email protected]dd265012009-01-08 20:45:27453 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55454 HTTPTestServer::CreateServer(L"net/data", NULL);
[email protected]dd265012009-01-08 20:45:27455 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52456 TestDelegate d;
457 {
[email protected]dd265012009-01-08 20:45:27458 TestURLRequest r(server->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52459 r.set_method("POST");
460
461 std::wstring dir;
462 PathService::Get(base::DIR_EXE, &dir);
[email protected]9396b252008-09-29 17:29:38463 file_util::SetCurrentDirectory(dir);
initial.commit586acc5fe2008-07-26 22:42:52464
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]10a1fe92008-11-04 21:47:02481 int64 longsize;
482 ASSERT_EQ(true, file_util::GetFileSize(path, &longsize));
483 int size = static_cast<int>(longsize);
initial.commit586acc5fe2008-07-26 22:42:52484 scoped_array<char> buf(new char[size]);
485
[email protected]dd265012009-01-08 20:45:27486 int size_read = static_cast<int>(file_util::ReadFile(path,
487 buf.get(), size));
[email protected]eac0709a2008-11-04 21:00:46488 ASSERT_EQ(size, size_read);
initial.commit586acc5fe2008-07-26 22:42:52489
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]d1ec59082009-02-11 02:48:15499 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52500#endif
501}
502
[email protected]7a0bb4bf2008-11-19 21:41:48503TEST_F(URLRequestTest, AboutBlankTest) {
initial.commit586acc5fe2008-07-26 22:42:52504 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]d1ec59082009-02-11 02:48:15518 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52519#endif
520}
521
[email protected]7a0bb4bf2008-11-19 21:41:48522TEST_F(URLRequestTest, FileTest) {
[email protected]b9e04f02008-11-27 04:03:57523 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52524 PathService::Get(base::FILE_EXE, &app_path);
[email protected]b9e04f02008-11-27 04:03:57525 GURL app_url = net::FilePathToFileURL(app_path.ToWStringHack());
initial.commit586acc5fe2008-07-26 22:42:52526
527 TestDelegate d;
528 {
[email protected]b9e04f02008-11-27 04:03:57529 TestURLRequest r(app_url, &d);
initial.commit586acc5fe2008-07-26 22:42:52530
531 r.Start();
532 EXPECT_TRUE(r.is_pending());
533
534 MessageLoop::current()->Run();
535
[email protected]9396b252008-09-29 17:29:38536 int64 file_size;
537 file_util::GetFileSize(app_path, &file_size);
initial.commit586acc5fe2008-07-26 22:42:52538
539 EXPECT_TRUE(!r.is_pending());
540 EXPECT_EQ(1, d.response_started_count());
541 EXPECT_FALSE(d.received_data_before_response());
[email protected]9396b252008-09-29 17:29:38542 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
initial.commit586acc5fe2008-07-26 22:42:52543 }
544#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15545 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52546#endif
547}
548
[email protected]7a0bb4bf2008-11-19 21:41:48549TEST_F(URLRequestTest, InvalidUrlTest) {
initial.commit586acc5fe2008-07-26 22:42:52550 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]d1ec59082009-02-11 02:48:15561 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52562#endif
563}
564
[email protected]6412d322008-11-21 23:49:30565// 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.
567TEST_F(URLRequestTest, DISABLED_DnsFailureTest) {
initial.commit586acc5fe2008-07-26 22:42:52568 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]d1ec59082009-02-11 02:48:15579 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52580#endif
581}
initial.commit586acc5fe2008-07-26 22:42:52582
[email protected]7a0bb4bf2008-11-19 21:41:48583TEST_F(URLRequestTest, ResponseHeadersTest) {
[email protected]dd265012009-01-08 20:45:27584 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55585 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27586 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52587 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27588 TestURLRequest req(server->TestServerPage("files/with-headers.html"), &d);
initial.commit586acc5fe2008-07-26 22:42:52589 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]7a0bb4bf2008-11-19 21:41:48608TEST_F(URLRequestTest, BZip2ContentTest) {
[email protected]dd265012009-01-08 20:45:27609 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55610 HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL);
[email protected]dd265012009-01-08 20:45:27611 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52612
613 // for localhost domain, we also should support bzip2 encoding
614 // first, get the original file
615 TestDelegate d1;
[email protected]dd265012009-01-08 20:45:27616 TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1);
initial.commit586acc5fe2008-07-26 22:42:52617 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]dd265012009-01-08 20:45:27624 TestURLRequest req2(server->TestServerPage("realbz2files/google.txt"), &d2);
initial.commit586acc5fe2008-07-26 22:42:52625 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]24cfabd2008-08-15 00:05:39631 EXPECT_EQ(got_content, got_bz2_content);
initial.commit586acc5fe2008-07-26 22:42:52632}
633
[email protected]7a0bb4bf2008-11-19 21:41:48634TEST_F(URLRequestTest, BZip2ContentTest_IncrementalHeader) {
[email protected]dd265012009-01-08 20:45:27635 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55636 HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL);
[email protected]dd265012009-01-08 20:45:27637 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52638
639 // for localhost domain, we also should support bzip2 encoding
640 // first, get the original file
641 TestDelegate d1;
[email protected]dd265012009-01-08 20:45:27642 TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1);
initial.commit586acc5fe2008-07-26 22:42:52643 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]dd265012009-01-08 20:45:27651 TestURLRequest req2(server->TestServerPage(
652 "realbz2files/google.txt?incremental-header"), &d2);
initial.commit586acc5fe2008-07-26 22:42:52653 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]24cfabd2008-08-15 00:05:39659 EXPECT_EQ(got_content, got_bz2_content);
initial.commit586acc5fe2008-07-26 22:42:52660}
661
[email protected]9396b252008-09-29 17:29:38662#if defined(OS_WIN)
[email protected]7a0bb4bf2008-11-19 21:41:48663TEST_F(URLRequestTest, ResolveShortcutTest) {
initial.commit586acc5fe2008-07-26 22:42:52664 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]8ac1a752008-07-31 19:40:37699 TestURLRequest r(net::FilePathToFileURL(lnk_path), &d);
initial.commit586acc5fe2008-07-26 22:42:52700
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]d1ec59082009-02-11 02:48:15730 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52731#endif
732}
[email protected]9396b252008-09-29 17:29:38733#endif // defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:52734
[email protected]7a0bb4bf2008-11-19 21:41:48735TEST_F(URLRequestTest, ContentTypeNormalizationTest) {
[email protected]dd265012009-01-08 20:45:27736 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55737 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27738 ASSERT_TRUE(NULL != server.get());
739
initial.commit586acc5fe2008-07-26 22:42:52740 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27741 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52742 "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]7a0bb4bf2008-11-19 21:41:48756TEST_F(URLRequestTest, FileDirCancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52757 // Put in mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37758 net::NetModule::SetResourceProvider(TestNetResourceProvider);
initial.commit586acc5fe2008-07-26 22:42:52759
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]8ac1a752008-07-31 19:40:37768 TestURLRequest req(net::FilePathToFileURL(file_path), &d);
initial.commit586acc5fe2008-07-26 22:42:52769 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]d1ec59082009-02-11 02:48:15777 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52778#endif
779
780 // Take out mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37781 net::NetModule::SetResourceProvider(NULL);
initial.commit586acc5fe2008-07-26 22:42:52782}
783
[email protected]7a0bb4bf2008-11-19 21:41:48784TEST_F(URLRequestTest, RestrictRedirects) {
[email protected]dd265012009-01-08 20:45:27785 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55786 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27787 ASSERT_TRUE(NULL != server.get());
788
initial.commit586acc5fe2008-07-26 22:42:52789 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27790 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52791 "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]7a0bb4bf2008-11-19 21:41:48799TEST_F(URLRequestTest, NoUserPassInReferrer) {
[email protected]dd265012009-01-08 20:45:27800 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55801 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27802 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52803 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27804 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52805 "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]7a0bb4bf2008-11-19 21:41:48813TEST_F(URLRequestTest, CancelRedirect) {
[email protected]dd265012009-01-08 20:45:27814 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55815 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27816 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52817 TestDelegate d;
818 {
819 d.set_cancel_in_received_redirect(true);
[email protected]dd265012009-01-08 20:45:27820 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52821 "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]7a0bb4bf2008-11-19 21:41:48832TEST_F(URLRequestTest, VaryHeader) {
[email protected]dd265012009-01-08 20:45:27833 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55834 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27835 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52836
837 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
838
839 Time response_time;
840
841 // populate the cache
842 {
843 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27844 URLRequest req(server->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52845 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]82fc7c72008-11-03 23:17:33855 PlatformThread::Sleep(10);
initial.commit586acc5fe2008-07-26 22:42:52856
857 // expect a cache hit
858 {
859 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27860 URLRequest req(server->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52861 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]dd265012009-01-08 20:45:27872 URLRequest req(server->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52873 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]7a0bb4bf2008-11-19 21:41:48882TEST_F(URLRequestTest, BasicAuth) {
initial.commit586acc5fe2008-07-26 22:42:52883 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
[email protected]dd265012009-01-08 20:45:27884 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55885 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27886 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52887
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]dd265012009-01-08 20:45:27896 URLRequest r(server->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:52897 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]82fc7c72008-11-03 23:17:33909 PlatformThread::Sleep(10 /* milliseconds */);
initial.commit586acc5fe2008-07-26 22:42:52910
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]dd265012009-01-08 20:45:27919 URLRequest r(server->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:52920 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.botbf09a502008-08-24 00:55:55933
[email protected]71c64f62008-11-15 04:36:51934// 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]7a0bb4bf2008-11-19 21:41:48938TEST_F(URLRequestTest, Post302RedirectGet) {
[email protected]dd265012009-01-08 20:45:27939 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55940 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27941 ASSERT_TRUE(NULL != server.get());
[email protected]71c64f62008-11-15 04:36:51942 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27943 TestURLRequest req(server->TestServerPage("files/redirect-to-echoall"), &d);
[email protected]71c64f62008-11-15 04:36:51944 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]dd265012009-01-08 20:45:27949 "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]71c64f62008-11-15 04:36:51953 "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]140932f2008-12-12 03:58:06978TEST_F(URLRequestTest, Post307RedirectPost) {
[email protected]dd265012009-01-08 20:45:27979 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55980 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27981 ASSERT_TRUE(NULL != server.get());
[email protected]140932f2008-12-12 03:58:06982 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27983 TestURLRequest req(server->TestServerPage("files/redirect307-to-echoall"),
984 &d);
[email protected]140932f2008-12-12 03:58:06985 req.set_method("POST");
986 req.Start();
987 MessageLoop::current()->Run();
988 EXPECT_EQ(req.method(), "POST");
989}
[email protected]dd265012009-01-08 20:45:27990
[email protected]4261e5b2009-01-08 22:53:22991// FTP tests appear to be hanging some of the time
992#if 1 // !defined(OS_WIN)
[email protected]dd265012009-01-08 20:45:27993 #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
1004TEST_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
1028TEST_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
1053TEST_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
1078TEST_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