blob: 16af0d024677229d28f32c7e273591ba27829103 [file] [log] [blame]
[email protected]661376a2009-04-29 02:04:231// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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
[email protected]399b8702009-05-01 20:34:0219#include "base/file_util.h"
[email protected]34b2b002009-11-20 06:53:2820#include "base/format_macros.h"
initial.commit586acc5fe2008-07-26 22:42:5221#include "base/message_loop.h"
22#include "base/path_service.h"
23#include "base/process_util.h"
[email protected]73f5d662008-11-20 01:08:1724#include "base/string_piece.h"
initial.commit586acc5fe2008-07-26 22:42:5225#include "base/string_util.h"
[email protected]0757e7702009-03-27 04:00:2226#include "net/base/cookie_monster.h"
initial.commit586acc5fe2008-07-26 22:42:5227#include "net/base/load_flags.h"
[email protected]c31a54592009-09-04 02:36:1628#include "net/base/load_log.h"
29#include "net/base/load_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5230#include "net/base/net_errors.h"
31#include "net/base/net_module.h"
32#include "net/base/net_util.h"
[email protected]195e77d2009-07-23 19:10:2333#include "net/base/upload_data.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/disk_cache/disk_cache.h"
[email protected]ba2f3342009-07-30 18:08:4235#include "net/ftp/ftp_network_layer.h"
initial.commit586acc5fe2008-07-26 22:42:5236#include "net/http/http_cache.h"
37#include "net/http/http_network_layer.h"
[email protected]319d9e6f2009-02-18 19:47:2138#include "net/http/http_response_headers.h"
[email protected]63de95b2008-12-10 04:11:2739#include "net/proxy/proxy_service.h"
[email protected]f7984fc62009-06-22 23:26:4440#include "net/socket/ssl_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5241#include "net/url_request/url_request.h"
[email protected]7886a8c2009-08-21 04:11:0942#include "net/url_request/url_request_file_dir_job.h"
[email protected]bcb84f8b2009-08-31 16:20:1443#include "net/url_request/url_request_http_job.h"
[email protected]a5c713f2009-04-16 21:05:4744#include "net/url_request/url_request_test_job.h"
initial.commit586acc5fe2008-07-26 22:42:5245#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1546#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5247
[email protected]e1acf6f2008-10-27 20:43:3348using base::Time;
49
initial.commit586acc5fe2008-07-26 22:42:5250namespace {
51
[email protected]ba2f3342009-07-30 18:08:4252class URLRequestTestContext : public URLRequestContext {
initial.commit586acc5fe2008-07-26 22:42:5253 public:
[email protected]ba2f3342009-07-30 18:08:4254 URLRequestTestContext() {
[email protected]b59ff372009-07-15 22:04:3255 host_resolver_ = net::CreateSystemHostResolver();
[email protected]51fff29d2008-12-19 22:17:5356 proxy_service_ = net::ProxyService::CreateNull();
[email protected]ba2f3342009-07-30 18:08:4257 ftp_transaction_factory_ = new net::FtpNetworkLayer(host_resolver_);
[email protected]db36938c2009-08-19 21:48:4258 ssl_config_service_ = new net::SSLConfigServiceDefaults;
[email protected]db8f44c2008-12-13 04:52:0159 http_transaction_factory_ =
[email protected]8a00f00a2009-06-12 00:49:3860 new net::HttpCache(
[email protected]db36938c2009-08-19 21:48:4261 net::HttpNetworkLayer::CreateFactory(host_resolver_, proxy_service_,
62 ssl_config_service_),
[email protected]8a00f00a2009-06-12 00:49:3863 disk_cache::CreateInMemoryCacheBackend(0));
[email protected]0757e7702009-03-27 04:00:2264 // In-memory cookie store.
65 cookie_store_ = new net::CookieMonster();
[email protected]09a362d32009-09-24 18:01:3366 accept_language_ = "en-us,fr";
67 accept_charset_ = "iso-8859-1,*,utf-8";
[email protected]db8f44c2008-12-13 04:52:0168 }
69
[email protected]5389bc72009-11-05 23:34:2470 private:
[email protected]ba2f3342009-07-30 18:08:4271 virtual ~URLRequestTestContext() {
[email protected]ba2f3342009-07-30 18:08:4272 delete ftp_transaction_factory_;
[email protected]db8f44c2008-12-13 04:52:0173 delete http_transaction_factory_;
initial.commit586acc5fe2008-07-26 22:42:5274 }
75};
76
77class TestURLRequest : public URLRequest {
78 public:
[email protected]d1ec59082009-02-11 02:48:1579 TestURLRequest(const GURL& url, Delegate* delegate)
80 : URLRequest(url, delegate) {
[email protected]ba2f3342009-07-30 18:08:4281 set_context(new URLRequestTestContext());
[email protected]d1ec59082009-02-11 02:48:1582 }
initial.commit586acc5fe2008-07-26 22:42:5283};
84
[email protected]8a16266e2009-09-10 21:08:3985base::StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:5286 return "header";
87}
88
[email protected]71c64f62008-11-15 04:36:5189// Do a case-insensitive search through |haystack| for |needle|.
90bool ContainsString(const std::string& haystack, const char* needle) {
91 std::string::const_iterator it =
92 std::search(haystack.begin(),
93 haystack.end(),
94 needle,
95 needle + strlen(needle),
96 CaseInsensitiveCompare<char>());
97 return it != haystack.end();
98}
99
[email protected]661376a2009-04-29 02:04:23100void FillBuffer(char* buffer, size_t len) {
101 static bool called = false;
102 if (!called) {
103 called = true;
104 int seed = static_cast<int>(Time::Now().ToInternalValue());
105 srand(seed);
106 }
107
108 for (size_t i = 0; i < len; i++) {
109 buffer[i] = static_cast<char>(rand());
110 if (!buffer[i])
111 buffer[i] = 'g';
112 }
113}
114
[email protected]195e77d2009-07-23 19:10:23115scoped_refptr<net::UploadData> CreateSimpleUploadData(const char* data) {
116 scoped_refptr<net::UploadData> upload = new net::UploadData;
117 upload->AppendBytes(data, strlen(data));
118 return upload;
119}
120
[email protected]9396b252008-09-29 17:29:38121} // namespace
initial.commit586acc5fe2008-07-26 22:42:52122
[email protected]7a0bb4bf2008-11-19 21:41:48123// Inherit PlatformTest since we require the autorelease pool on Mac OS X.f
124class URLRequestTest : public PlatformTest {
125};
126
[email protected]b89290212009-08-14 22:37:35127class URLRequestTestHTTP : public URLRequestTest {
128 protected:
129 static void SetUpTestCase() {
[email protected]37314622009-08-17 20:29:39130 server_ = HTTPTestServer::CreateForkingServer(
131 L"net/data/url_request_unittest/");
[email protected]b89290212009-08-14 22:37:35132 }
133
134 static void TearDownTestCase() {
135 server_ = NULL;
136 }
137
138 static scoped_refptr<HTTPTestServer> server_;
139};
140
141// static
142scoped_refptr<HTTPTestServer> URLRequestTestHTTP::server_;
143
144TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]d1ec59082009-02-11 02:48:15145 // In this unit test, we're using the HTTPTestServer as a proxy server and
[email protected]dc651782009-02-14 01:45:08146 // issuing a CONNECT request with the magic host name "www.redirect.com".
147 // The HTTPTestServer will return a 302 response, which we should not
[email protected]d1ec59082009-02-11 02:48:15148 // follow.
[email protected]b89290212009-08-14 22:37:35149 ASSERT_TRUE(NULL != server_.get());
[email protected]d1ec59082009-02-11 02:48:15150 TestDelegate d;
151 {
152 URLRequest r(GURL("https://www.redirect.com/"), &d);
153 std::string proxy("localhost:");
154 proxy.append(IntToString(kHTTPDefaultPort));
155 r.set_context(new TestURLRequestContext(proxy));
156
157 r.Start();
158 EXPECT_TRUE(r.is_pending());
159
160 MessageLoop::current()->Run();
161
[email protected]c744cf22009-02-27 07:28:08162 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
163 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08164 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:15165 // We should not have followed the redirect.
166 EXPECT_EQ(0, d.received_redirect_count());
167 }
168}
169
[email protected]b89290212009-08-14 22:37:35170TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]dc651782009-02-14 01:45:08171 // In this unit test, we're using the HTTPTestServer as a proxy server and
172 // issuing a CONNECT request with the magic host name "www.server-auth.com".
173 // The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:35174 ASSERT_TRUE(NULL != server_.get());
[email protected]dc651782009-02-14 01:45:08175 TestDelegate d;
176 {
177 URLRequest r(GURL("https://www.server-auth.com/"), &d);
178 std::string proxy("localhost:");
179 proxy.append(IntToString(kHTTPDefaultPort));
180 r.set_context(new TestURLRequestContext(proxy));
181
182 r.Start();
183 EXPECT_TRUE(r.is_pending());
184
185 MessageLoop::current()->Run();
186
187 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]c744cf22009-02-27 07:28:08188 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08189 }
190}
191
[email protected]b89290212009-08-14 22:37:35192TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
193 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52194 TestDelegate d;
195 {
[email protected]b89290212009-08-14 22:37:35196 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52197
198 r.Start();
199 EXPECT_TRUE(r.is_pending());
200
201 MessageLoop::current()->Run();
202
203 EXPECT_EQ(1, d.response_started_count());
204 EXPECT_FALSE(d.received_data_before_response());
205 EXPECT_NE(0, d.bytes_received());
[email protected]c31a54592009-09-04 02:36:16206
[email protected]ab6a5c42009-11-13 00:25:29207 // The first part of the log will be for URL_REQUEST_START.
208 // After that, there should be an HTTP_TRANSACTION_READ_BODY
209 EXPECT_TRUE(
210 net::LogContains(*r.load_log(), 0,
211 net::LoadLog::TYPE_URL_REQUEST_START,
212 net::LoadLog::PHASE_BEGIN));
213 EXPECT_TRUE(
214 net::LogContains(*r.load_log(), -3,
215 net::LoadLog::TYPE_URL_REQUEST_START,
216 net::LoadLog::PHASE_END));
217 EXPECT_TRUE(
218 net::LogContains(*r.load_log(), -2,
219 net::LoadLog::TYPE_HTTP_TRANSACTION_READ_BODY,
220 net::LoadLog::PHASE_BEGIN));
221 EXPECT_TRUE(
222 net::LogContains(*r.load_log(), -1,
223 net::LoadLog::TYPE_HTTP_TRANSACTION_READ_BODY,
224 net::LoadLog::PHASE_END));
initial.commit586acc5fe2008-07-26 22:42:52225 }
initial.commit586acc5fe2008-07-26 22:42:52226}
227
[email protected]b89290212009-08-14 22:37:35228TEST_F(URLRequestTestHTTP, GetTest) {
229 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52230 TestDelegate d;
231 {
[email protected]b89290212009-08-14 22:37:35232 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52233
234 r.Start();
235 EXPECT_TRUE(r.is_pending());
236
237 MessageLoop::current()->Run();
238
239 EXPECT_EQ(1, d.response_started_count());
240 EXPECT_FALSE(d.received_data_before_response());
241 EXPECT_NE(0, d.bytes_received());
242 }
[email protected]5d7b373e2009-09-02 07:19:03243}
244
[email protected]73e0bba2009-02-19 22:57:09245TEST_F(URLRequestTest, QuitTest) {
[email protected]b89290212009-08-14 22:37:35246 // Don't use shared server here because we order it to quit.
247 // It would impact other tests.
[email protected]73e0bba2009-02-19 22:57:09248 scoped_refptr<HTTPTestServer> server =
249 HTTPTestServer::CreateServer(L"", NULL);
250 ASSERT_TRUE(NULL != server.get());
251 server->SendQuit();
252 EXPECT_TRUE(server->WaitToFinish(20000));
[email protected]73e0bba2009-02-19 22:57:09253}
254
255class HTTPSRequestTest : public testing::Test {
[email protected]ea224582008-12-07 20:25:46256};
257
258#if defined(OS_MACOSX)
[email protected]3016ded2009-06-19 18:27:59259// Status 6/19/09:
260//
261// If these tests are enabled on OSX, the first one (HTTPSGetTest)
262// will fail. I didn't track it down explicitly, but did observe that
263// the testserver.py kills itself (e.g. "process_util_posix.cc(84)]
264// Unable to terminate process."). tlslite and testserver.py are hard
265// to debug (redirection of stdout/stderr to a file so you can't see
266// errors; lots of naked "except:" statements, etc), but I did track
267// down an SSL auth failure as one cause of it deciding to die
268// silently.
269//
270// The next test, HTTPSMismatchedTest, will look like it hangs by
271// looping over calls to SSLHandshake() (Security framework call) as
272// called from SSLClientSocketMac::DoHandshake(). Return values are a
273// repeating pattern of -9803 (come back later) and -9812 (cert valid
274// but root not trusted). If you don't have the cert in your keychain
275// as documented on http://dev.chromium.org/developers/testing, the
276// -9812 becomes a -9813 (no root cert). Interestingly, the handshake
277// also appears to be a failure point for other disabled tests, such
278// as (SSLClientSocketTest,Connect) in
279// net/base/ssl_client_socket_unittest.cc.
280//
281// Old comment (not sure if obsolete):
[email protected]73e0bba2009-02-19 22:57:09282// ssl_client_socket_mac.cc crashes currently in GetSSLInfo
283// when called on a connection with an unrecognized certificate
[email protected]bacff652009-03-31 17:50:33284#define MAYBE_HTTPSGetTest DISABLED_HTTPSGetTest
285#define MAYBE_HTTPSMismatchedTest DISABLED_HTTPSMismatchedTest
286#define MAYBE_HTTPSExpiredTest DISABLED_HTTPSExpiredTest
[email protected]ea224582008-12-07 20:25:46287#else
[email protected]bacff652009-03-31 17:50:33288#define MAYBE_HTTPSGetTest HTTPSGetTest
289#define MAYBE_HTTPSMismatchedTest HTTPSMismatchedTest
290#define MAYBE_HTTPSExpiredTest HTTPSExpiredTest
[email protected]ea224582008-12-07 20:25:46291#endif
292
293TEST_F(HTTPSRequestTest, MAYBE_HTTPSGetTest) {
294 // Note: tools/testserver/testserver.py does not need
295 // a working document root to server the pages / and /hello.html,
296 // so this test doesn't really need to specify a document root.
297 // But if it did, a good one would be net/data/ssl.
[email protected]dd265012009-01-08 20:45:27298 scoped_refptr<HTTPSTestServer> server =
[email protected]73e0bba2009-02-19 22:57:09299 HTTPSTestServer::CreateGoodServer(L"net/data/ssl");
[email protected]dd265012009-01-08 20:45:27300 ASSERT_TRUE(NULL != server.get());
[email protected]ea224582008-12-07 20:25:46301
[email protected]ea224582008-12-07 20:25:46302 TestDelegate d;
303 {
[email protected]dd265012009-01-08 20:45:27304 TestURLRequest r(server->TestServerPage(""), &d);
[email protected]ea224582008-12-07 20:25:46305
306 r.Start();
307 EXPECT_TRUE(r.is_pending());
308
309 MessageLoop::current()->Run();
310
311 EXPECT_EQ(1, d.response_started_count());
312 EXPECT_FALSE(d.received_data_before_response());
313 EXPECT_NE(0, d.bytes_received());
314 }
[email protected]ea224582008-12-07 20:25:46315}
316
[email protected]bacff652009-03-31 17:50:33317TEST_F(HTTPSRequestTest, MAYBE_HTTPSMismatchedTest) {
318 scoped_refptr<HTTPSTestServer> server =
319 HTTPSTestServer::CreateMismatchedServer(L"net/data/ssl");
320 ASSERT_TRUE(NULL != server.get());
321
322 bool err_allowed = true;
323 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
324 TestDelegate d;
325 {
326 d.set_allow_certificate_errors(err_allowed);
327 TestURLRequest r(server->TestServerPage(""), &d);
328
329 r.Start();
330 EXPECT_TRUE(r.is_pending());
331
332 MessageLoop::current()->Run();
333
334 EXPECT_EQ(1, d.response_started_count());
335 EXPECT_FALSE(d.received_data_before_response());
336 EXPECT_TRUE(d.have_certificate_errors());
337 if (err_allowed)
338 EXPECT_NE(0, d.bytes_received());
339 else
340 EXPECT_EQ(0, d.bytes_received());
341 }
342 }
343}
344
345TEST_F(HTTPSRequestTest, MAYBE_HTTPSExpiredTest) {
346 scoped_refptr<HTTPSTestServer> server =
347 HTTPSTestServer::CreateExpiredServer(L"net/data/ssl");
348 ASSERT_TRUE(NULL != server.get());
349
350 // Iterate from false to true, just so that we do the opposite of the
351 // previous test in order to increase test coverage.
352 bool err_allowed = false;
353 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
354 TestDelegate d;
355 {
356 d.set_allow_certificate_errors(err_allowed);
357 TestURLRequest r(server->TestServerPage(""), &d);
358
359 r.Start();
360 EXPECT_TRUE(r.is_pending());
361
362 MessageLoop::current()->Run();
363
364 EXPECT_EQ(1, d.response_started_count());
365 EXPECT_FALSE(d.received_data_before_response());
366 EXPECT_TRUE(d.have_certificate_errors());
367 if (err_allowed)
368 EXPECT_NE(0, d.bytes_received());
369 else
370 EXPECT_EQ(0, d.bytes_received());
371 }
372 }
373}
[email protected]73e0bba2009-02-19 22:57:09374
[email protected]37314622009-08-17 20:29:39375TEST_F(URLRequestTestHTTP, CancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52376 TestDelegate d;
377 {
378 TestURLRequest r(GURL("http://www.google.com/"), &d);
379
380 r.Start();
381 EXPECT_TRUE(r.is_pending());
382
383 r.Cancel();
384
385 MessageLoop::current()->Run();
386
387 // We expect to receive OnResponseStarted even though the request has been
388 // cancelled.
389 EXPECT_EQ(1, d.response_started_count());
390 EXPECT_EQ(0, d.bytes_received());
391 EXPECT_FALSE(d.received_data_before_response());
392 }
initial.commit586acc5fe2008-07-26 22:42:52393}
394
[email protected]37314622009-08-17 20:29:39395TEST_F(URLRequestTestHTTP, CancelTest2) {
396 ASSERT_TRUE(NULL != server_.get());
[email protected]dd265012009-01-08 20:45:27397
398 // error C2446: '!=' : no conversion from 'HTTPTestServer *const '
399 // to 'const int'
400
initial.commit586acc5fe2008-07-26 22:42:52401 TestDelegate d;
402 {
[email protected]37314622009-08-17 20:29:39403 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52404
405 d.set_cancel_in_response_started(true);
406
407 r.Start();
408 EXPECT_TRUE(r.is_pending());
409
410 MessageLoop::current()->Run();
411
412 EXPECT_EQ(1, d.response_started_count());
413 EXPECT_EQ(0, d.bytes_received());
414 EXPECT_FALSE(d.received_data_before_response());
415 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
416 }
initial.commit586acc5fe2008-07-26 22:42:52417}
418
[email protected]37314622009-08-17 20:29:39419TEST_F(URLRequestTestHTTP, CancelTest3) {
420 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52421 TestDelegate d;
422 {
[email protected]37314622009-08-17 20:29:39423 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52424
425 d.set_cancel_in_received_data(true);
426
427 r.Start();
428 EXPECT_TRUE(r.is_pending());
429
430 MessageLoop::current()->Run();
431
432 EXPECT_EQ(1, d.response_started_count());
433 // There is no guarantee about how much data was received
434 // before the cancel was issued. It could have been 0 bytes,
435 // or it could have been all the bytes.
436 // EXPECT_EQ(0, d.bytes_received());
437 EXPECT_FALSE(d.received_data_before_response());
438 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
439 }
initial.commit586acc5fe2008-07-26 22:42:52440}
441
[email protected]37314622009-08-17 20:29:39442TEST_F(URLRequestTestHTTP, CancelTest4) {
443 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52444 TestDelegate d;
445 {
[email protected]37314622009-08-17 20:29:39446 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52447
448 r.Start();
449 EXPECT_TRUE(r.is_pending());
450
451 // The request will be implicitly canceled when it is destroyed. The
452 // test delegate must not post a quit message when this happens because
453 // this test doesn't actually have a message loop. The quit message would
454 // get put on this thread's message queue and the next test would exit
455 // early, causing problems.
456 d.set_quit_on_complete(false);
457 }
458 // expect things to just cleanup properly.
459
460 // we won't actually get a received reponse here because we've never run the
461 // message loop
462 EXPECT_FALSE(d.received_data_before_response());
463 EXPECT_EQ(0, d.bytes_received());
464}
465
[email protected]37314622009-08-17 20:29:39466TEST_F(URLRequestTestHTTP, CancelTest5) {
467 ASSERT_TRUE(NULL != server_.get());
[email protected]ba2f3342009-07-30 18:08:42468 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
initial.commit586acc5fe2008-07-26 22:42:52469
470 // populate cache
471 {
472 TestDelegate d;
[email protected]37314622009-08-17 20:29:39473 URLRequest r(server_->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52474 r.set_context(context);
475 r.Start();
476 MessageLoop::current()->Run();
477 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
478 }
479
480 // cancel read from cache (see bug 990242)
481 {
482 TestDelegate d;
[email protected]37314622009-08-17 20:29:39483 URLRequest r(server_->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52484 r.set_context(context);
485 r.Start();
486 r.Cancel();
487 MessageLoop::current()->Run();
488
489 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
490 EXPECT_EQ(1, d.response_started_count());
491 EXPECT_EQ(0, d.bytes_received());
492 EXPECT_FALSE(d.received_data_before_response());
493 }
initial.commit586acc5fe2008-07-26 22:42:52494}
495
[email protected]37314622009-08-17 20:29:39496TEST_F(URLRequestTestHTTP, PostTest) {
497 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52498 const int kMsgSize = 20000; // multiple of 10
499 const int kIterations = 50;
500 char *uploadBytes = new char[kMsgSize+1];
501 char *ptr = uploadBytes;
502 char marker = 'a';
[email protected]d1ec59082009-02-11 02:48:15503 for (int idx = 0; idx < kMsgSize/10; idx++) {
initial.commit586acc5fe2008-07-26 22:42:52504 memcpy(ptr, "----------", 10);
505 ptr += 10;
506 if (idx % 100 == 0) {
507 ptr--;
508 *ptr++ = marker;
509 if (++marker > 'z')
510 marker = 'a';
511 }
initial.commit586acc5fe2008-07-26 22:42:52512 }
513 uploadBytes[kMsgSize] = '\0';
514
[email protected]ba2f3342009-07-30 18:08:42515 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
initial.commit586acc5fe2008-07-26 22:42:52516
517 for (int i = 0; i < kIterations; ++i) {
518 TestDelegate d;
[email protected]37314622009-08-17 20:29:39519 URLRequest r(server_->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52520 r.set_context(context);
521 r.set_method("POST");
522
523 r.AppendBytesToUpload(uploadBytes, kMsgSize);
524
525 r.Start();
526 EXPECT_TRUE(r.is_pending());
527
528 MessageLoop::current()->Run();
529
530 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
531 (int) r.status().status() << ", os error: " << r.status().os_error();
532
533 EXPECT_FALSE(d.received_data_before_response());
534 EXPECT_EQ(uploadBytes, d.data_received());
[email protected]d1ec59082009-02-11 02:48:15535 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0);
initial.commit586acc5fe2008-07-26 22:42:52536 EXPECT_EQ(d.data_received().compare(uploadBytes), 0);
537 }
538 delete[] uploadBytes;
initial.commit586acc5fe2008-07-26 22:42:52539}
540
[email protected]37314622009-08-17 20:29:39541TEST_F(URLRequestTestHTTP, PostEmptyTest) {
542 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52543 TestDelegate d;
544 {
[email protected]37314622009-08-17 20:29:39545 TestURLRequest r(server_->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52546 r.set_method("POST");
547
548 r.Start();
549 EXPECT_TRUE(r.is_pending());
550
551 MessageLoop::current()->Run();
552
553 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
554 (int) r.status().status() << ", os error: " << r.status().os_error();
555
556 EXPECT_FALSE(d.received_data_before_response());
557 EXPECT_TRUE(d.data_received().empty());
558 }
initial.commit586acc5fe2008-07-26 22:42:52559}
560
[email protected]37314622009-08-17 20:29:39561TEST_F(URLRequestTestHTTP, PostFileTest) {
562 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52563 TestDelegate d;
564 {
[email protected]37314622009-08-17 20:29:39565 TestURLRequest r(server_->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52566 r.set_method("POST");
567
[email protected]399b8702009-05-01 20:34:02568 FilePath dir;
initial.commit586acc5fe2008-07-26 22:42:52569 PathService::Get(base::DIR_EXE, &dir);
[email protected]9396b252008-09-29 17:29:38570 file_util::SetCurrentDirectory(dir);
initial.commit586acc5fe2008-07-26 22:42:52571
[email protected]72cbd322009-04-07 10:17:12572 FilePath path;
initial.commit586acc5fe2008-07-26 22:42:52573 PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]72cbd322009-04-07 10:17:12574 path = path.Append(FILE_PATH_LITERAL("net"));
575 path = path.Append(FILE_PATH_LITERAL("data"));
576 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
577 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
initial.commit586acc5fe2008-07-26 22:42:52578 r.AppendFileToUpload(path);
579
580 // This file should just be ignored in the upload stream.
[email protected]72cbd322009-04-07 10:17:12581 r.AppendFileToUpload(FilePath(FILE_PATH_LITERAL(
582 "c:\\path\\to\\non\\existant\\file.randomness.12345")));
initial.commit586acc5fe2008-07-26 22:42:52583
584 r.Start();
585 EXPECT_TRUE(r.is_pending());
586
587 MessageLoop::current()->Run();
588
[email protected]10a1fe92008-11-04 21:47:02589 int64 longsize;
590 ASSERT_EQ(true, file_util::GetFileSize(path, &longsize));
591 int size = static_cast<int>(longsize);
initial.commit586acc5fe2008-07-26 22:42:52592 scoped_array<char> buf(new char[size]);
593
[email protected]dd265012009-01-08 20:45:27594 int size_read = static_cast<int>(file_util::ReadFile(path,
595 buf.get(), size));
[email protected]eac0709a2008-11-04 21:00:46596 ASSERT_EQ(size, size_read);
initial.commit586acc5fe2008-07-26 22:42:52597
598 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
599 (int) r.status().status() << ", os error: " << r.status().os_error();
600
601 EXPECT_FALSE(d.received_data_before_response());
602
603 ASSERT_EQ(size, d.bytes_received());
604 EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size));
605 }
initial.commit586acc5fe2008-07-26 22:42:52606}
607
[email protected]7a0bb4bf2008-11-19 21:41:48608TEST_F(URLRequestTest, AboutBlankTest) {
initial.commit586acc5fe2008-07-26 22:42:52609 TestDelegate d;
610 {
611 TestURLRequest r(GURL("about:blank"), &d);
612
613 r.Start();
614 EXPECT_TRUE(r.is_pending());
615
616 MessageLoop::current()->Run();
617
618 EXPECT_TRUE(!r.is_pending());
619 EXPECT_FALSE(d.received_data_before_response());
620 EXPECT_EQ(d.bytes_received(), 0);
621 }
initial.commit586acc5fe2008-07-26 22:42:52622}
623
[email protected]fcb7fd92009-10-22 04:18:58624TEST_F(URLRequestTest, DataURLImageTest) {
625 TestDelegate d;
626 {
627 // Use our nice little Chrome logo.
628 TestURLRequest r(GURL(
629 "data:image/png;base64,"
630 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
631 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
632 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
633 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
634 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
635 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
636 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
637 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
638 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
639 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
640 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
641 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
642 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
643 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
644 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
645 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
646 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
647 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
648 &d);
649
650 r.Start();
651 EXPECT_TRUE(r.is_pending());
652
653 MessageLoop::current()->Run();
654
655 EXPECT_TRUE(!r.is_pending());
656 EXPECT_FALSE(d.received_data_before_response());
657 EXPECT_EQ(d.bytes_received(), 911);
658 }
659}
660
[email protected]7a0bb4bf2008-11-19 21:41:48661TEST_F(URLRequestTest, FileTest) {
[email protected]b9e04f02008-11-27 04:03:57662 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52663 PathService::Get(base::FILE_EXE, &app_path);
[email protected]72cbd322009-04-07 10:17:12664 GURL app_url = net::FilePathToFileURL(app_path);
initial.commit586acc5fe2008-07-26 22:42:52665
666 TestDelegate d;
667 {
[email protected]b9e04f02008-11-27 04:03:57668 TestURLRequest r(app_url, &d);
initial.commit586acc5fe2008-07-26 22:42:52669
670 r.Start();
671 EXPECT_TRUE(r.is_pending());
672
673 MessageLoop::current()->Run();
674
[email protected]b77280c2009-03-20 17:27:47675 int64 file_size = -1;
676 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
initial.commit586acc5fe2008-07-26 22:42:52677
678 EXPECT_TRUE(!r.is_pending());
679 EXPECT_EQ(1, d.response_started_count());
680 EXPECT_FALSE(d.received_data_before_response());
[email protected]9396b252008-09-29 17:29:38681 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
initial.commit586acc5fe2008-07-26 22:42:52682 }
initial.commit586acc5fe2008-07-26 22:42:52683}
684
[email protected]661376a2009-04-29 02:04:23685TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
686 const size_t buffer_size = 4000;
687 scoped_array<char> buffer(new char[buffer_size]);
688 FillBuffer(buffer.get(), buffer_size);
689
690 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55691 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23692 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53693 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23694
695 int64 file_size;
696 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
697
698 const size_t first_byte_position = 500;
699 const size_t last_byte_position = buffer_size - first_byte_position;
700 const size_t content_length = last_byte_position - first_byte_position + 1;
701 std::string partial_buffer_string(buffer.get() + first_byte_position,
702 buffer.get() + last_byte_position + 1);
703
704 TestDelegate d;
705 {
706 TestURLRequest r(temp_url, &d);
707
[email protected]34b2b002009-11-20 06:53:28708 r.SetExtraRequestHeaders(
709 StringPrintf("Range: bytes=%" PRIuS "-%" PRIuS "\n",
710 first_byte_position, last_byte_position));
[email protected]661376a2009-04-29 02:04:23711 r.Start();
712 EXPECT_TRUE(r.is_pending());
713
714 MessageLoop::current()->Run();
715 EXPECT_TRUE(!r.is_pending());
716 EXPECT_EQ(1, d.response_started_count());
717 EXPECT_FALSE(d.received_data_before_response());
718 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
719 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
720 EXPECT_TRUE(partial_buffer_string == d.data_received());
721 }
722
723 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23724}
725
726TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
727 const size_t buffer_size = 4000;
728 scoped_array<char> buffer(new char[buffer_size]);
729 FillBuffer(buffer.get(), buffer_size);
730
731 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55732 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23733 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53734 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23735
736 int64 file_size;
737 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
738
739 const size_t first_byte_position = 500;
740 const size_t last_byte_position = buffer_size - 1;
741 const size_t content_length = last_byte_position - first_byte_position + 1;
742 std::string partial_buffer_string(buffer.get() + first_byte_position,
743 buffer.get() + last_byte_position + 1);
744
745 TestDelegate d;
746 {
747 TestURLRequest r(temp_url, &d);
748
[email protected]34b2b002009-11-20 06:53:28749 r.SetExtraRequestHeaders(StringPrintf("Range: bytes=%" PRIuS "-\n",
[email protected]661376a2009-04-29 02:04:23750 first_byte_position));
751 r.Start();
752 EXPECT_TRUE(r.is_pending());
753
754 MessageLoop::current()->Run();
755 EXPECT_TRUE(!r.is_pending());
756 EXPECT_EQ(1, d.response_started_count());
757 EXPECT_FALSE(d.received_data_before_response());
758 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
759 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
760 EXPECT_TRUE(partial_buffer_string == d.data_received());
761 }
762
763 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23764}
765
766TEST_F(URLRequestTest, FileTestMultipleRanges) {
767 const size_t buffer_size = 400000;
768 scoped_array<char> buffer(new char[buffer_size]);
769 FillBuffer(buffer.get(), buffer_size);
770
771 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55772 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23773 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53774 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23775
776 int64 file_size;
777 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
778
779 TestDelegate d;
780 {
781 TestURLRequest r(temp_url, &d);
782
783 r.SetExtraRequestHeaders(StringPrintf("Range: bytes=0-0,10-200,200-300\n"));
784 r.Start();
785 EXPECT_TRUE(r.is_pending());
786
787 MessageLoop::current()->Run();
788 EXPECT_TRUE(d.request_failed());
789 }
790
791 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23792}
793
[email protected]7a0bb4bf2008-11-19 21:41:48794TEST_F(URLRequestTest, InvalidUrlTest) {
initial.commit586acc5fe2008-07-26 22:42:52795 TestDelegate d;
796 {
797 TestURLRequest r(GURL("invalid url"), &d);
798
799 r.Start();
800 EXPECT_TRUE(r.is_pending());
801
802 MessageLoop::current()->Run();
803 EXPECT_TRUE(d.request_failed());
804 }
initial.commit586acc5fe2008-07-26 22:42:52805}
806
[email protected]6412d322008-11-21 23:49:30807// This test is disabled because it fails on some computers due to proxies
808// returning a page in response to this request rather than reporting failure.
809TEST_F(URLRequestTest, DISABLED_DnsFailureTest) {
initial.commit586acc5fe2008-07-26 22:42:52810 TestDelegate d;
811 {
812 URLRequest r(GURL("http://thisisnotavalidurl0123456789foo.com/"), &d);
813
814 r.Start();
815 EXPECT_TRUE(r.is_pending());
816
817 MessageLoop::current()->Run();
818 EXPECT_TRUE(d.request_failed());
819 }
initial.commit586acc5fe2008-07-26 22:42:52820}
initial.commit586acc5fe2008-07-26 22:42:52821
[email protected]37314622009-08-17 20:29:39822TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
823 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52824 TestDelegate d;
[email protected]37314622009-08-17 20:29:39825 TestURLRequest req(server_->TestServerPage("files/with-headers.html"), &d);
initial.commit586acc5fe2008-07-26 22:42:52826 req.Start();
827 MessageLoop::current()->Run();
828
829 const net::HttpResponseHeaders* headers = req.response_headers();
[email protected]589deddb2009-10-05 23:41:40830
831 // Simple sanity check that response_info() accesses the same data.
832 EXPECT_EQ(headers, req.response_info().headers.get());
833
initial.commit586acc5fe2008-07-26 22:42:52834 std::string header;
835 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
836 EXPECT_EQ("private", header);
837
838 header.clear();
839 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
840 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
841
842 // The response has two "X-Multiple-Entries" headers.
843 // This verfies our output has them concatenated together.
844 header.clear();
845 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
846 EXPECT_EQ("a, b", header);
847}
848
[email protected]a86c97cc2009-06-24 21:26:27849// TODO(jar): 14801 Remove BZIP code completely.
850TEST_F(URLRequestTest, DISABLED_BZip2ContentTest) {
[email protected]dd265012009-01-08 20:45:27851 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55852 HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL);
[email protected]dd265012009-01-08 20:45:27853 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52854
855 // for localhost domain, we also should support bzip2 encoding
856 // first, get the original file
857 TestDelegate d1;
[email protected]dd265012009-01-08 20:45:27858 TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1);
initial.commit586acc5fe2008-07-26 22:42:52859 req1.Start();
860 MessageLoop::current()->Run();
861
862 const std::string& got_content = d1.data_received();
863
864 // second, get bzip2 content
865 TestDelegate d2;
[email protected]dd265012009-01-08 20:45:27866 TestURLRequest req2(server->TestServerPage("realbz2files/google.txt"), &d2);
initial.commit586acc5fe2008-07-26 22:42:52867 req2.Start();
868 MessageLoop::current()->Run();
869
870 const std::string& got_bz2_content = d2.data_received();
871
872 // compare those two results
[email protected]24cfabd2008-08-15 00:05:39873 EXPECT_EQ(got_content, got_bz2_content);
initial.commit586acc5fe2008-07-26 22:42:52874}
875
[email protected]a86c97cc2009-06-24 21:26:27876// TODO(jar): 14801 Remove BZIP code completely.
877TEST_F(URLRequestTest, DISABLED_BZip2ContentTest_IncrementalHeader) {
[email protected]dd265012009-01-08 20:45:27878 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55879 HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL);
[email protected]dd265012009-01-08 20:45:27880 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52881
882 // for localhost domain, we also should support bzip2 encoding
883 // first, get the original file
884 TestDelegate d1;
[email protected]dd265012009-01-08 20:45:27885 TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1);
initial.commit586acc5fe2008-07-26 22:42:52886 req1.Start();
887 MessageLoop::current()->Run();
888
889 const std::string& got_content = d1.data_received();
890
891 // second, get bzip2 content. ask the testserver to send the BZ2 header in
892 // two chunks with a delay between them. this tests our fix for bug 867161.
893 TestDelegate d2;
[email protected]dd265012009-01-08 20:45:27894 TestURLRequest req2(server->TestServerPage(
895 "realbz2files/google.txt?incremental-header"), &d2);
initial.commit586acc5fe2008-07-26 22:42:52896 req2.Start();
897 MessageLoop::current()->Run();
898
899 const std::string& got_bz2_content = d2.data_received();
900
901 // compare those two results
[email protected]24cfabd2008-08-15 00:05:39902 EXPECT_EQ(got_content, got_bz2_content);
initial.commit586acc5fe2008-07-26 22:42:52903}
904
[email protected]9396b252008-09-29 17:29:38905#if defined(OS_WIN)
[email protected]7a0bb4bf2008-11-19 21:41:48906TEST_F(URLRequestTest, ResolveShortcutTest) {
[email protected]399b8702009-05-01 20:34:02907 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52908 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:02909 app_path = app_path.AppendASCII("net");
910 app_path = app_path.AppendASCII("data");
911 app_path = app_path.AppendASCII("url_request_unittest");
912 app_path = app_path.AppendASCII("with-headers.html");
initial.commit586acc5fe2008-07-26 22:42:52913
[email protected]399b8702009-05-01 20:34:02914 std::wstring lnk_path = app_path.value() + L".lnk";
initial.commit586acc5fe2008-07-26 22:42:52915
916 HRESULT result;
917 IShellLink *shell = NULL;
918 IPersistFile *persist = NULL;
919
920 CoInitialize(NULL);
921 // Temporarily create a shortcut for test
922 result = CoCreateInstance(CLSID_ShellLink, NULL,
[email protected]5d7b373e2009-09-02 07:19:03923 CLSCTX_INPROC_SERVER, IID_IShellLink,
924 reinterpret_cast<LPVOID*>(&shell));
[email protected]d149ce82009-07-01 23:57:02925 ASSERT_TRUE(SUCCEEDED(result));
initial.commit586acc5fe2008-07-26 22:42:52926 result = shell->QueryInterface(IID_IPersistFile,
[email protected]5d7b373e2009-09-02 07:19:03927 reinterpret_cast<LPVOID*>(&persist));
[email protected]d149ce82009-07-01 23:57:02928 ASSERT_TRUE(SUCCEEDED(result));
[email protected]399b8702009-05-01 20:34:02929 result = shell->SetPath(app_path.value().c_str());
initial.commit586acc5fe2008-07-26 22:42:52930 EXPECT_TRUE(SUCCEEDED(result));
931 result = shell->SetDescription(L"ResolveShortcutTest");
932 EXPECT_TRUE(SUCCEEDED(result));
933 result = persist->Save(lnk_path.c_str(), TRUE);
934 EXPECT_TRUE(SUCCEEDED(result));
935 if (persist)
936 persist->Release();
937 if (shell)
938 shell->Release();
939
940 TestDelegate d;
941 {
[email protected]72cbd322009-04-07 10:17:12942 TestURLRequest r(net::FilePathToFileURL(FilePath(lnk_path)), &d);
initial.commit586acc5fe2008-07-26 22:42:52943
944 r.Start();
945 EXPECT_TRUE(r.is_pending());
946
947 MessageLoop::current()->Run();
948
949 WIN32_FILE_ATTRIBUTE_DATA data;
[email protected]399b8702009-05-01 20:34:02950 GetFileAttributesEx(app_path.value().c_str(),
951 GetFileExInfoStandard, &data);
952 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
initial.commit586acc5fe2008-07-26 22:42:52953 FILE_SHARE_READ, NULL, OPEN_EXISTING,
954 FILE_ATTRIBUTE_NORMAL, NULL);
955 EXPECT_NE(INVALID_HANDLE_VALUE, file);
956 scoped_array<char> buffer(new char[data.nFileSizeLow]);
957 DWORD read_size;
958 BOOL result;
959 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
960 &read_size, NULL);
961 std::string content(buffer.get(), read_size);
962 CloseHandle(file);
963
964 EXPECT_TRUE(!r.is_pending());
965 EXPECT_EQ(1, d.received_redirect_count());
966 EXPECT_EQ(content, d.data_received());
967 }
968
969 // Clean the shortcut
970 DeleteFile(lnk_path.c_str());
971 CoUninitialize();
initial.commit586acc5fe2008-07-26 22:42:52972}
[email protected]9396b252008-09-29 17:29:38973#endif // defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:52974
[email protected]37314622009-08-17 20:29:39975TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
976 ASSERT_TRUE(NULL != server_.get());
[email protected]dd265012009-01-08 20:45:27977
initial.commit586acc5fe2008-07-26 22:42:52978 TestDelegate d;
[email protected]37314622009-08-17 20:29:39979 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52980 "files/content-type-normalization.html"), &d);
981 req.Start();
982 MessageLoop::current()->Run();
983
984 std::string mime_type;
985 req.GetMimeType(&mime_type);
986 EXPECT_EQ("text/html", mime_type);
987
988 std::string charset;
989 req.GetCharset(&charset);
990 EXPECT_EQ("utf-8", charset);
991 req.Cancel();
992}
993
[email protected]7a0bb4bf2008-11-19 21:41:48994TEST_F(URLRequestTest, FileDirCancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52995 // Put in mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37996 net::NetModule::SetResourceProvider(TestNetResourceProvider);
initial.commit586acc5fe2008-07-26 22:42:52997
998 TestDelegate d;
999 {
[email protected]72cbd322009-04-07 10:17:121000 FilePath file_path;
initial.commit586acc5fe2008-07-26 22:42:521001 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
[email protected]72cbd322009-04-07 10:17:121002 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
1003 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
initial.commit586acc5fe2008-07-26 22:42:521004
[email protected]8ac1a752008-07-31 19:40:371005 TestURLRequest req(net::FilePathToFileURL(file_path), &d);
initial.commit586acc5fe2008-07-26 22:42:521006 req.Start();
1007 EXPECT_TRUE(req.is_pending());
1008
1009 d.set_cancel_in_received_data_pending(true);
1010
1011 MessageLoop::current()->Run();
1012 }
initial.commit586acc5fe2008-07-26 22:42:521013
1014 // Take out mock resource provider.
[email protected]8ac1a752008-07-31 19:40:371015 net::NetModule::SetResourceProvider(NULL);
initial.commit586acc5fe2008-07-26 22:42:521016}
1017
[email protected]7886a8c2009-08-21 04:11:091018TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
1019 // There is an implicit redirect when loading a file path that matches a
1020 // directory and does not end with a slash. Ensure that following such
1021 // redirects does not crash. See http://crbug.com/18686.
1022
1023 FilePath path;
1024 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1025 path = path.Append(FILE_PATH_LITERAL("net"));
1026 path = path.Append(FILE_PATH_LITERAL("data"));
1027 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1028
1029 TestDelegate d;
1030 d.set_quit_on_redirect(true);
1031 TestURLRequest req(net::FilePathToFileURL(path), &d);
1032 req.Start();
1033 MessageLoop::current()->Run();
1034
1035 // Let the directory lister have time to finish its work, which will
1036 // cause the URLRequestFileDirJob's ref count to drop to 1.
1037 URLRequestFileDirJob* job = static_cast<URLRequestFileDirJob*>(req.job());
1038 while (!job->list_complete()) {
1039 PlatformThread::Sleep(10);
1040 MessageLoop::current()->RunAllPending();
1041 }
1042
1043 // Should not crash during this call!
1044 req.FollowDeferredRedirect();
1045
1046 // Flush event queue.
1047 MessageLoop::current()->RunAllPending();
1048}
1049
[email protected]37314622009-08-17 20:29:391050TEST_F(URLRequestTestHTTP, RestrictRedirects) {
1051 ASSERT_TRUE(NULL != server_.get());
[email protected]dd265012009-01-08 20:45:271052
initial.commit586acc5fe2008-07-26 22:42:521053 TestDelegate d;
[email protected]37314622009-08-17 20:29:391054 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:521055 "files/redirect-to-file.html"), &d);
1056 req.Start();
1057 MessageLoop::current()->Run();
1058
1059 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1060 EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error());
1061}
1062
[email protected]37314622009-08-17 20:29:391063TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
1064 ASSERT_TRUE(NULL != server_.get());
[email protected]4e66ed12009-07-21 23:38:421065
1066 TestDelegate d;
[email protected]37314622009-08-17 20:29:391067 TestURLRequest req(server_->TestServerPage(
[email protected]4e66ed12009-07-21 23:38:421068 "files/redirect-to-invalid-url.html"), &d);
1069 req.Start();
1070 MessageLoop::current()->Run();
1071
1072 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1073 EXPECT_EQ(net::ERR_INVALID_URL, req.status().os_error());
1074}
1075
[email protected]37314622009-08-17 20:29:391076TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
1077 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521078 TestDelegate d;
[email protected]37314622009-08-17 20:29:391079 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:521080 "echoheader?Referer"), &d);
1081 req.set_referrer("http://user:[email protected]/");
1082 req.Start();
1083 MessageLoop::current()->Run();
1084
1085 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
1086}
1087
[email protected]37314622009-08-17 20:29:391088TEST_F(URLRequestTestHTTP, CancelRedirect) {
1089 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521090 TestDelegate d;
1091 {
1092 d.set_cancel_in_received_redirect(true);
[email protected]37314622009-08-17 20:29:391093 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:521094 "files/redirect-test.html"), &d);
1095 req.Start();
1096 MessageLoop::current()->Run();
1097
1098 EXPECT_EQ(1, d.response_started_count());
1099 EXPECT_EQ(0, d.bytes_received());
1100 EXPECT_FALSE(d.received_data_before_response());
1101 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1102 }
1103}
1104
[email protected]37314622009-08-17 20:29:391105TEST_F(URLRequestTestHTTP, DeferredRedirect) {
1106 ASSERT_TRUE(NULL != server_.get());
[email protected]195e77d2009-07-23 19:10:231107 TestDelegate d;
1108 {
1109 d.set_quit_on_redirect(true);
[email protected]37314622009-08-17 20:29:391110 TestURLRequest req(server_->TestServerPage(
[email protected]195e77d2009-07-23 19:10:231111 "files/redirect-test.html"), &d);
1112 req.Start();
1113 MessageLoop::current()->Run();
1114
1115 EXPECT_EQ(1, d.received_redirect_count());
1116
1117 req.FollowDeferredRedirect();
1118 MessageLoop::current()->Run();
1119
1120 EXPECT_EQ(1, d.response_started_count());
1121 EXPECT_FALSE(d.received_data_before_response());
1122 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
1123
1124 FilePath path;
1125 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1126 path = path.Append(FILE_PATH_LITERAL("net"));
1127 path = path.Append(FILE_PATH_LITERAL("data"));
1128 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1129 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
1130
1131 std::string contents;
1132 EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
1133 EXPECT_EQ(contents, d.data_received());
1134 }
1135}
1136
[email protected]37314622009-08-17 20:29:391137TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
1138 ASSERT_TRUE(NULL != server_.get());
[email protected]195e77d2009-07-23 19:10:231139 TestDelegate d;
1140 {
1141 d.set_quit_on_redirect(true);
[email protected]37314622009-08-17 20:29:391142 TestURLRequest req(server_->TestServerPage(
[email protected]195e77d2009-07-23 19:10:231143 "files/redirect-test.html"), &d);
1144 req.Start();
1145 MessageLoop::current()->Run();
1146
1147 EXPECT_EQ(1, d.received_redirect_count());
1148
1149 req.Cancel();
1150 MessageLoop::current()->Run();
1151
1152 EXPECT_EQ(1, d.response_started_count());
1153 EXPECT_EQ(0, d.bytes_received());
1154 EXPECT_FALSE(d.received_data_before_response());
1155 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1156 }
1157}
1158
[email protected]37314622009-08-17 20:29:391159TEST_F(URLRequestTestHTTP, VaryHeader) {
1160 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521161
[email protected]ba2f3342009-07-30 18:08:421162 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
initial.commit586acc5fe2008-07-26 22:42:521163
initial.commit586acc5fe2008-07-26 22:42:521164 // populate the cache
1165 {
1166 TestDelegate d;
[email protected]37314622009-08-17 20:29:391167 URLRequest req(server_->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521168 req.set_context(context);
1169 req.SetExtraRequestHeaders("foo:1");
1170 req.Start();
1171 MessageLoop::current()->Run();
initial.commit586acc5fe2008-07-26 22:42:521172 }
1173
initial.commit586acc5fe2008-07-26 22:42:521174 // expect a cache hit
1175 {
1176 TestDelegate d;
[email protected]37314622009-08-17 20:29:391177 URLRequest req(server_->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521178 req.set_context(context);
1179 req.SetExtraRequestHeaders("foo:1");
1180 req.Start();
1181 MessageLoop::current()->Run();
1182
[email protected]cb4ff9d2009-09-04 22:51:531183 EXPECT_TRUE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521184 }
1185
1186 // expect a cache miss
1187 {
1188 TestDelegate d;
[email protected]37314622009-08-17 20:29:391189 URLRequest req(server_->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521190 req.set_context(context);
1191 req.SetExtraRequestHeaders("foo:2");
1192 req.Start();
1193 MessageLoop::current()->Run();
1194
[email protected]cb4ff9d2009-09-04 22:51:531195 EXPECT_FALSE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521196 }
1197}
1198
[email protected]cb4ff9d2009-09-04 22:51:531199TEST_F(URLRequestTestHTTP, BasicAuth) {
[email protected]ba2f3342009-07-30 18:08:421200 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
[email protected]37314622009-08-17 20:29:391201 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521202
initial.commit586acc5fe2008-07-26 22:42:521203 // populate the cache
1204 {
1205 TestDelegate d;
1206 d.set_username(L"user");
1207 d.set_password(L"secret");
1208
[email protected]37314622009-08-17 20:29:391209 URLRequest r(server_->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:521210 r.set_context(context);
1211 r.Start();
1212
1213 MessageLoop::current()->Run();
1214
1215 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
initial.commit586acc5fe2008-07-26 22:42:521216 }
1217
initial.commit586acc5fe2008-07-26 22:42:521218 // repeat request with end-to-end validation. since auth-basic results in a
1219 // cachable page, we expect this test to result in a 304. in which case, the
1220 // response should be fetched from the cache.
1221 {
1222 TestDelegate d;
1223 d.set_username(L"user");
1224 d.set_password(L"secret");
1225
[email protected]37314622009-08-17 20:29:391226 URLRequest r(server_->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:521227 r.set_context(context);
1228 r.set_load_flags(net::LOAD_VALIDATE_CACHE);
1229 r.Start();
1230
1231 MessageLoop::current()->Run();
1232
1233 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1234
[email protected]cb4ff9d2009-09-04 22:51:531235 // Should be the same cached document.
1236 EXPECT_TRUE(r.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521237 }
1238}
license.botbf09a502008-08-24 00:55:551239
[email protected]0757e7702009-03-27 04:00:221240// Check that Set-Cookie headers in 401 responses are respected.
1241// http://crbug.com/6450
[email protected]37314622009-08-17 20:29:391242TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
1243 ASSERT_TRUE(NULL != server_.get());
[email protected]0757e7702009-03-27 04:00:221244
1245 GURL url_requiring_auth =
[email protected]37314622009-08-17 20:29:391246 server_->TestServerPage("auth-basic?set-cookie-if-challenged");
[email protected]0757e7702009-03-27 04:00:221247
1248 // Request a page that will give a 401 containing a Set-Cookie header.
1249 // Verify that when the transaction is restarted, it includes the new cookie.
1250 {
[email protected]ba2f3342009-07-30 18:08:421251 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
[email protected]0757e7702009-03-27 04:00:221252 TestDelegate d;
1253 d.set_username(L"user");
1254 d.set_password(L"secret");
1255
1256 URLRequest r(url_requiring_auth, &d);
1257 r.set_context(context);
1258 r.Start();
1259
1260 MessageLoop::current()->Run();
1261
1262 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1263
1264 // Make sure we sent the cookie in the restarted transaction.
1265 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1266 != std::string::npos);
1267 }
1268
1269 // Same test as above, except this time the restart is initiated earlier
1270 // (without user intervention since identity is embedded in the URL).
1271 {
[email protected]ba2f3342009-07-30 18:08:421272 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
[email protected]0757e7702009-03-27 04:00:221273 TestDelegate d;
1274
1275 GURL::Replacements replacements;
1276 std::string username("user2");
1277 std::string password("secret");
1278 replacements.SetUsernameStr(username);
1279 replacements.SetPasswordStr(password);
1280 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
1281
1282 URLRequest r(url_with_identity, &d);
1283 r.set_context(context);
1284 r.Start();
1285
1286 MessageLoop::current()->Run();
1287
1288 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
1289
1290 // Make sure we sent the cookie in the restarted transaction.
1291 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1292 != std::string::npos);
1293 }
1294}
1295
[email protected]861fcd52009-08-26 02:33:461296TEST_F(URLRequestTest, DoNotSendCookies) {
1297 scoped_refptr<HTTPTestServer> server =
1298 HTTPTestServer::CreateServer(L"", NULL);
1299 ASSERT_TRUE(NULL != server.get());
1300 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
1301
1302 // Set up a cookie.
1303 {
1304 TestDelegate d;
1305 URLRequest req(server->TestServerPage("set-cookie?CookieToNotSend=1"), &d);
1306 req.set_context(context);
1307 req.Start();
1308 MessageLoop::current()->Run();
1309 }
1310
1311 // Verify that the cookie is set.
1312 {
1313 TestDelegate d;
1314 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1315 req.set_context(context);
1316 req.Start();
1317 MessageLoop::current()->Run();
1318
1319 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1320 != std::string::npos);
1321 }
1322
1323 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
1324 {
1325 TestDelegate d;
1326 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1327 req.set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES);
1328 req.set_context(context);
1329 req.Start();
1330 MessageLoop::current()->Run();
1331
1332 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1333 == std::string::npos);
1334 }
1335}
1336
1337TEST_F(URLRequestTest, DoNotSaveCookies) {
1338 scoped_refptr<HTTPTestServer> server =
1339 HTTPTestServer::CreateServer(L"", NULL);
1340 ASSERT_TRUE(NULL != server.get());
1341 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
1342
1343 // Set up a cookie.
1344 {
1345 TestDelegate d;
1346 URLRequest req(server->TestServerPage("set-cookie?CookieToNotUpdate=2"),
1347 &d);
1348 req.set_context(context);
1349 req.Start();
1350 MessageLoop::current()->Run();
1351 }
1352
1353 // Try to set-up another cookie and update the previous cookie.
1354 {
1355 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
1356 TestDelegate d;
1357 URLRequest req(server->TestServerPage(
1358 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
1359 req.set_load_flags(net::LOAD_DO_NOT_SAVE_COOKIES);
1360 req.set_context(context);
1361 req.Start();
1362
1363 MessageLoop::current()->Run();
1364 }
1365
1366 // Verify the cookies weren't saved or updated.
1367 {
1368 TestDelegate d;
1369 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1370 req.set_context(context);
1371 req.Start();
1372 MessageLoop::current()->Run();
1373
1374 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1375 == std::string::npos);
1376 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1377 != std::string::npos);
1378 }
1379}
1380
[email protected]71c64f62008-11-15 04:36:511381// In this test, we do a POST which the server will 302 redirect.
1382// The subsequent transaction should use GET, and should not send the
1383// Content-Type header.
1384// http://code.google.com/p/chromium/issues/detail?id=843
[email protected]37314622009-08-17 20:29:391385TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
[email protected]195e77d2009-07-23 19:10:231386 const char kData[] = "hello world";
[email protected]37314622009-08-17 20:29:391387 ASSERT_TRUE(NULL != server_.get());
[email protected]71c64f62008-11-15 04:36:511388 TestDelegate d;
[email protected]37314622009-08-17 20:29:391389 TestURLRequest req(server_->TestServerPage("files/redirect-to-echoall"), &d);
[email protected]71c64f62008-11-15 04:36:511390 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:231391 req.set_upload(CreateSimpleUploadData(kData));
[email protected]71c64f62008-11-15 04:36:511392
1393 // Set headers (some of which are specific to the POST).
[email protected]71c64f62008-11-15 04:36:511394 req.SetExtraRequestHeaders(
[email protected]dd265012009-01-08 20:45:271395 "Content-Type: multipart/form-data; "
1396 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
1397 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
1398 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
[email protected]71c64f62008-11-15 04:36:511399 "Accept-Language: en-US,en\r\n"
1400 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
[email protected]195e77d2009-07-23 19:10:231401 "Content-Length: 11\r\n"
[email protected]a86c97cc2009-06-24 21:26:271402 "Origin: http://localhost:1337/");
[email protected]71c64f62008-11-15 04:36:511403 req.Start();
1404 MessageLoop::current()->Run();
1405
1406 std::string mime_type;
1407 req.GetMimeType(&mime_type);
1408 EXPECT_EQ("text/html", mime_type);
1409
1410 const std::string& data = d.data_received();
1411
1412 // Check that the post-specific headers were stripped:
1413 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
1414 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
1415 EXPECT_FALSE(ContainsString(data, "Origin:"));
1416
1417 // These extra request headers should not have been stripped.
1418 EXPECT_TRUE(ContainsString(data, "Accept:"));
1419 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
1420 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
1421}
1422
[email protected]37314622009-08-17 20:29:391423TEST_F(URLRequestTestHTTP, Post307RedirectPost) {
[email protected]195e77d2009-07-23 19:10:231424 const char kData[] = "hello world";
[email protected]37314622009-08-17 20:29:391425 ASSERT_TRUE(NULL != server_.get());
[email protected]140932f2008-12-12 03:58:061426 TestDelegate d;
[email protected]37314622009-08-17 20:29:391427 TestURLRequest req(server_->TestServerPage("files/redirect307-to-echo"),
[email protected]dd265012009-01-08 20:45:271428 &d);
[email protected]140932f2008-12-12 03:58:061429 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:231430 req.set_upload(CreateSimpleUploadData(kData).get());
1431 req.SetExtraRequestHeaders(
1432 "Content-Length: " + UintToString(sizeof(kData) - 1));
[email protected]140932f2008-12-12 03:58:061433 req.Start();
1434 MessageLoop::current()->Run();
[email protected]195e77d2009-07-23 19:10:231435 EXPECT_EQ("POST", req.method());
1436 EXPECT_EQ(kData, d.data_received());
[email protected]140932f2008-12-12 03:58:061437}
[email protected]dd265012009-01-08 20:45:271438
[email protected]a5c713f2009-04-16 21:05:471439// Custom URLRequestJobs for use with interceptor tests
1440class RestartTestJob : public URLRequestTestJob {
1441 public:
[email protected]a86c97cc2009-06-24 21:26:271442 explicit RestartTestJob(URLRequest* request)
1443 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:471444 protected:
1445 virtual void StartAsync() {
1446 this->NotifyRestartRequired();
1447 }
[email protected]5389bc72009-11-05 23:34:241448 private:
1449 ~RestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471450};
1451
1452class CancelTestJob : public URLRequestTestJob {
1453 public:
[email protected]a86c97cc2009-06-24 21:26:271454 explicit CancelTestJob(URLRequest* request)
1455 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:471456 protected:
1457 virtual void StartAsync() {
1458 request_->Cancel();
1459 }
[email protected]5389bc72009-11-05 23:34:241460 private:
1461 ~CancelTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471462};
1463
1464class CancelThenRestartTestJob : public URLRequestTestJob {
1465 public:
[email protected]a86c97cc2009-06-24 21:26:271466 explicit CancelThenRestartTestJob(URLRequest* request)
[email protected]a5c713f2009-04-16 21:05:471467 : URLRequestTestJob(request, true) {
1468 }
1469 protected:
1470 virtual void StartAsync() {
1471 request_->Cancel();
1472 this->NotifyRestartRequired();
1473 }
[email protected]5389bc72009-11-05 23:34:241474 private:
1475 ~CancelThenRestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471476};
1477
1478// An Interceptor for use with interceptor tests
1479class TestInterceptor : URLRequest::Interceptor {
1480 public:
1481 TestInterceptor()
1482 : intercept_main_request_(false), restart_main_request_(false),
1483 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1484 simulate_main_network_error_(false),
1485 intercept_redirect_(false), cancel_redirect_request_(false),
1486 intercept_final_response_(false), cancel_final_request_(false),
1487 did_intercept_main_(false), did_restart_main_(false),
1488 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1489 did_simulate_error_main_(false),
1490 did_intercept_redirect_(false), did_cancel_redirect_(false),
1491 did_intercept_final_(false), did_cancel_final_(false) {
1492 URLRequest::RegisterRequestInterceptor(this);
1493 }
1494
1495 ~TestInterceptor() {
1496 URLRequest::UnregisterRequestInterceptor(this);
1497 }
1498
1499 virtual URLRequestJob* MaybeIntercept(URLRequest* request) {
1500 if (restart_main_request_) {
1501 restart_main_request_ = false;
1502 did_restart_main_ = true;
1503 return new RestartTestJob(request);
1504 }
1505 if (cancel_main_request_) {
1506 cancel_main_request_ = false;
1507 did_cancel_main_ = true;
1508 return new CancelTestJob(request);
1509 }
1510 if (cancel_then_restart_main_request_) {
1511 cancel_then_restart_main_request_ = false;
1512 did_cancel_then_restart_main_ = true;
1513 return new CancelThenRestartTestJob(request);
1514 }
1515 if (simulate_main_network_error_) {
1516 simulate_main_network_error_ = false;
1517 did_simulate_error_main_ = true;
1518 // will error since the requeted url is not one of its canned urls
1519 return new URLRequestTestJob(request, true);
1520 }
1521 if (!intercept_main_request_)
1522 return NULL;
1523 intercept_main_request_ = false;
1524 did_intercept_main_ = true;
1525 return new URLRequestTestJob(request,
1526 main_headers_,
1527 main_data_,
1528 true);
1529 }
1530
1531 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
1532 const GURL& location) {
1533 if (cancel_redirect_request_) {
1534 cancel_redirect_request_ = false;
1535 did_cancel_redirect_ = true;
1536 return new CancelTestJob(request);
1537 }
1538 if (!intercept_redirect_)
1539 return NULL;
1540 intercept_redirect_ = false;
1541 did_intercept_redirect_ = true;
1542 return new URLRequestTestJob(request,
1543 redirect_headers_,
1544 redirect_data_,
1545 true);
1546 }
1547
1548 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) {
1549 if (cancel_final_request_) {
1550 cancel_final_request_ = false;
1551 did_cancel_final_ = true;
1552 return new CancelTestJob(request);
1553 }
1554 if (!intercept_final_response_)
1555 return NULL;
1556 intercept_final_response_ = false;
1557 did_intercept_final_ = true;
1558 return new URLRequestTestJob(request,
1559 final_headers_,
1560 final_data_,
1561 true);
1562 }
1563
1564 // Whether to intercept the main request, and if so the response to return.
1565 bool intercept_main_request_;
1566 std::string main_headers_;
1567 std::string main_data_;
1568
1569 // Other actions we take at MaybeIntercept time
1570 bool restart_main_request_;
1571 bool cancel_main_request_;
1572 bool cancel_then_restart_main_request_;
1573 bool simulate_main_network_error_;
1574
1575 // Whether to intercept redirects, and if so the response to return.
1576 bool intercept_redirect_;
1577 std::string redirect_headers_;
1578 std::string redirect_data_;
1579
1580 // Other actions we can take at MaybeInterceptRedirect time
1581 bool cancel_redirect_request_;
1582
1583 // Whether to intercept final response, and if so the response to return.
1584 bool intercept_final_response_;
1585 std::string final_headers_;
1586 std::string final_data_;
1587
1588 // Other actions we can take at MaybeInterceptResponse time
1589 bool cancel_final_request_;
1590
1591 // If we did something or not
1592 bool did_intercept_main_;
1593 bool did_restart_main_;
1594 bool did_cancel_main_;
1595 bool did_cancel_then_restart_main_;
1596 bool did_simulate_error_main_;
1597 bool did_intercept_redirect_;
1598 bool did_cancel_redirect_;
1599 bool did_intercept_final_;
1600 bool did_cancel_final_;
1601
1602 // Static getters for canned response header and data strings
1603
1604 static std::string ok_data() {
1605 return URLRequestTestJob::test_data_1();
1606 }
1607
1608 static std::string ok_headers() {
1609 return URLRequestTestJob::test_headers();
1610 }
1611
1612 static std::string redirect_data() {
1613 return std::string();
1614 }
1615
1616 static std::string redirect_headers() {
1617 return URLRequestTestJob::test_redirect_headers();
1618 }
1619
1620 static std::string error_data() {
1621 return std::string("ohhh nooooo mr. bill!");
1622 }
1623
1624 static std::string error_headers() {
1625 return URLRequestTestJob::test_error_headers();
1626 }
1627};
1628
1629TEST_F(URLRequestTest, Intercept) {
1630 TestInterceptor interceptor;
1631
1632 // intercept the main request and respond with a simple response
1633 interceptor.intercept_main_request_ = true;
1634 interceptor.main_headers_ = TestInterceptor::ok_headers();
1635 interceptor.main_data_ = TestInterceptor::ok_data();
1636
1637 TestDelegate d;
1638 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1639 URLRequest::UserData* user_data0 = new URLRequest::UserData();
1640 URLRequest::UserData* user_data1 = new URLRequest::UserData();
1641 URLRequest::UserData* user_data2 = new URLRequest::UserData();
1642 req.SetUserData(NULL, user_data0);
1643 req.SetUserData(&user_data1, user_data1);
1644 req.SetUserData(&user_data2, user_data2);
1645 req.set_method("GET");
1646 req.Start();
1647 MessageLoop::current()->Run();
1648
1649 // Make sure we can retrieve our specific user data
1650 EXPECT_EQ(user_data0, req.GetUserData(NULL));
1651 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
1652 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
1653
1654 // Check the interceptor got called as expected
1655 EXPECT_TRUE(interceptor.did_intercept_main_);
1656
1657 // Check we got one good response
1658 EXPECT_TRUE(req.status().is_success());
1659 EXPECT_EQ(200, req.response_headers()->response_code());
1660 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1661 EXPECT_EQ(1, d.response_started_count());
1662 EXPECT_EQ(0, d.received_redirect_count());
1663}
1664
1665TEST_F(URLRequestTest, InterceptRedirect) {
1666 TestInterceptor interceptor;
1667
1668 // intercept the main request and respond with a redirect
1669 interceptor.intercept_main_request_ = true;
1670 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1671 interceptor.main_data_ = TestInterceptor::redirect_data();
1672
1673 // intercept that redirect and respond a final OK response
1674 interceptor.intercept_redirect_ = true;
1675 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1676 interceptor.redirect_data_ = TestInterceptor::ok_data();
1677
1678 TestDelegate d;
1679 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1680 req.set_method("GET");
1681 req.Start();
1682 MessageLoop::current()->Run();
1683
1684 // Check the interceptor got called as expected
1685 EXPECT_TRUE(interceptor.did_intercept_main_);
1686 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1687
1688 // Check we got one good response
1689 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:111690 if (req.status().is_success()) {
1691 EXPECT_EQ(200, req.response_headers()->response_code());
1692 }
[email protected]a5c713f2009-04-16 21:05:471693 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1694 EXPECT_EQ(1, d.response_started_count());
1695 EXPECT_EQ(0, d.received_redirect_count());
1696}
1697
1698TEST_F(URLRequestTest, InterceptServerError) {
1699 TestInterceptor interceptor;
1700
1701 // intercept the main request to generate a server error response
1702 interceptor.intercept_main_request_ = true;
1703 interceptor.main_headers_ = TestInterceptor::error_headers();
1704 interceptor.main_data_ = TestInterceptor::error_data();
1705
1706 // intercept that error and respond with an OK response
1707 interceptor.intercept_final_response_ = true;
1708 interceptor.final_headers_ = TestInterceptor::ok_headers();
1709 interceptor.final_data_ = TestInterceptor::ok_data();
1710
1711 TestDelegate d;
1712 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1713 req.set_method("GET");
1714 req.Start();
1715 MessageLoop::current()->Run();
1716
1717 // Check the interceptor got called as expected
1718 EXPECT_TRUE(interceptor.did_intercept_main_);
1719 EXPECT_TRUE(interceptor.did_intercept_final_);
1720
1721 // Check we got one good response
1722 EXPECT_TRUE(req.status().is_success());
1723 EXPECT_EQ(200, req.response_headers()->response_code());
1724 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1725 EXPECT_EQ(1, d.response_started_count());
1726 EXPECT_EQ(0, d.received_redirect_count());
1727}
1728
1729TEST_F(URLRequestTest, InterceptNetworkError) {
1730 TestInterceptor interceptor;
1731
1732 // intercept the main request to simulate a network error
1733 interceptor.simulate_main_network_error_ = true;
1734
1735 // intercept that error and respond with an OK response
1736 interceptor.intercept_final_response_ = true;
1737 interceptor.final_headers_ = TestInterceptor::ok_headers();
1738 interceptor.final_data_ = TestInterceptor::ok_data();
1739
1740 TestDelegate d;
1741 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1742 req.set_method("GET");
1743 req.Start();
1744 MessageLoop::current()->Run();
1745
1746 // Check the interceptor got called as expected
1747 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1748 EXPECT_TRUE(interceptor.did_intercept_final_);
1749
1750 // Check we received one good response
1751 EXPECT_TRUE(req.status().is_success());
1752 EXPECT_EQ(200, req.response_headers()->response_code());
1753 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1754 EXPECT_EQ(1, d.response_started_count());
1755 EXPECT_EQ(0, d.received_redirect_count());
1756}
1757
1758TEST_F(URLRequestTest, InterceptRestartRequired) {
1759 TestInterceptor interceptor;
1760
1761 // restart the main request
1762 interceptor.restart_main_request_ = true;
1763
1764 // then intercept the new main request and respond with an OK response
1765 interceptor.intercept_main_request_ = true;
1766 interceptor.main_headers_ = TestInterceptor::ok_headers();
1767 interceptor.main_data_ = TestInterceptor::ok_data();
1768
1769 TestDelegate d;
1770 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1771 req.set_method("GET");
1772 req.Start();
1773 MessageLoop::current()->Run();
1774
1775 // Check the interceptor got called as expected
1776 EXPECT_TRUE(interceptor.did_restart_main_);
1777 EXPECT_TRUE(interceptor.did_intercept_main_);
1778
1779 // Check we received one good response
1780 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:111781 if (req.status().is_success()) {
1782 EXPECT_EQ(200, req.response_headers()->response_code());
1783 }
[email protected]a5c713f2009-04-16 21:05:471784 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1785 EXPECT_EQ(1, d.response_started_count());
1786 EXPECT_EQ(0, d.received_redirect_count());
1787}
1788
1789TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1790 TestInterceptor interceptor;
1791
1792 // intercept the main request and cancel from within the restarted job
1793 interceptor.cancel_main_request_ = true;
1794
1795 // setup to intercept final response and override it with an OK response
1796 interceptor.intercept_final_response_ = true;
1797 interceptor.final_headers_ = TestInterceptor::ok_headers();
1798 interceptor.final_data_ = TestInterceptor::ok_data();
1799
1800 TestDelegate d;
1801 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1802 req.set_method("GET");
1803 req.Start();
1804 MessageLoop::current()->Run();
1805
1806 // Check the interceptor got called as expected
1807 EXPECT_TRUE(interceptor.did_cancel_main_);
1808 EXPECT_FALSE(interceptor.did_intercept_final_);
1809
1810 // Check we see a canceled request
1811 EXPECT_FALSE(req.status().is_success());
1812 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1813}
1814
1815TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1816 TestInterceptor interceptor;
1817
1818 // intercept the main request and respond with a redirect
1819 interceptor.intercept_main_request_ = true;
1820 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1821 interceptor.main_data_ = TestInterceptor::redirect_data();
1822
1823 // intercept the redirect and cancel from within that job
1824 interceptor.cancel_redirect_request_ = true;
1825
1826 // setup to intercept final response and override it with an OK response
1827 interceptor.intercept_final_response_ = true;
1828 interceptor.final_headers_ = TestInterceptor::ok_headers();
1829 interceptor.final_data_ = TestInterceptor::ok_data();
1830
1831 TestDelegate d;
1832 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1833 req.set_method("GET");
1834 req.Start();
1835 MessageLoop::current()->Run();
1836
1837 // Check the interceptor got called as expected
1838 EXPECT_TRUE(interceptor.did_intercept_main_);
1839 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1840 EXPECT_FALSE(interceptor.did_intercept_final_);
1841
1842 // Check we see a canceled request
1843 EXPECT_FALSE(req.status().is_success());
1844 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1845}
1846
1847TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1848 TestInterceptor interceptor;
1849
1850 // intercept the main request to simulate a network error
1851 interceptor.simulate_main_network_error_ = true;
1852
1853 // setup to intercept final response and cancel from within that job
1854 interceptor.cancel_final_request_ = true;
1855
1856 TestDelegate d;
1857 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1858 req.set_method("GET");
1859 req.Start();
1860 MessageLoop::current()->Run();
1861
1862 // Check the interceptor got called as expected
1863 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1864 EXPECT_TRUE(interceptor.did_cancel_final_);
1865
1866 // Check we see a canceled request
1867 EXPECT_FALSE(req.status().is_success());
1868 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1869}
1870
1871TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1872 TestInterceptor interceptor;
1873
1874 // intercept the main request and cancel then restart from within that job
1875 interceptor.cancel_then_restart_main_request_ = true;
1876
1877 // setup to intercept final response and override it with an OK response
1878 interceptor.intercept_final_response_ = true;
1879 interceptor.final_headers_ = TestInterceptor::ok_headers();
1880 interceptor.final_data_ = TestInterceptor::ok_data();
1881
1882 TestDelegate d;
1883 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1884 req.set_method("GET");
1885 req.Start();
1886 MessageLoop::current()->Run();
1887
1888 // Check the interceptor got called as expected
1889 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1890 EXPECT_FALSE(interceptor.did_intercept_final_);
1891
1892 // Check we see a canceled request
1893 EXPECT_FALSE(req.status().is_success());
1894 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1895}
1896
[email protected]b89ca032009-08-31 21:41:311897class URLRequestTestFTP : public URLRequestTest {
1898 protected:
1899 static void SetUpTestCase() {
1900 server_ = FTPTestServer::CreateServer(L"");
1901 }
1902
1903 static void TearDownTestCase() {
1904 server_ = NULL;
1905 }
1906
1907 static scoped_refptr<FTPTestServer> server_;
1908};
1909
1910// static
1911scoped_refptr<FTPTestServer> URLRequestTestFTP::server_;
1912
[email protected]5accf7332009-11-24 03:41:381913// Flaky, see http://crbug.com/25045.
1914TEST_F(URLRequestTestFTP, FLAKY_FTPDirectoryListing) {
[email protected]a25e90e2009-09-09 17:05:371915 ASSERT_TRUE(NULL != server_.get());
1916 TestDelegate d;
1917 {
1918 TestURLRequest r(server_->TestServerPage("/"), &d);
1919 r.Start();
1920 EXPECT_TRUE(r.is_pending());
1921
1922 MessageLoop::current()->Run();
1923
1924 EXPECT_FALSE(r.is_pending());
1925 EXPECT_EQ(1, d.response_started_count());
1926 EXPECT_FALSE(d.received_data_before_response());
1927 EXPECT_LT(0, d.bytes_received());
1928 }
1929}
1930
[email protected]b89ca032009-08-31 21:41:311931TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) {
1932 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:021933 FilePath app_path;
[email protected]dd265012009-01-08 20:45:271934 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:021935 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:271936 TestDelegate d;
1937 {
[email protected]b89ca032009-08-31 21:41:311938 TestURLRequest r(server_->TestServerPage("/LICENSE"), &d);
[email protected]dd265012009-01-08 20:45:271939 r.Start();
1940 EXPECT_TRUE(r.is_pending());
1941
1942 MessageLoop::current()->Run();
1943
1944 int64 file_size = 0;
1945 file_util::GetFileSize(app_path, &file_size);
1946
[email protected]ba2f3342009-07-30 18:08:421947 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:271948 EXPECT_EQ(1, d.response_started_count());
1949 EXPECT_FALSE(d.received_data_before_response());
1950 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
1951 }
1952}
1953
[email protected]b89ca032009-08-31 21:41:311954TEST_F(URLRequestTestFTP, FTPGetTest) {
1955 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:021956 FilePath app_path;
[email protected]dd265012009-01-08 20:45:271957 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:021958 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:271959 TestDelegate d;
1960 {
[email protected]b89ca032009-08-31 21:41:311961 TestURLRequest r(server_->TestServerPage("/LICENSE", "chrome", "chrome"),
1962 &d);
[email protected]dd265012009-01-08 20:45:271963 r.Start();
1964 EXPECT_TRUE(r.is_pending());
1965
1966 MessageLoop::current()->Run();
1967
1968 int64 file_size = 0;
1969 file_util::GetFileSize(app_path, &file_size);
1970
[email protected]ba2f3342009-07-30 18:08:421971 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:271972 EXPECT_EQ(1, d.response_started_count());
1973 EXPECT_FALSE(d.received_data_before_response());
1974 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
1975 }
1976}
1977
[email protected]b89ca032009-08-31 21:41:311978TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) {
1979 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:021980 FilePath app_path;
[email protected]dd265012009-01-08 20:45:271981 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:021982 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:271983 TestDelegate d;
1984 {
[email protected]b89ca032009-08-31 21:41:311985 TestURLRequest r(server_->TestServerPage("/LICENSE",
1986 "chrome", "wrong_password"), &d);
[email protected]dd265012009-01-08 20:45:271987 r.Start();
1988 EXPECT_TRUE(r.is_pending());
1989
1990 MessageLoop::current()->Run();
1991
1992 int64 file_size = 0;
1993 file_util::GetFileSize(app_path, &file_size);
1994
[email protected]ba2f3342009-07-30 18:08:421995 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:271996 EXPECT_EQ(1, d.response_started_count());
1997 EXPECT_FALSE(d.received_data_before_response());
1998 EXPECT_EQ(d.bytes_received(), 0);
1999 }
2000}
2001
[email protected]cde4e80d2009-10-16 19:58:152002// Flaky, see http://crbug.com/25045.
2003TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPasswordRestart) {
[email protected]b89ca032009-08-31 21:41:312004 ASSERT_TRUE(NULL != server_.get());
[email protected]8b8a197d2009-08-26 15:57:582005 FilePath app_path;
2006 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2007 app_path = app_path.AppendASCII("LICENSE");
2008 TestDelegate d;
2009 // Set correct login credentials. The delegate will be asked for them when
2010 // the initial login with wrong credentials will fail.
2011 d.set_username(L"chrome");
2012 d.set_password(L"chrome");
2013 {
[email protected]b89ca032009-08-31 21:41:312014 TestURLRequest r(server_->TestServerPage("/LICENSE",
2015 "chrome", "wrong_password"), &d);
[email protected]8b8a197d2009-08-26 15:57:582016 r.Start();
2017 EXPECT_TRUE(r.is_pending());
2018
2019 MessageLoop::current()->Run();
2020
2021 int64 file_size = 0;
2022 file_util::GetFileSize(app_path, &file_size);
2023
2024 EXPECT_FALSE(r.is_pending());
2025 EXPECT_EQ(1, d.response_started_count());
2026 EXPECT_FALSE(d.received_data_before_response());
2027 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2028 }
2029}
2030
[email protected]b89ca032009-08-31 21:41:312031TEST_F(URLRequestTestFTP, FTPCheckWrongUser) {
2032 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:022033 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272034 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022035 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272036 TestDelegate d;
2037 {
[email protected]b89ca032009-08-31 21:41:312038 TestURLRequest r(server_->TestServerPage("/LICENSE",
2039 "wrong_user", "chrome"), &d);
[email protected]dd265012009-01-08 20:45:272040 r.Start();
2041 EXPECT_TRUE(r.is_pending());
2042
2043 MessageLoop::current()->Run();
2044
2045 int64 file_size = 0;
2046 file_util::GetFileSize(app_path, &file_size);
2047
[email protected]ba2f3342009-07-30 18:08:422048 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272049 EXPECT_EQ(1, d.response_started_count());
2050 EXPECT_FALSE(d.received_data_before_response());
2051 EXPECT_EQ(d.bytes_received(), 0);
2052 }
2053}
[email protected]8b8a197d2009-08-26 15:57:582054
[email protected]cde4e80d2009-10-16 19:58:152055// Flaky, see http://crbug.com/25045.
2056TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUserRestart) {
[email protected]b89ca032009-08-31 21:41:312057 ASSERT_TRUE(NULL != server_.get());
[email protected]8b8a197d2009-08-26 15:57:582058 FilePath app_path;
2059 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2060 app_path = app_path.AppendASCII("LICENSE");
2061 TestDelegate d;
2062 // Set correct login credentials. The delegate will be asked for them when
2063 // the initial login with wrong credentials will fail.
2064 d.set_username(L"chrome");
2065 d.set_password(L"chrome");
2066 {
[email protected]b89ca032009-08-31 21:41:312067 TestURLRequest r(server_->TestServerPage("/LICENSE",
2068 "wrong_user", "chrome"), &d);
[email protected]8b8a197d2009-08-26 15:57:582069 r.Start();
2070 EXPECT_TRUE(r.is_pending());
2071
2072 MessageLoop::current()->Run();
2073
2074 int64 file_size = 0;
2075 file_util::GetFileSize(app_path, &file_size);
2076
2077 EXPECT_FALSE(r.is_pending());
2078 EXPECT_EQ(1, d.response_started_count());
2079 EXPECT_FALSE(d.received_data_before_response());
2080 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2081 }
2082}
[email protected]60a3df52009-09-22 16:13:242083
[email protected]cde4e80d2009-10-16 19:58:152084// Flaky, see http://crbug.com/25045.
2085TEST_F(URLRequestTestFTP, FLAKY_FTPCacheURLCredentials) {
[email protected]60a3df52009-09-22 16:13:242086 ASSERT_TRUE(NULL != server_.get());
2087 FilePath app_path;
2088 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2089 app_path = app_path.AppendASCII("LICENSE");
2090
2091 scoped_ptr<TestDelegate> d(new TestDelegate);
2092 {
2093 // Pass correct login identity in the URL.
2094 TestURLRequest r(server_->TestServerPage("/LICENSE",
2095 "chrome", "chrome"),
2096 d.get());
2097 r.Start();
2098 EXPECT_TRUE(r.is_pending());
2099
2100 MessageLoop::current()->Run();
2101
2102 int64 file_size = 0;
2103 file_util::GetFileSize(app_path, &file_size);
2104
2105 EXPECT_FALSE(r.is_pending());
2106 EXPECT_EQ(1, d->response_started_count());
2107 EXPECT_FALSE(d->received_data_before_response());
2108 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2109 }
2110
2111 d.reset(new TestDelegate);
2112 {
2113 // This request should use cached identity from previous request.
2114 TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get());
2115 r.Start();
2116 EXPECT_TRUE(r.is_pending());
2117
2118 MessageLoop::current()->Run();
2119
2120 int64 file_size = 0;
2121 file_util::GetFileSize(app_path, &file_size);
2122
2123 EXPECT_FALSE(r.is_pending());
2124 EXPECT_EQ(1, d->response_started_count());
2125 EXPECT_FALSE(d->received_data_before_response());
2126 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2127 }
2128}
2129
[email protected]cde4e80d2009-10-16 19:58:152130// Flaky, see http://crbug.com/25045.
2131TEST_F(URLRequestTestFTP, FLAKY_FTPCacheLoginBoxCredentials) {
[email protected]60a3df52009-09-22 16:13:242132 ASSERT_TRUE(NULL != server_.get());
2133 FilePath app_path;
2134 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2135 app_path = app_path.AppendASCII("LICENSE");
2136
2137 scoped_ptr<TestDelegate> d(new TestDelegate);
2138 // Set correct login credentials. The delegate will be asked for them when
2139 // the initial login with wrong credentials will fail.
2140 d->set_username(L"chrome");
2141 d->set_password(L"chrome");
2142 {
2143 TestURLRequest r(server_->TestServerPage("/LICENSE",
2144 "chrome", "wrong_password"),
2145 d.get());
2146 r.Start();
2147 EXPECT_TRUE(r.is_pending());
2148
2149 MessageLoop::current()->Run();
2150
2151 int64 file_size = 0;
2152 file_util::GetFileSize(app_path, &file_size);
2153
2154 EXPECT_FALSE(r.is_pending());
2155 EXPECT_EQ(1, d->response_started_count());
2156 EXPECT_FALSE(d->received_data_before_response());
2157 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2158 }
2159
2160 // Use a new delegate without explicit credentials. The cached ones should be
2161 // used.
2162 d.reset(new TestDelegate);
2163 {
2164 // Don't pass wrong credentials in the URL, they would override valid cached
2165 // ones.
2166 TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get());
2167 r.Start();
2168 EXPECT_TRUE(r.is_pending());
2169
2170 MessageLoop::current()->Run();
2171
2172 int64 file_size = 0;
2173 file_util::GetFileSize(app_path, &file_size);
2174
2175 EXPECT_FALSE(r.is_pending());
2176 EXPECT_EQ(1, d->response_started_count());
2177 EXPECT_FALSE(d->received_data_before_response());
2178 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2179 }
2180}
[email protected]09a362d32009-09-24 18:01:332181
2182// Check that default A-L header is sent.
2183TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
2184 ASSERT_TRUE(NULL != server_.get());
2185 TestDelegate d;
2186 TestURLRequest req(server_->TestServerPage("echoheader?Accept-Language"), &d);
2187 req.set_context(new URLRequestTestContext());
2188 req.Start();
2189 MessageLoop::current()->Run();
2190 EXPECT_EQ(req.context()->accept_language(), d.data_received());
2191}
2192
2193// Check that if request overrides the A-L header, the default is not appended.
2194// See http://crbug.com/20894
2195TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
2196 ASSERT_TRUE(NULL != server_.get());
2197 TestDelegate d;
[email protected]eb834fb2009-10-23 16:09:412198 TestURLRequest
2199 req(server_->TestServerPage("echoheaderoverride?Accept-Language"), &d);
[email protected]09a362d32009-09-24 18:01:332200 req.set_context(new URLRequestTestContext());
2201 req.SetExtraRequestHeaders("Accept-Language: ru");
2202 req.Start();
2203 MessageLoop::current()->Run();
2204 EXPECT_EQ(std::string("ru"), d.data_received());
2205}
2206
2207// Check that default A-C header is sent.
2208TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) {
2209 ASSERT_TRUE(NULL != server_.get());
2210 TestDelegate d;
2211 TestURLRequest req(server_->TestServerPage("echoheader?Accept-Charset"), &d);
2212 req.set_context(new URLRequestTestContext());
2213 req.Start();
2214 MessageLoop::current()->Run();
2215 EXPECT_EQ(req.context()->accept_charset(), d.data_received());
2216}
2217
2218// Check that if request overrides the A-C header, the default is not appended.
2219// See http://crbug.com/20894
2220TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) {
2221 ASSERT_TRUE(NULL != server_.get());
2222 TestDelegate d;
[email protected]eb834fb2009-10-23 16:09:412223 TestURLRequest
2224 req(server_->TestServerPage("echoheaderoverride?Accept-Charset"), &d);
[email protected]09a362d32009-09-24 18:01:332225 req.set_context(new URLRequestTestContext());
2226 req.SetExtraRequestHeaders("Accept-Charset: koi-8r");
2227 req.Start();
2228 MessageLoop::current()->Run();
2229 EXPECT_EQ(std::string("koi-8r"), d.data_received());
2230}