blob: f2028f1309b31cfac6ed93264117fbdc891805b0 [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]dc651782009-02-14 01:45:08106 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
[email protected]d1ec59082009-02-11 02:48:15107 // We should have rewritten the 302 response code as 500.
108 EXPECT_EQ(500, r.GetResponseCode());
[email protected]dc651782009-02-14 01:45:08109 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:15110 // We should not have followed the redirect.
111 EXPECT_EQ(0, d.received_redirect_count());
112 }
113}
114
[email protected]dc651782009-02-14 01:45:08115TEST_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]7a0bb4bf2008-11-19 21:41:48139TEST_F(URLRequestTest, GetTest_NoCache) {
[email protected]dd265012009-01-08 20:45:27140 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55141 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27142 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52143 TestDelegate d;
144 {
[email protected]dd265012009-01-08 20:45:27145 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52146
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]d1ec59082009-02-11 02:48:15157 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52158#endif
159}
160
[email protected]7a0bb4bf2008-11-19 21:41:48161TEST_F(URLRequestTest, GetTest) {
[email protected]dd265012009-01-08 20:45:27162 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55163 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27164 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52165 TestDelegate d;
166 {
[email protected]dd265012009-01-08 20:45:27167 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52168
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]d1ec59082009-02-11 02:48:15179 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52180#endif
181}
182
[email protected]73e0bba2009-02-19 22:57:09183TEST_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]ac4335c2009-02-19 00:48:06189
[email protected]73e0bba2009-02-19 22:57:09190#ifndef NDEBUG
191 DCHECK_EQ(url_request_metrics.object_count, 0);
192#endif
193}
194
195class HTTPSRequestTest : public testing::Test {
[email protected]ea224582008-12-07 20:25:46196};
197
198#if defined(OS_MACOSX)
[email protected]73e0bba2009-02-19 22:57:09199// 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]ea224582008-12-07 20:25:46202#else
[email protected]73e0bba2009-02-19 22:57:09203#define MAYBE_HTTPSGetTest HTTPSGetTest
[email protected]ea224582008-12-07 20:25:46204#endif
205
206TEST_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]dd265012009-01-08 20:45:27211 scoped_refptr<HTTPSTestServer> server =
[email protected]73e0bba2009-02-19 22:57:09212 HTTPSTestServer::CreateGoodServer(L"net/data/ssl");
[email protected]dd265012009-01-08 20:45:27213 ASSERT_TRUE(NULL != server.get());
[email protected]ea224582008-12-07 20:25:46214
[email protected]ea224582008-12-07 20:25:46215 TestDelegate d;
216 {
[email protected]dd265012009-01-08 20:45:27217 TestURLRequest r(server->TestServerPage(""), &d);
[email protected]ea224582008-12-07 20:25:46218
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]d1ec59082009-02-11 02:48:15229 DCHECK_EQ(url_request_metrics.object_count, 0);
[email protected]ea224582008-12-07 20:25:46230#endif
231}
232
[email protected]73e0bba2009-02-19 22:57:09233// TODO(dkegel): add test for expired and mismatched certificates here
234
[email protected]7a0bb4bf2008-11-19 21:41:48235TEST_F(URLRequestTest, CancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52236 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]d1ec59082009-02-11 02:48:15254 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52255#endif
256}
257
[email protected]7a0bb4bf2008-11-19 21:41:48258TEST_F(URLRequestTest, CancelTest2) {
[email protected]dd265012009-01-08 20:45:27259 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55260 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27261 ASSERT_TRUE(NULL != server.get());
262
263 // error C2446: '!=' : no conversion from 'HTTPTestServer *const '
264 // to 'const int'
265
initial.commit586acc5fe2008-07-26 22:42:52266 TestDelegate d;
267 {
[email protected]dd265012009-01-08 20:45:27268 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52269
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]d1ec59082009-02-11 02:48:15283 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52284#endif
285}
286
[email protected]7a0bb4bf2008-11-19 21:41:48287TEST_F(URLRequestTest, CancelTest3) {
[email protected]dd265012009-01-08 20:45:27288 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55289 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27290 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52291 TestDelegate d;
292 {
[email protected]dd265012009-01-08 20:45:27293 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52294
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]d1ec59082009-02-11 02:48:15311 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52312#endif
313}
314
[email protected]7a0bb4bf2008-11-19 21:41:48315TEST_F(URLRequestTest, CancelTest4) {
[email protected]dd265012009-01-08 20:45:27316 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55317 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27318 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52319 TestDelegate d;
320 {
[email protected]dd265012009-01-08 20:45:27321 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52322
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]7a0bb4bf2008-11-19 21:41:48341TEST_F(URLRequestTest, CancelTest5) {
[email protected]dd265012009-01-08 20:45:27342 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55343 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27344 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52345 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
346
347 // populate cache
348 {
349 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27350 URLRequest r(server->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52351 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]dd265012009-01-08 20:45:27360 URLRequest r(server->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52361 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]7a0bb4bf2008-11-19 21:41:48377TEST_F(URLRequestTest, PostTest) {
[email protected]dd265012009-01-08 20:45:27378 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55379 HTTPTestServer::CreateServer(L"net/data", NULL);
[email protected]dd265012009-01-08 20:45:27380 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52381 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]d1ec59082009-02-11 02:48:15386 for (int idx = 0; idx < kMsgSize/10; idx++) {
initial.commit586acc5fe2008-07-26 22:42:52387 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.commit586acc5fe2008-07-26 22:42:52395 }
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]dd265012009-01-08 20:45:27403 URLRequest r(server->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52404 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]d1ec59082009-02-11 02:48:15419 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0);
initial.commit586acc5fe2008-07-26 22:42:52420 EXPECT_EQ(d.data_received().compare(uploadBytes), 0);
421 }
422 delete[] uploadBytes;
423#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15424 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52425#endif
426}
427
[email protected]7a0bb4bf2008-11-19 21:41:48428TEST_F(URLRequestTest, PostEmptyTest) {
[email protected]dd265012009-01-08 20:45:27429 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55430 HTTPTestServer::CreateServer(L"net/data", NULL);
[email protected]dd265012009-01-08 20:45:27431 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52432 TestDelegate d;
433 {
[email protected]dd265012009-01-08 20:45:27434 TestURLRequest r(server->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52435 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]d1ec59082009-02-11 02:48:15449 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52450#endif
451}
452
[email protected]7a0bb4bf2008-11-19 21:41:48453TEST_F(URLRequestTest, PostFileTest) {
[email protected]dd265012009-01-08 20:45:27454 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55455 HTTPTestServer::CreateServer(L"net/data", NULL);
[email protected]dd265012009-01-08 20:45:27456 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52457 TestDelegate d;
458 {
[email protected]dd265012009-01-08 20:45:27459 TestURLRequest r(server->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52460 r.set_method("POST");
461
462 std::wstring dir;
463 PathService::Get(base::DIR_EXE, &dir);
[email protected]9396b252008-09-29 17:29:38464 file_util::SetCurrentDirectory(dir);
initial.commit586acc5fe2008-07-26 22:42:52465
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]10a1fe92008-11-04 21:47:02482 int64 longsize;
483 ASSERT_EQ(true, file_util::GetFileSize(path, &longsize));
484 int size = static_cast<int>(longsize);
initial.commit586acc5fe2008-07-26 22:42:52485 scoped_array<char> buf(new char[size]);
486
[email protected]dd265012009-01-08 20:45:27487 int size_read = static_cast<int>(file_util::ReadFile(path,
488 buf.get(), size));
[email protected]eac0709a2008-11-04 21:00:46489 ASSERT_EQ(size, size_read);
initial.commit586acc5fe2008-07-26 22:42:52490
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]d1ec59082009-02-11 02:48:15500 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52501#endif
502}
503
[email protected]7a0bb4bf2008-11-19 21:41:48504TEST_F(URLRequestTest, AboutBlankTest) {
initial.commit586acc5fe2008-07-26 22:42:52505 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]d1ec59082009-02-11 02:48:15519 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52520#endif
521}
522
[email protected]7a0bb4bf2008-11-19 21:41:48523TEST_F(URLRequestTest, FileTest) {
[email protected]b9e04f02008-11-27 04:03:57524 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52525 PathService::Get(base::FILE_EXE, &app_path);
[email protected]b9e04f02008-11-27 04:03:57526 GURL app_url = net::FilePathToFileURL(app_path.ToWStringHack());
initial.commit586acc5fe2008-07-26 22:42:52527
528 TestDelegate d;
529 {
[email protected]b9e04f02008-11-27 04:03:57530 TestURLRequest r(app_url, &d);
initial.commit586acc5fe2008-07-26 22:42:52531
532 r.Start();
533 EXPECT_TRUE(r.is_pending());
534
535 MessageLoop::current()->Run();
536
[email protected]9396b252008-09-29 17:29:38537 int64 file_size;
538 file_util::GetFileSize(app_path, &file_size);
initial.commit586acc5fe2008-07-26 22:42:52539
540 EXPECT_TRUE(!r.is_pending());
541 EXPECT_EQ(1, d.response_started_count());
542 EXPECT_FALSE(d.received_data_before_response());
[email protected]9396b252008-09-29 17:29:38543 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
initial.commit586acc5fe2008-07-26 22:42:52544 }
545#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15546 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52547#endif
548}
549
[email protected]7a0bb4bf2008-11-19 21:41:48550TEST_F(URLRequestTest, InvalidUrlTest) {
initial.commit586acc5fe2008-07-26 22:42:52551 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]d1ec59082009-02-11 02:48:15562 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52563#endif
564}
565
[email protected]6412d322008-11-21 23:49:30566// 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.
568TEST_F(URLRequestTest, DISABLED_DnsFailureTest) {
initial.commit586acc5fe2008-07-26 22:42:52569 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]d1ec59082009-02-11 02:48:15580 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52581#endif
582}
initial.commit586acc5fe2008-07-26 22:42:52583
[email protected]7a0bb4bf2008-11-19 21:41:48584TEST_F(URLRequestTest, ResponseHeadersTest) {
[email protected]dd265012009-01-08 20:45:27585 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55586 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27587 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52588 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27589 TestURLRequest req(server->TestServerPage("files/with-headers.html"), &d);
initial.commit586acc5fe2008-07-26 22:42:52590 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]7a0bb4bf2008-11-19 21:41:48609TEST_F(URLRequestTest, BZip2ContentTest) {
[email protected]dd265012009-01-08 20:45:27610 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55611 HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL);
[email protected]dd265012009-01-08 20:45:27612 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52613
614 // for localhost domain, we also should support bzip2 encoding
615 // first, get the original file
616 TestDelegate d1;
[email protected]dd265012009-01-08 20:45:27617 TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1);
initial.commit586acc5fe2008-07-26 22:42:52618 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]dd265012009-01-08 20:45:27625 TestURLRequest req2(server->TestServerPage("realbz2files/google.txt"), &d2);
initial.commit586acc5fe2008-07-26 22:42:52626 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]24cfabd2008-08-15 00:05:39632 EXPECT_EQ(got_content, got_bz2_content);
initial.commit586acc5fe2008-07-26 22:42:52633}
634
[email protected]7a0bb4bf2008-11-19 21:41:48635TEST_F(URLRequestTest, BZip2ContentTest_IncrementalHeader) {
[email protected]dd265012009-01-08 20:45:27636 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55637 HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL);
[email protected]dd265012009-01-08 20:45:27638 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52639
640 // for localhost domain, we also should support bzip2 encoding
641 // first, get the original file
642 TestDelegate d1;
[email protected]dd265012009-01-08 20:45:27643 TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1);
initial.commit586acc5fe2008-07-26 22:42:52644 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]dd265012009-01-08 20:45:27652 TestURLRequest req2(server->TestServerPage(
653 "realbz2files/google.txt?incremental-header"), &d2);
initial.commit586acc5fe2008-07-26 22:42:52654 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]24cfabd2008-08-15 00:05:39660 EXPECT_EQ(got_content, got_bz2_content);
initial.commit586acc5fe2008-07-26 22:42:52661}
662
[email protected]9396b252008-09-29 17:29:38663#if defined(OS_WIN)
[email protected]7a0bb4bf2008-11-19 21:41:48664TEST_F(URLRequestTest, ResolveShortcutTest) {
initial.commit586acc5fe2008-07-26 22:42:52665 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]8ac1a752008-07-31 19:40:37700 TestURLRequest r(net::FilePathToFileURL(lnk_path), &d);
initial.commit586acc5fe2008-07-26 22:42:52701
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]d1ec59082009-02-11 02:48:15731 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52732#endif
733}
[email protected]9396b252008-09-29 17:29:38734#endif // defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:52735
[email protected]7a0bb4bf2008-11-19 21:41:48736TEST_F(URLRequestTest, ContentTypeNormalizationTest) {
[email protected]dd265012009-01-08 20:45:27737 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55738 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27739 ASSERT_TRUE(NULL != server.get());
740
initial.commit586acc5fe2008-07-26 22:42:52741 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27742 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52743 "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]7a0bb4bf2008-11-19 21:41:48757TEST_F(URLRequestTest, FileDirCancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52758 // Put in mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37759 net::NetModule::SetResourceProvider(TestNetResourceProvider);
initial.commit586acc5fe2008-07-26 22:42:52760
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]8ac1a752008-07-31 19:40:37769 TestURLRequest req(net::FilePathToFileURL(file_path), &d);
initial.commit586acc5fe2008-07-26 22:42:52770 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]d1ec59082009-02-11 02:48:15778 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52779#endif
780
781 // Take out mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37782 net::NetModule::SetResourceProvider(NULL);
initial.commit586acc5fe2008-07-26 22:42:52783}
784
[email protected]7a0bb4bf2008-11-19 21:41:48785TEST_F(URLRequestTest, RestrictRedirects) {
[email protected]dd265012009-01-08 20:45:27786 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55787 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27788 ASSERT_TRUE(NULL != server.get());
789
initial.commit586acc5fe2008-07-26 22:42:52790 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27791 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52792 "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]7a0bb4bf2008-11-19 21:41:48800TEST_F(URLRequestTest, NoUserPassInReferrer) {
[email protected]dd265012009-01-08 20:45:27801 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55802 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27803 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52804 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27805 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52806 "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]7a0bb4bf2008-11-19 21:41:48814TEST_F(URLRequestTest, CancelRedirect) {
[email protected]dd265012009-01-08 20:45:27815 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55816 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27817 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52818 TestDelegate d;
819 {
820 d.set_cancel_in_received_redirect(true);
[email protected]dd265012009-01-08 20:45:27821 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52822 "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]7a0bb4bf2008-11-19 21:41:48833TEST_F(URLRequestTest, VaryHeader) {
[email protected]dd265012009-01-08 20:45:27834 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55835 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27836 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52837
838 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
839
840 Time response_time;
841
842 // populate the cache
843 {
844 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27845 URLRequest req(server->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52846 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]82fc7c72008-11-03 23:17:33856 PlatformThread::Sleep(10);
initial.commit586acc5fe2008-07-26 22:42:52857
858 // expect a cache hit
859 {
860 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27861 URLRequest req(server->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52862 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]dd265012009-01-08 20:45:27873 URLRequest req(server->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52874 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]7a0bb4bf2008-11-19 21:41:48883TEST_F(URLRequestTest, BasicAuth) {
initial.commit586acc5fe2008-07-26 22:42:52884 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
[email protected]dd265012009-01-08 20:45:27885 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55886 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27887 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52888
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]dd265012009-01-08 20:45:27897 URLRequest r(server->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:52898 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]82fc7c72008-11-03 23:17:33910 PlatformThread::Sleep(10 /* milliseconds */);
initial.commit586acc5fe2008-07-26 22:42:52911
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]dd265012009-01-08 20:45:27920 URLRequest r(server->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:52921 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.botbf09a502008-08-24 00:55:55934
[email protected]71c64f62008-11-15 04:36:51935// 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]7a0bb4bf2008-11-19 21:41:48939TEST_F(URLRequestTest, Post302RedirectGet) {
[email protected]dd265012009-01-08 20:45:27940 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55941 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27942 ASSERT_TRUE(NULL != server.get());
[email protected]71c64f62008-11-15 04:36:51943 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27944 TestURLRequest req(server->TestServerPage("files/redirect-to-echoall"), &d);
[email protected]71c64f62008-11-15 04:36:51945 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]dd265012009-01-08 20:45:27950 "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]71c64f62008-11-15 04:36:51954 "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]140932f2008-12-12 03:58:06979TEST_F(URLRequestTest, Post307RedirectPost) {
[email protected]dd265012009-01-08 20:45:27980 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55981 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27982 ASSERT_TRUE(NULL != server.get());
[email protected]140932f2008-12-12 03:58:06983 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27984 TestURLRequest req(server->TestServerPage("files/redirect307-to-echoall"),
985 &d);
[email protected]140932f2008-12-12 03:58:06986 req.set_method("POST");
987 req.Start();
988 MessageLoop::current()->Run();
989 EXPECT_EQ(req.method(), "POST");
990}
[email protected]dd265012009-01-08 20:45:27991
[email protected]4261e5b2009-01-08 22:53:22992// FTP tests appear to be hanging some of the time
993#if 1 // !defined(OS_WIN)
[email protected]dd265012009-01-08 20:45:27994 #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
1005TEST_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
1029TEST_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
1054TEST_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
1079TEST_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