blob: 48cfd35410d32b6ef5f49606bac1984f022cdd2c [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]1a157302010-01-29 03:36:4512#elif defined(USE_NSS)
[email protected]1b1a264a2010-01-14 22:36:3513#include "base/nss_util.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]8a16266e2009-09-10 21:08:3952base::StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:5253 return "header";
54}
55
[email protected]71c64f62008-11-15 04:36:5156// Do a case-insensitive search through |haystack| for |needle|.
57bool ContainsString(const std::string& haystack, const char* needle) {
58 std::string::const_iterator it =
59 std::search(haystack.begin(),
60 haystack.end(),
61 needle,
62 needle + strlen(needle),
63 CaseInsensitiveCompare<char>());
64 return it != haystack.end();
65}
66
[email protected]661376a2009-04-29 02:04:2367void FillBuffer(char* buffer, size_t len) {
68 static bool called = false;
69 if (!called) {
70 called = true;
71 int seed = static_cast<int>(Time::Now().ToInternalValue());
72 srand(seed);
73 }
74
75 for (size_t i = 0; i < len; i++) {
76 buffer[i] = static_cast<char>(rand());
77 if (!buffer[i])
78 buffer[i] = 'g';
79 }
80}
81
[email protected]195e77d2009-07-23 19:10:2382scoped_refptr<net::UploadData> CreateSimpleUploadData(const char* data) {
83 scoped_refptr<net::UploadData> upload = new net::UploadData;
84 upload->AppendBytes(data, strlen(data));
85 return upload;
86}
87
[email protected]9396b252008-09-29 17:29:3888} // namespace
initial.commit586acc5fe2008-07-26 22:42:5289
[email protected]7a0bb4bf2008-11-19 21:41:4890// Inherit PlatformTest since we require the autorelease pool on Mac OS X.f
91class URLRequestTest : public PlatformTest {
92};
93
[email protected]b89290212009-08-14 22:37:3594class URLRequestTestHTTP : public URLRequestTest {
95 protected:
96 static void SetUpTestCase() {
[email protected]37314622009-08-17 20:29:3997 server_ = HTTPTestServer::CreateForkingServer(
98 L"net/data/url_request_unittest/");
[email protected]b89290212009-08-14 22:37:3599 }
100
101 static void TearDownTestCase() {
102 server_ = NULL;
103 }
104
[email protected]762d2db2010-01-11 19:03:01105 void HTTPUploadDataOperationTest(const std::string& method) {
106 ASSERT_TRUE(NULL != server_.get());
107 const int kMsgSize = 20000; // multiple of 10
108 const int kIterations = 50;
109 char *uploadBytes = new char[kMsgSize+1];
110 char *ptr = uploadBytes;
111 char marker = 'a';
112 for (int idx = 0; idx < kMsgSize/10; idx++) {
113 memcpy(ptr, "----------", 10);
114 ptr += 10;
115 if (idx % 100 == 0) {
116 ptr--;
117 *ptr++ = marker;
118 if (++marker > 'z')
119 marker = 'a';
120 }
121 }
122 uploadBytes[kMsgSize] = '\0';
123
124 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
125
126 for (int i = 0; i < kIterations; ++i) {
127 TestDelegate d;
128 URLRequest r(server_->TestServerPage("echo"), &d);
129 r.set_context(context);
130 r.set_method(method.c_str());
131
132 r.AppendBytesToUpload(uploadBytes, kMsgSize);
133
134 r.Start();
135 EXPECT_TRUE(r.is_pending());
136
137 MessageLoop::current()->Run();
138
139 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
140 (int) r.status().status() << ", os error: " << r.status().os_error();
141
142 EXPECT_FALSE(d.received_data_before_response());
143 EXPECT_EQ(uploadBytes, d.data_received());
144 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0);
145 EXPECT_EQ(d.data_received().compare(uploadBytes), 0);
146 }
147 delete[] uploadBytes;
148 }
149
[email protected]b89290212009-08-14 22:37:35150 static scoped_refptr<HTTPTestServer> server_;
151};
152
153// static
154scoped_refptr<HTTPTestServer> URLRequestTestHTTP::server_;
155
156TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]d1ec59082009-02-11 02:48:15157 // In this unit test, we're using the HTTPTestServer as a proxy server and
[email protected]dc651782009-02-14 01:45:08158 // issuing a CONNECT request with the magic host name "www.redirect.com".
159 // The HTTPTestServer will return a 302 response, which we should not
[email protected]d1ec59082009-02-11 02:48:15160 // follow.
[email protected]b89290212009-08-14 22:37:35161 ASSERT_TRUE(NULL != server_.get());
[email protected]d1ec59082009-02-11 02:48:15162 TestDelegate d;
163 {
164 URLRequest r(GURL("https://www.redirect.com/"), &d);
165 std::string proxy("localhost:");
166 proxy.append(IntToString(kHTTPDefaultPort));
167 r.set_context(new TestURLRequestContext(proxy));
168
169 r.Start();
170 EXPECT_TRUE(r.is_pending());
171
172 MessageLoop::current()->Run();
173
[email protected]c744cf22009-02-27 07:28:08174 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
175 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08176 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:15177 // We should not have followed the redirect.
178 EXPECT_EQ(0, d.received_redirect_count());
179 }
180}
181
[email protected]b89290212009-08-14 22:37:35182TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]dc651782009-02-14 01:45:08183 // In this unit test, we're using the HTTPTestServer as a proxy server and
184 // issuing a CONNECT request with the magic host name "www.server-auth.com".
185 // The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:35186 ASSERT_TRUE(NULL != server_.get());
[email protected]dc651782009-02-14 01:45:08187 TestDelegate d;
188 {
189 URLRequest r(GURL("https://www.server-auth.com/"), &d);
190 std::string proxy("localhost:");
191 proxy.append(IntToString(kHTTPDefaultPort));
192 r.set_context(new TestURLRequestContext(proxy));
193
194 r.Start();
195 EXPECT_TRUE(r.is_pending());
196
197 MessageLoop::current()->Run();
198
199 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]c744cf22009-02-27 07:28:08200 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08201 }
202}
203
[email protected]b89290212009-08-14 22:37:35204TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
205 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52206 TestDelegate d;
207 {
[email protected]b89290212009-08-14 22:37:35208 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52209
210 r.Start();
211 EXPECT_TRUE(r.is_pending());
212
213 MessageLoop::current()->Run();
214
215 EXPECT_EQ(1, d.response_started_count());
216 EXPECT_FALSE(d.received_data_before_response());
217 EXPECT_NE(0, d.bytes_received());
[email protected]c31a54592009-09-04 02:36:16218
[email protected]ab6a5c42009-11-13 00:25:29219 // The first part of the log will be for URL_REQUEST_START.
220 // After that, there should be an HTTP_TRANSACTION_READ_BODY
[email protected]e9002a92010-01-29 07:10:46221 EXPECT_TRUE(net::LogContainsBeginEvent(
222 *r.load_log(), 0, net::LoadLog::TYPE_URL_REQUEST_START));
223 EXPECT_TRUE(net::LogContainsEndEvent(
224 *r.load_log(), -3, net::LoadLog::TYPE_URL_REQUEST_START));
225 EXPECT_TRUE(net::LogContainsBeginEvent(
226 *r.load_log(), -2, net::LoadLog::TYPE_HTTP_TRANSACTION_READ_BODY));
227 EXPECT_TRUE(net::LogContainsEndEvent(
228 *r.load_log(), -1, net::LoadLog::TYPE_HTTP_TRANSACTION_READ_BODY));
initial.commit586acc5fe2008-07-26 22:42:52229 }
initial.commit586acc5fe2008-07-26 22:42:52230}
231
[email protected]b89290212009-08-14 22:37:35232TEST_F(URLRequestTestHTTP, GetTest) {
233 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52234 TestDelegate d;
235 {
[email protected]b89290212009-08-14 22:37:35236 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52237
238 r.Start();
239 EXPECT_TRUE(r.is_pending());
240
241 MessageLoop::current()->Run();
242
243 EXPECT_EQ(1, d.response_started_count());
244 EXPECT_FALSE(d.received_data_before_response());
245 EXPECT_NE(0, d.bytes_received());
246 }
[email protected]5d7b373e2009-09-02 07:19:03247}
248
[email protected]7844480a2009-12-16 21:18:58249TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
250 scoped_refptr<HTTPSTestServer> https_server =
251 HTTPSTestServer::CreateGoodServer(L"net/data/ssl/");
252 ASSERT_TRUE(NULL != https_server.get());
253 ASSERT_TRUE(NULL != server_.get());
254
255 // An https server is sent a request with an https referer,
256 // and responds with a redirect to an http url. The http
257 // server should not be sent the referer.
258 GURL http_destination = server_->TestServerPage("");
259 TestDelegate d;
260 TestURLRequest req(https_server->TestServerPage(
261 "server-redirect?" + http_destination.spec()), &d);
262 req.set_referrer("https://www.referrer.com/");
263 req.Start();
264 MessageLoop::current()->Run();
265
266 EXPECT_EQ(1, d.response_started_count());
267 EXPECT_EQ(1, d.received_redirect_count());
268 EXPECT_EQ(http_destination, req.url());
269 EXPECT_EQ(std::string(), req.referrer());
270}
271
[email protected]73e0bba2009-02-19 22:57:09272TEST_F(URLRequestTest, QuitTest) {
[email protected]b89290212009-08-14 22:37:35273 // Don't use shared server here because we order it to quit.
274 // It would impact other tests.
[email protected]73e0bba2009-02-19 22:57:09275 scoped_refptr<HTTPTestServer> server =
276 HTTPTestServer::CreateServer(L"", NULL);
277 ASSERT_TRUE(NULL != server.get());
278 server->SendQuit();
279 EXPECT_TRUE(server->WaitToFinish(20000));
[email protected]73e0bba2009-02-19 22:57:09280}
281
282class HTTPSRequestTest : public testing::Test {
[email protected]ea224582008-12-07 20:25:46283};
284
285#if defined(OS_MACOSX)
[email protected]3016ded2009-06-19 18:27:59286// Status 6/19/09:
287//
288// If these tests are enabled on OSX, the first one (HTTPSGetTest)
289// will fail. I didn't track it down explicitly, but did observe that
290// the testserver.py kills itself (e.g. "process_util_posix.cc(84)]
291// Unable to terminate process."). tlslite and testserver.py are hard
292// to debug (redirection of stdout/stderr to a file so you can't see
293// errors; lots of naked "except:" statements, etc), but I did track
294// down an SSL auth failure as one cause of it deciding to die
295// silently.
296//
297// The next test, HTTPSMismatchedTest, will look like it hangs by
298// looping over calls to SSLHandshake() (Security framework call) as
299// called from SSLClientSocketMac::DoHandshake(). Return values are a
300// repeating pattern of -9803 (come back later) and -9812 (cert valid
301// but root not trusted). If you don't have the cert in your keychain
302// as documented on http://dev.chromium.org/developers/testing, the
303// -9812 becomes a -9813 (no root cert). Interestingly, the handshake
304// also appears to be a failure point for other disabled tests, such
305// as (SSLClientSocketTest,Connect) in
306// net/base/ssl_client_socket_unittest.cc.
307//
308// Old comment (not sure if obsolete):
[email protected]73e0bba2009-02-19 22:57:09309// ssl_client_socket_mac.cc crashes currently in GetSSLInfo
310// when called on a connection with an unrecognized certificate
[email protected]bacff652009-03-31 17:50:33311#define MAYBE_HTTPSGetTest DISABLED_HTTPSGetTest
312#define MAYBE_HTTPSMismatchedTest DISABLED_HTTPSMismatchedTest
313#define MAYBE_HTTPSExpiredTest DISABLED_HTTPSExpiredTest
[email protected]ea224582008-12-07 20:25:46314#else
[email protected]bacff652009-03-31 17:50:33315#define MAYBE_HTTPSGetTest HTTPSGetTest
316#define MAYBE_HTTPSMismatchedTest HTTPSMismatchedTest
317#define MAYBE_HTTPSExpiredTest HTTPSExpiredTest
[email protected]ea224582008-12-07 20:25:46318#endif
319
320TEST_F(HTTPSRequestTest, MAYBE_HTTPSGetTest) {
321 // Note: tools/testserver/testserver.py does not need
322 // a working document root to server the pages / and /hello.html,
323 // so this test doesn't really need to specify a document root.
324 // But if it did, a good one would be net/data/ssl.
[email protected]dd265012009-01-08 20:45:27325 scoped_refptr<HTTPSTestServer> server =
[email protected]73e0bba2009-02-19 22:57:09326 HTTPSTestServer::CreateGoodServer(L"net/data/ssl");
[email protected]dd265012009-01-08 20:45:27327 ASSERT_TRUE(NULL != server.get());
[email protected]ea224582008-12-07 20:25:46328
[email protected]ea224582008-12-07 20:25:46329 TestDelegate d;
330 {
[email protected]dd265012009-01-08 20:45:27331 TestURLRequest r(server->TestServerPage(""), &d);
[email protected]ea224582008-12-07 20:25:46332
333 r.Start();
334 EXPECT_TRUE(r.is_pending());
335
336 MessageLoop::current()->Run();
337
338 EXPECT_EQ(1, d.response_started_count());
339 EXPECT_FALSE(d.received_data_before_response());
340 EXPECT_NE(0, d.bytes_received());
341 }
[email protected]ea224582008-12-07 20:25:46342}
343
[email protected]bacff652009-03-31 17:50:33344TEST_F(HTTPSRequestTest, MAYBE_HTTPSMismatchedTest) {
345 scoped_refptr<HTTPSTestServer> server =
346 HTTPSTestServer::CreateMismatchedServer(L"net/data/ssl");
347 ASSERT_TRUE(NULL != server.get());
348
349 bool err_allowed = true;
350 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
351 TestDelegate d;
352 {
353 d.set_allow_certificate_errors(err_allowed);
354 TestURLRequest r(server->TestServerPage(""), &d);
355
356 r.Start();
357 EXPECT_TRUE(r.is_pending());
358
359 MessageLoop::current()->Run();
360
361 EXPECT_EQ(1, d.response_started_count());
362 EXPECT_FALSE(d.received_data_before_response());
363 EXPECT_TRUE(d.have_certificate_errors());
364 if (err_allowed)
365 EXPECT_NE(0, d.bytes_received());
366 else
367 EXPECT_EQ(0, d.bytes_received());
368 }
369 }
370}
371
372TEST_F(HTTPSRequestTest, MAYBE_HTTPSExpiredTest) {
373 scoped_refptr<HTTPSTestServer> server =
374 HTTPSTestServer::CreateExpiredServer(L"net/data/ssl");
375 ASSERT_TRUE(NULL != server.get());
376
377 // Iterate from false to true, just so that we do the opposite of the
378 // previous test in order to increase test coverage.
379 bool err_allowed = false;
380 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
381 TestDelegate d;
382 {
383 d.set_allow_certificate_errors(err_allowed);
384 TestURLRequest r(server->TestServerPage(""), &d);
385
386 r.Start();
387 EXPECT_TRUE(r.is_pending());
388
389 MessageLoop::current()->Run();
390
391 EXPECT_EQ(1, d.response_started_count());
392 EXPECT_FALSE(d.received_data_before_response());
393 EXPECT_TRUE(d.have_certificate_errors());
394 if (err_allowed)
395 EXPECT_NE(0, d.bytes_received());
396 else
397 EXPECT_EQ(0, d.bytes_received());
398 }
399 }
400}
[email protected]73e0bba2009-02-19 22:57:09401
[email protected]37314622009-08-17 20:29:39402TEST_F(URLRequestTestHTTP, CancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52403 TestDelegate d;
404 {
405 TestURLRequest r(GURL("http://www.google.com/"), &d);
406
407 r.Start();
408 EXPECT_TRUE(r.is_pending());
409
410 r.Cancel();
411
412 MessageLoop::current()->Run();
413
414 // We expect to receive OnResponseStarted even though the request has been
415 // cancelled.
416 EXPECT_EQ(1, d.response_started_count());
417 EXPECT_EQ(0, d.bytes_received());
418 EXPECT_FALSE(d.received_data_before_response());
419 }
initial.commit586acc5fe2008-07-26 22:42:52420}
421
[email protected]37314622009-08-17 20:29:39422TEST_F(URLRequestTestHTTP, CancelTest2) {
423 ASSERT_TRUE(NULL != server_.get());
[email protected]dd265012009-01-08 20:45:27424
425 // error C2446: '!=' : no conversion from 'HTTPTestServer *const '
426 // to 'const int'
427
initial.commit586acc5fe2008-07-26 22:42:52428 TestDelegate d;
429 {
[email protected]37314622009-08-17 20:29:39430 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52431
432 d.set_cancel_in_response_started(true);
433
434 r.Start();
435 EXPECT_TRUE(r.is_pending());
436
437 MessageLoop::current()->Run();
438
439 EXPECT_EQ(1, d.response_started_count());
440 EXPECT_EQ(0, d.bytes_received());
441 EXPECT_FALSE(d.received_data_before_response());
442 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
443 }
initial.commit586acc5fe2008-07-26 22:42:52444}
445
[email protected]37314622009-08-17 20:29:39446TEST_F(URLRequestTestHTTP, CancelTest3) {
447 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52448 TestDelegate d;
449 {
[email protected]37314622009-08-17 20:29:39450 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52451
452 d.set_cancel_in_received_data(true);
453
454 r.Start();
455 EXPECT_TRUE(r.is_pending());
456
457 MessageLoop::current()->Run();
458
459 EXPECT_EQ(1, d.response_started_count());
460 // There is no guarantee about how much data was received
461 // before the cancel was issued. It could have been 0 bytes,
462 // or it could have been all the bytes.
463 // EXPECT_EQ(0, d.bytes_received());
464 EXPECT_FALSE(d.received_data_before_response());
465 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
466 }
initial.commit586acc5fe2008-07-26 22:42:52467}
468
[email protected]37314622009-08-17 20:29:39469TEST_F(URLRequestTestHTTP, CancelTest4) {
470 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52471 TestDelegate d;
472 {
[email protected]37314622009-08-17 20:29:39473 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52474
475 r.Start();
476 EXPECT_TRUE(r.is_pending());
477
478 // The request will be implicitly canceled when it is destroyed. The
479 // test delegate must not post a quit message when this happens because
480 // this test doesn't actually have a message loop. The quit message would
481 // get put on this thread's message queue and the next test would exit
482 // early, causing problems.
483 d.set_quit_on_complete(false);
484 }
485 // expect things to just cleanup properly.
486
487 // we won't actually get a received reponse here because we've never run the
488 // message loop
489 EXPECT_FALSE(d.received_data_before_response());
490 EXPECT_EQ(0, d.bytes_received());
491}
492
[email protected]37314622009-08-17 20:29:39493TEST_F(URLRequestTestHTTP, CancelTest5) {
494 ASSERT_TRUE(NULL != server_.get());
[email protected]ba2f3342009-07-30 18:08:42495 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
initial.commit586acc5fe2008-07-26 22:42:52496
497 // populate cache
498 {
499 TestDelegate d;
[email protected]37314622009-08-17 20:29:39500 URLRequest r(server_->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52501 r.set_context(context);
502 r.Start();
503 MessageLoop::current()->Run();
504 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
505 }
506
507 // cancel read from cache (see bug 990242)
508 {
509 TestDelegate d;
[email protected]37314622009-08-17 20:29:39510 URLRequest r(server_->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52511 r.set_context(context);
512 r.Start();
513 r.Cancel();
514 MessageLoop::current()->Run();
515
516 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
517 EXPECT_EQ(1, d.response_started_count());
518 EXPECT_EQ(0, d.bytes_received());
519 EXPECT_FALSE(d.received_data_before_response());
520 }
initial.commit586acc5fe2008-07-26 22:42:52521}
522
[email protected]37314622009-08-17 20:29:39523TEST_F(URLRequestTestHTTP, PostTest) {
[email protected]762d2db2010-01-11 19:03:01524 HTTPUploadDataOperationTest("POST");
525}
initial.commit586acc5fe2008-07-26 22:42:52526
[email protected]762d2db2010-01-11 19:03:01527TEST_F(URLRequestTestHTTP, PutTest) {
528 HTTPUploadDataOperationTest("PUT");
initial.commit586acc5fe2008-07-26 22:42:52529}
530
[email protected]37314622009-08-17 20:29:39531TEST_F(URLRequestTestHTTP, PostEmptyTest) {
532 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52533 TestDelegate d;
534 {
[email protected]37314622009-08-17 20:29:39535 TestURLRequest r(server_->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52536 r.set_method("POST");
537
538 r.Start();
539 EXPECT_TRUE(r.is_pending());
540
541 MessageLoop::current()->Run();
542
543 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
544 (int) r.status().status() << ", os error: " << r.status().os_error();
545
546 EXPECT_FALSE(d.received_data_before_response());
547 EXPECT_TRUE(d.data_received().empty());
548 }
initial.commit586acc5fe2008-07-26 22:42:52549}
550
[email protected]37314622009-08-17 20:29:39551TEST_F(URLRequestTestHTTP, PostFileTest) {
552 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52553 TestDelegate d;
554 {
[email protected]37314622009-08-17 20:29:39555 TestURLRequest r(server_->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52556 r.set_method("POST");
557
[email protected]399b8702009-05-01 20:34:02558 FilePath dir;
initial.commit586acc5fe2008-07-26 22:42:52559 PathService::Get(base::DIR_EXE, &dir);
[email protected]9396b252008-09-29 17:29:38560 file_util::SetCurrentDirectory(dir);
initial.commit586acc5fe2008-07-26 22:42:52561
[email protected]72cbd322009-04-07 10:17:12562 FilePath path;
initial.commit586acc5fe2008-07-26 22:42:52563 PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]72cbd322009-04-07 10:17:12564 path = path.Append(FILE_PATH_LITERAL("net"));
565 path = path.Append(FILE_PATH_LITERAL("data"));
566 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
567 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
initial.commit586acc5fe2008-07-26 22:42:52568 r.AppendFileToUpload(path);
569
570 // This file should just be ignored in the upload stream.
[email protected]72cbd322009-04-07 10:17:12571 r.AppendFileToUpload(FilePath(FILE_PATH_LITERAL(
572 "c:\\path\\to\\non\\existant\\file.randomness.12345")));
initial.commit586acc5fe2008-07-26 22:42:52573
574 r.Start();
575 EXPECT_TRUE(r.is_pending());
576
577 MessageLoop::current()->Run();
578
[email protected]10a1fe92008-11-04 21:47:02579 int64 longsize;
580 ASSERT_EQ(true, file_util::GetFileSize(path, &longsize));
581 int size = static_cast<int>(longsize);
initial.commit586acc5fe2008-07-26 22:42:52582 scoped_array<char> buf(new char[size]);
583
[email protected]dd265012009-01-08 20:45:27584 int size_read = static_cast<int>(file_util::ReadFile(path,
585 buf.get(), size));
[email protected]eac0709a2008-11-04 21:00:46586 ASSERT_EQ(size, size_read);
initial.commit586acc5fe2008-07-26 22:42:52587
588 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
589 (int) r.status().status() << ", os error: " << r.status().os_error();
590
591 EXPECT_FALSE(d.received_data_before_response());
592
593 ASSERT_EQ(size, d.bytes_received());
594 EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size));
595 }
initial.commit586acc5fe2008-07-26 22:42:52596}
597
[email protected]7a0bb4bf2008-11-19 21:41:48598TEST_F(URLRequestTest, AboutBlankTest) {
initial.commit586acc5fe2008-07-26 22:42:52599 TestDelegate d;
600 {
601 TestURLRequest r(GURL("about:blank"), &d);
602
603 r.Start();
604 EXPECT_TRUE(r.is_pending());
605
606 MessageLoop::current()->Run();
607
608 EXPECT_TRUE(!r.is_pending());
609 EXPECT_FALSE(d.received_data_before_response());
610 EXPECT_EQ(d.bytes_received(), 0);
611 }
initial.commit586acc5fe2008-07-26 22:42:52612}
613
[email protected]fcb7fd92009-10-22 04:18:58614TEST_F(URLRequestTest, DataURLImageTest) {
615 TestDelegate d;
616 {
617 // Use our nice little Chrome logo.
618 TestURLRequest r(GURL(
619 "data:image/png;base64,"
620 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
621 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
622 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
623 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
624 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
625 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
626 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
627 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
628 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
629 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
630 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
631 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
632 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
633 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
634 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
635 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
636 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
637 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
638 &d);
639
640 r.Start();
641 EXPECT_TRUE(r.is_pending());
642
643 MessageLoop::current()->Run();
644
645 EXPECT_TRUE(!r.is_pending());
646 EXPECT_FALSE(d.received_data_before_response());
647 EXPECT_EQ(d.bytes_received(), 911);
648 }
649}
650
[email protected]7a0bb4bf2008-11-19 21:41:48651TEST_F(URLRequestTest, FileTest) {
[email protected]b9e04f02008-11-27 04:03:57652 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52653 PathService::Get(base::FILE_EXE, &app_path);
[email protected]72cbd322009-04-07 10:17:12654 GURL app_url = net::FilePathToFileURL(app_path);
initial.commit586acc5fe2008-07-26 22:42:52655
656 TestDelegate d;
657 {
[email protected]b9e04f02008-11-27 04:03:57658 TestURLRequest r(app_url, &d);
initial.commit586acc5fe2008-07-26 22:42:52659
660 r.Start();
661 EXPECT_TRUE(r.is_pending());
662
663 MessageLoop::current()->Run();
664
[email protected]b77280c2009-03-20 17:27:47665 int64 file_size = -1;
666 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
initial.commit586acc5fe2008-07-26 22:42:52667
668 EXPECT_TRUE(!r.is_pending());
669 EXPECT_EQ(1, d.response_started_count());
670 EXPECT_FALSE(d.received_data_before_response());
[email protected]9396b252008-09-29 17:29:38671 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
initial.commit586acc5fe2008-07-26 22:42:52672 }
initial.commit586acc5fe2008-07-26 22:42:52673}
674
[email protected]661376a2009-04-29 02:04:23675TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
676 const size_t buffer_size = 4000;
677 scoped_array<char> buffer(new char[buffer_size]);
678 FillBuffer(buffer.get(), buffer_size);
679
680 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55681 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23682 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53683 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23684
685 int64 file_size;
686 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
687
688 const size_t first_byte_position = 500;
689 const size_t last_byte_position = buffer_size - first_byte_position;
690 const size_t content_length = last_byte_position - first_byte_position + 1;
691 std::string partial_buffer_string(buffer.get() + first_byte_position,
692 buffer.get() + last_byte_position + 1);
693
694 TestDelegate d;
695 {
696 TestURLRequest r(temp_url, &d);
697
[email protected]34b2b002009-11-20 06:53:28698 r.SetExtraRequestHeaders(
699 StringPrintf("Range: bytes=%" PRIuS "-%" PRIuS "\n",
700 first_byte_position, last_byte_position));
[email protected]661376a2009-04-29 02:04:23701 r.Start();
702 EXPECT_TRUE(r.is_pending());
703
704 MessageLoop::current()->Run();
705 EXPECT_TRUE(!r.is_pending());
706 EXPECT_EQ(1, d.response_started_count());
707 EXPECT_FALSE(d.received_data_before_response());
708 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
709 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
710 EXPECT_TRUE(partial_buffer_string == d.data_received());
711 }
712
713 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23714}
715
716TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
717 const size_t buffer_size = 4000;
718 scoped_array<char> buffer(new char[buffer_size]);
719 FillBuffer(buffer.get(), buffer_size);
720
721 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55722 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23723 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53724 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23725
726 int64 file_size;
727 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
728
729 const size_t first_byte_position = 500;
730 const size_t last_byte_position = buffer_size - 1;
731 const size_t content_length = last_byte_position - first_byte_position + 1;
732 std::string partial_buffer_string(buffer.get() + first_byte_position,
733 buffer.get() + last_byte_position + 1);
734
735 TestDelegate d;
736 {
737 TestURLRequest r(temp_url, &d);
738
[email protected]34b2b002009-11-20 06:53:28739 r.SetExtraRequestHeaders(StringPrintf("Range: bytes=%" PRIuS "-\n",
[email protected]661376a2009-04-29 02:04:23740 first_byte_position));
741 r.Start();
742 EXPECT_TRUE(r.is_pending());
743
744 MessageLoop::current()->Run();
745 EXPECT_TRUE(!r.is_pending());
746 EXPECT_EQ(1, d.response_started_count());
747 EXPECT_FALSE(d.received_data_before_response());
748 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
749 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
750 EXPECT_TRUE(partial_buffer_string == d.data_received());
751 }
752
753 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23754}
755
756TEST_F(URLRequestTest, FileTestMultipleRanges) {
757 const size_t buffer_size = 400000;
758 scoped_array<char> buffer(new char[buffer_size]);
759 FillBuffer(buffer.get(), buffer_size);
760
761 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55762 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23763 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53764 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23765
766 int64 file_size;
767 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
768
769 TestDelegate d;
770 {
771 TestURLRequest r(temp_url, &d);
772
773 r.SetExtraRequestHeaders(StringPrintf("Range: bytes=0-0,10-200,200-300\n"));
774 r.Start();
775 EXPECT_TRUE(r.is_pending());
776
777 MessageLoop::current()->Run();
778 EXPECT_TRUE(d.request_failed());
779 }
780
781 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23782}
783
[email protected]7a0bb4bf2008-11-19 21:41:48784TEST_F(URLRequestTest, InvalidUrlTest) {
initial.commit586acc5fe2008-07-26 22:42:52785 TestDelegate d;
786 {
787 TestURLRequest r(GURL("invalid url"), &d);
788
789 r.Start();
790 EXPECT_TRUE(r.is_pending());
791
792 MessageLoop::current()->Run();
793 EXPECT_TRUE(d.request_failed());
794 }
initial.commit586acc5fe2008-07-26 22:42:52795}
796
[email protected]6412d322008-11-21 23:49:30797// This test is disabled because it fails on some computers due to proxies
798// returning a page in response to this request rather than reporting failure.
799TEST_F(URLRequestTest, DISABLED_DnsFailureTest) {
initial.commit586acc5fe2008-07-26 22:42:52800 TestDelegate d;
801 {
802 URLRequest r(GURL("http://thisisnotavalidurl0123456789foo.com/"), &d);
803
804 r.Start();
805 EXPECT_TRUE(r.is_pending());
806
807 MessageLoop::current()->Run();
808 EXPECT_TRUE(d.request_failed());
809 }
initial.commit586acc5fe2008-07-26 22:42:52810}
initial.commit586acc5fe2008-07-26 22:42:52811
[email protected]37314622009-08-17 20:29:39812TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
813 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52814 TestDelegate d;
[email protected]37314622009-08-17 20:29:39815 TestURLRequest req(server_->TestServerPage("files/with-headers.html"), &d);
initial.commit586acc5fe2008-07-26 22:42:52816 req.Start();
817 MessageLoop::current()->Run();
818
819 const net::HttpResponseHeaders* headers = req.response_headers();
[email protected]589deddb2009-10-05 23:41:40820
821 // Simple sanity check that response_info() accesses the same data.
822 EXPECT_EQ(headers, req.response_info().headers.get());
823
initial.commit586acc5fe2008-07-26 22:42:52824 std::string header;
825 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
826 EXPECT_EQ("private", header);
827
828 header.clear();
829 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
830 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
831
832 // The response has two "X-Multiple-Entries" headers.
833 // This verfies our output has them concatenated together.
834 header.clear();
835 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
836 EXPECT_EQ("a, b", header);
837}
838
[email protected]9396b252008-09-29 17:29:38839#if defined(OS_WIN)
[email protected]7a0bb4bf2008-11-19 21:41:48840TEST_F(URLRequestTest, ResolveShortcutTest) {
[email protected]399b8702009-05-01 20:34:02841 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52842 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:02843 app_path = app_path.AppendASCII("net");
844 app_path = app_path.AppendASCII("data");
845 app_path = app_path.AppendASCII("url_request_unittest");
846 app_path = app_path.AppendASCII("with-headers.html");
initial.commit586acc5fe2008-07-26 22:42:52847
[email protected]399b8702009-05-01 20:34:02848 std::wstring lnk_path = app_path.value() + L".lnk";
initial.commit586acc5fe2008-07-26 22:42:52849
850 HRESULT result;
851 IShellLink *shell = NULL;
852 IPersistFile *persist = NULL;
853
854 CoInitialize(NULL);
855 // Temporarily create a shortcut for test
856 result = CoCreateInstance(CLSID_ShellLink, NULL,
[email protected]5d7b373e2009-09-02 07:19:03857 CLSCTX_INPROC_SERVER, IID_IShellLink,
858 reinterpret_cast<LPVOID*>(&shell));
[email protected]d149ce82009-07-01 23:57:02859 ASSERT_TRUE(SUCCEEDED(result));
initial.commit586acc5fe2008-07-26 22:42:52860 result = shell->QueryInterface(IID_IPersistFile,
[email protected]5d7b373e2009-09-02 07:19:03861 reinterpret_cast<LPVOID*>(&persist));
[email protected]d149ce82009-07-01 23:57:02862 ASSERT_TRUE(SUCCEEDED(result));
[email protected]399b8702009-05-01 20:34:02863 result = shell->SetPath(app_path.value().c_str());
initial.commit586acc5fe2008-07-26 22:42:52864 EXPECT_TRUE(SUCCEEDED(result));
865 result = shell->SetDescription(L"ResolveShortcutTest");
866 EXPECT_TRUE(SUCCEEDED(result));
867 result = persist->Save(lnk_path.c_str(), TRUE);
868 EXPECT_TRUE(SUCCEEDED(result));
869 if (persist)
870 persist->Release();
871 if (shell)
872 shell->Release();
873
874 TestDelegate d;
875 {
[email protected]72cbd322009-04-07 10:17:12876 TestURLRequest r(net::FilePathToFileURL(FilePath(lnk_path)), &d);
initial.commit586acc5fe2008-07-26 22:42:52877
878 r.Start();
879 EXPECT_TRUE(r.is_pending());
880
881 MessageLoop::current()->Run();
882
883 WIN32_FILE_ATTRIBUTE_DATA data;
[email protected]399b8702009-05-01 20:34:02884 GetFileAttributesEx(app_path.value().c_str(),
885 GetFileExInfoStandard, &data);
886 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
initial.commit586acc5fe2008-07-26 22:42:52887 FILE_SHARE_READ, NULL, OPEN_EXISTING,
888 FILE_ATTRIBUTE_NORMAL, NULL);
889 EXPECT_NE(INVALID_HANDLE_VALUE, file);
890 scoped_array<char> buffer(new char[data.nFileSizeLow]);
891 DWORD read_size;
892 BOOL result;
893 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
894 &read_size, NULL);
895 std::string content(buffer.get(), read_size);
896 CloseHandle(file);
897
898 EXPECT_TRUE(!r.is_pending());
899 EXPECT_EQ(1, d.received_redirect_count());
900 EXPECT_EQ(content, d.data_received());
901 }
902
903 // Clean the shortcut
904 DeleteFile(lnk_path.c_str());
905 CoUninitialize();
initial.commit586acc5fe2008-07-26 22:42:52906}
[email protected]9396b252008-09-29 17:29:38907#endif // defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:52908
[email protected]37314622009-08-17 20:29:39909TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
910 ASSERT_TRUE(NULL != server_.get());
[email protected]dd265012009-01-08 20:45:27911
initial.commit586acc5fe2008-07-26 22:42:52912 TestDelegate d;
[email protected]37314622009-08-17 20:29:39913 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52914 "files/content-type-normalization.html"), &d);
915 req.Start();
916 MessageLoop::current()->Run();
917
918 std::string mime_type;
919 req.GetMimeType(&mime_type);
920 EXPECT_EQ("text/html", mime_type);
921
922 std::string charset;
923 req.GetCharset(&charset);
924 EXPECT_EQ("utf-8", charset);
925 req.Cancel();
926}
927
[email protected]7a0bb4bf2008-11-19 21:41:48928TEST_F(URLRequestTest, FileDirCancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52929 // Put in mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37930 net::NetModule::SetResourceProvider(TestNetResourceProvider);
initial.commit586acc5fe2008-07-26 22:42:52931
932 TestDelegate d;
933 {
[email protected]72cbd322009-04-07 10:17:12934 FilePath file_path;
initial.commit586acc5fe2008-07-26 22:42:52935 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
[email protected]72cbd322009-04-07 10:17:12936 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
937 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
initial.commit586acc5fe2008-07-26 22:42:52938
[email protected]8ac1a752008-07-31 19:40:37939 TestURLRequest req(net::FilePathToFileURL(file_path), &d);
initial.commit586acc5fe2008-07-26 22:42:52940 req.Start();
941 EXPECT_TRUE(req.is_pending());
942
943 d.set_cancel_in_received_data_pending(true);
944
945 MessageLoop::current()->Run();
946 }
initial.commit586acc5fe2008-07-26 22:42:52947
948 // Take out mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37949 net::NetModule::SetResourceProvider(NULL);
initial.commit586acc5fe2008-07-26 22:42:52950}
951
[email protected]7886a8c2009-08-21 04:11:09952TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
953 // There is an implicit redirect when loading a file path that matches a
954 // directory and does not end with a slash. Ensure that following such
955 // redirects does not crash. See http://crbug.com/18686.
956
957 FilePath path;
958 PathService::Get(base::DIR_SOURCE_ROOT, &path);
959 path = path.Append(FILE_PATH_LITERAL("net"));
960 path = path.Append(FILE_PATH_LITERAL("data"));
961 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
962
963 TestDelegate d;
964 d.set_quit_on_redirect(true);
965 TestURLRequest req(net::FilePathToFileURL(path), &d);
966 req.Start();
967 MessageLoop::current()->Run();
968
969 // Let the directory lister have time to finish its work, which will
970 // cause the URLRequestFileDirJob's ref count to drop to 1.
971 URLRequestFileDirJob* job = static_cast<URLRequestFileDirJob*>(req.job());
972 while (!job->list_complete()) {
973 PlatformThread::Sleep(10);
974 MessageLoop::current()->RunAllPending();
975 }
976
977 // Should not crash during this call!
978 req.FollowDeferredRedirect();
979
980 // Flush event queue.
981 MessageLoop::current()->RunAllPending();
982}
983
[email protected]37314622009-08-17 20:29:39984TEST_F(URLRequestTestHTTP, RestrictRedirects) {
985 ASSERT_TRUE(NULL != server_.get());
[email protected]dd265012009-01-08 20:45:27986
initial.commit586acc5fe2008-07-26 22:42:52987 TestDelegate d;
[email protected]37314622009-08-17 20:29:39988 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52989 "files/redirect-to-file.html"), &d);
990 req.Start();
991 MessageLoop::current()->Run();
992
993 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
994 EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error());
995}
996
[email protected]37314622009-08-17 20:29:39997TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
998 ASSERT_TRUE(NULL != server_.get());
[email protected]4e66ed12009-07-21 23:38:42999
1000 TestDelegate d;
[email protected]37314622009-08-17 20:29:391001 TestURLRequest req(server_->TestServerPage(
[email protected]4e66ed12009-07-21 23:38:421002 "files/redirect-to-invalid-url.html"), &d);
1003 req.Start();
1004 MessageLoop::current()->Run();
1005
1006 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1007 EXPECT_EQ(net::ERR_INVALID_URL, req.status().os_error());
1008}
1009
[email protected]37314622009-08-17 20:29:391010TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
1011 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521012 TestDelegate d;
[email protected]37314622009-08-17 20:29:391013 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:521014 "echoheader?Referer"), &d);
1015 req.set_referrer("http://user:[email protected]/");
1016 req.Start();
1017 MessageLoop::current()->Run();
1018
1019 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
1020}
1021
[email protected]37314622009-08-17 20:29:391022TEST_F(URLRequestTestHTTP, CancelRedirect) {
1023 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521024 TestDelegate d;
1025 {
1026 d.set_cancel_in_received_redirect(true);
[email protected]37314622009-08-17 20:29:391027 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:521028 "files/redirect-test.html"), &d);
1029 req.Start();
1030 MessageLoop::current()->Run();
1031
1032 EXPECT_EQ(1, d.response_started_count());
1033 EXPECT_EQ(0, d.bytes_received());
1034 EXPECT_FALSE(d.received_data_before_response());
1035 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1036 }
1037}
1038
[email protected]37314622009-08-17 20:29:391039TEST_F(URLRequestTestHTTP, DeferredRedirect) {
1040 ASSERT_TRUE(NULL != server_.get());
[email protected]195e77d2009-07-23 19:10:231041 TestDelegate d;
1042 {
1043 d.set_quit_on_redirect(true);
[email protected]37314622009-08-17 20:29:391044 TestURLRequest req(server_->TestServerPage(
[email protected]195e77d2009-07-23 19:10:231045 "files/redirect-test.html"), &d);
1046 req.Start();
1047 MessageLoop::current()->Run();
1048
1049 EXPECT_EQ(1, d.received_redirect_count());
1050
1051 req.FollowDeferredRedirect();
1052 MessageLoop::current()->Run();
1053
1054 EXPECT_EQ(1, d.response_started_count());
1055 EXPECT_FALSE(d.received_data_before_response());
1056 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
1057
1058 FilePath path;
1059 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1060 path = path.Append(FILE_PATH_LITERAL("net"));
1061 path = path.Append(FILE_PATH_LITERAL("data"));
1062 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1063 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
1064
1065 std::string contents;
1066 EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
1067 EXPECT_EQ(contents, d.data_received());
1068 }
1069}
1070
[email protected]37314622009-08-17 20:29:391071TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
1072 ASSERT_TRUE(NULL != server_.get());
[email protected]195e77d2009-07-23 19:10:231073 TestDelegate d;
1074 {
1075 d.set_quit_on_redirect(true);
[email protected]37314622009-08-17 20:29:391076 TestURLRequest req(server_->TestServerPage(
[email protected]195e77d2009-07-23 19:10:231077 "files/redirect-test.html"), &d);
1078 req.Start();
1079 MessageLoop::current()->Run();
1080
1081 EXPECT_EQ(1, d.received_redirect_count());
1082
1083 req.Cancel();
1084 MessageLoop::current()->Run();
1085
1086 EXPECT_EQ(1, d.response_started_count());
1087 EXPECT_EQ(0, d.bytes_received());
1088 EXPECT_FALSE(d.received_data_before_response());
1089 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1090 }
1091}
1092
[email protected]37314622009-08-17 20:29:391093TEST_F(URLRequestTestHTTP, VaryHeader) {
1094 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521095
[email protected]ba2f3342009-07-30 18:08:421096 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
initial.commit586acc5fe2008-07-26 22:42:521097
initial.commit586acc5fe2008-07-26 22:42:521098 // populate the cache
1099 {
1100 TestDelegate d;
[email protected]37314622009-08-17 20:29:391101 URLRequest req(server_->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521102 req.set_context(context);
1103 req.SetExtraRequestHeaders("foo:1");
1104 req.Start();
1105 MessageLoop::current()->Run();
initial.commit586acc5fe2008-07-26 22:42:521106 }
1107
initial.commit586acc5fe2008-07-26 22:42:521108 // expect a cache hit
1109 {
1110 TestDelegate d;
[email protected]37314622009-08-17 20:29:391111 URLRequest req(server_->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521112 req.set_context(context);
1113 req.SetExtraRequestHeaders("foo:1");
1114 req.Start();
1115 MessageLoop::current()->Run();
1116
[email protected]cb4ff9d2009-09-04 22:51:531117 EXPECT_TRUE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521118 }
1119
1120 // expect a cache miss
1121 {
1122 TestDelegate d;
[email protected]37314622009-08-17 20:29:391123 URLRequest req(server_->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521124 req.set_context(context);
1125 req.SetExtraRequestHeaders("foo:2");
1126 req.Start();
1127 MessageLoop::current()->Run();
1128
[email protected]cb4ff9d2009-09-04 22:51:531129 EXPECT_FALSE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521130 }
1131}
1132
[email protected]cb4ff9d2009-09-04 22:51:531133TEST_F(URLRequestTestHTTP, BasicAuth) {
[email protected]ba2f3342009-07-30 18:08:421134 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
[email protected]37314622009-08-17 20:29:391135 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521136
initial.commit586acc5fe2008-07-26 22:42:521137 // populate the cache
1138 {
1139 TestDelegate d;
1140 d.set_username(L"user");
1141 d.set_password(L"secret");
1142
[email protected]37314622009-08-17 20:29:391143 URLRequest r(server_->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:521144 r.set_context(context);
1145 r.Start();
1146
1147 MessageLoop::current()->Run();
1148
1149 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
initial.commit586acc5fe2008-07-26 22:42:521150 }
1151
initial.commit586acc5fe2008-07-26 22:42:521152 // repeat request with end-to-end validation. since auth-basic results in a
1153 // cachable page, we expect this test to result in a 304. in which case, the
1154 // response should be fetched from the cache.
1155 {
1156 TestDelegate d;
1157 d.set_username(L"user");
1158 d.set_password(L"secret");
1159
[email protected]37314622009-08-17 20:29:391160 URLRequest r(server_->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:521161 r.set_context(context);
1162 r.set_load_flags(net::LOAD_VALIDATE_CACHE);
1163 r.Start();
1164
1165 MessageLoop::current()->Run();
1166
1167 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1168
[email protected]cb4ff9d2009-09-04 22:51:531169 // Should be the same cached document.
1170 EXPECT_TRUE(r.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521171 }
1172}
license.botbf09a502008-08-24 00:55:551173
[email protected]0757e7702009-03-27 04:00:221174// Check that Set-Cookie headers in 401 responses are respected.
1175// http://crbug.com/6450
[email protected]be6fca6c2010-01-30 21:48:571176TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
[email protected]37314622009-08-17 20:29:391177 ASSERT_TRUE(NULL != server_.get());
[email protected]0757e7702009-03-27 04:00:221178
1179 GURL url_requiring_auth =
[email protected]37314622009-08-17 20:29:391180 server_->TestServerPage("auth-basic?set-cookie-if-challenged");
[email protected]0757e7702009-03-27 04:00:221181
1182 // Request a page that will give a 401 containing a Set-Cookie header.
1183 // Verify that when the transaction is restarted, it includes the new cookie.
1184 {
[email protected]ba2f3342009-07-30 18:08:421185 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
[email protected]0757e7702009-03-27 04:00:221186 TestDelegate d;
1187 d.set_username(L"user");
1188 d.set_password(L"secret");
1189
1190 URLRequest r(url_requiring_auth, &d);
1191 r.set_context(context);
1192 r.Start();
1193
1194 MessageLoop::current()->Run();
1195
1196 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1197
1198 // Make sure we sent the cookie in the restarted transaction.
1199 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1200 != std::string::npos);
1201 }
1202
1203 // Same test as above, except this time the restart is initiated earlier
1204 // (without user intervention since identity is embedded in the URL).
1205 {
[email protected]ba2f3342009-07-30 18:08:421206 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
[email protected]0757e7702009-03-27 04:00:221207 TestDelegate d;
1208
1209 GURL::Replacements replacements;
1210 std::string username("user2");
1211 std::string password("secret");
1212 replacements.SetUsernameStr(username);
1213 replacements.SetPasswordStr(password);
1214 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
1215
1216 URLRequest r(url_with_identity, &d);
1217 r.set_context(context);
1218 r.Start();
1219
1220 MessageLoop::current()->Run();
1221
1222 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
1223
1224 // Make sure we sent the cookie in the restarted transaction.
1225 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1226 != std::string::npos);
1227 }
1228}
1229
[email protected]be6fca6c2010-01-30 21:48:571230TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]861fcd52009-08-26 02:33:461231 scoped_refptr<HTTPTestServer> server =
1232 HTTPTestServer::CreateServer(L"", NULL);
1233 ASSERT_TRUE(NULL != server.get());
1234 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
1235
1236 // Set up a cookie.
1237 {
1238 TestDelegate d;
1239 URLRequest req(server->TestServerPage("set-cookie?CookieToNotSend=1"), &d);
1240 req.set_context(context);
1241 req.Start();
1242 MessageLoop::current()->Run();
1243 }
1244
1245 // Verify that the cookie is set.
1246 {
1247 TestDelegate d;
1248 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1249 req.set_context(context);
1250 req.Start();
1251 MessageLoop::current()->Run();
1252
1253 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1254 != std::string::npos);
1255 }
1256
1257 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
1258 {
1259 TestDelegate d;
1260 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1261 req.set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES);
1262 req.set_context(context);
1263 req.Start();
1264 MessageLoop::current()->Run();
1265
1266 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1267 == std::string::npos);
1268 }
1269}
1270
[email protected]be6fca6c2010-01-30 21:48:571271TEST_F(URLRequestTest, DoNotSaveCookies) {
[email protected]861fcd52009-08-26 02:33:461272 scoped_refptr<HTTPTestServer> server =
1273 HTTPTestServer::CreateServer(L"", NULL);
1274 ASSERT_TRUE(NULL != server.get());
1275 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
1276
1277 // Set up a cookie.
1278 {
1279 TestDelegate d;
1280 URLRequest req(server->TestServerPage("set-cookie?CookieToNotUpdate=2"),
1281 &d);
1282 req.set_context(context);
1283 req.Start();
1284 MessageLoop::current()->Run();
1285 }
1286
1287 // Try to set-up another cookie and update the previous cookie.
1288 {
1289 scoped_refptr<URLRequestContext> context = new URLRequestTestContext();
1290 TestDelegate d;
1291 URLRequest req(server->TestServerPage(
1292 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
1293 req.set_load_flags(net::LOAD_DO_NOT_SAVE_COOKIES);
1294 req.set_context(context);
1295 req.Start();
1296
1297 MessageLoop::current()->Run();
1298 }
1299
1300 // Verify the cookies weren't saved or updated.
1301 {
1302 TestDelegate d;
1303 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1304 req.set_context(context);
1305 req.Start();
1306 MessageLoop::current()->Run();
1307
1308 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1309 == std::string::npos);
1310 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1311 != std::string::npos);
1312 }
1313}
1314
[email protected]71c64f62008-11-15 04:36:511315// In this test, we do a POST which the server will 302 redirect.
1316// The subsequent transaction should use GET, and should not send the
1317// Content-Type header.
1318// http://code.google.com/p/chromium/issues/detail?id=843
[email protected]37314622009-08-17 20:29:391319TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
[email protected]195e77d2009-07-23 19:10:231320 const char kData[] = "hello world";
[email protected]37314622009-08-17 20:29:391321 ASSERT_TRUE(NULL != server_.get());
[email protected]71c64f62008-11-15 04:36:511322 TestDelegate d;
[email protected]37314622009-08-17 20:29:391323 TestURLRequest req(server_->TestServerPage("files/redirect-to-echoall"), &d);
[email protected]71c64f62008-11-15 04:36:511324 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:231325 req.set_upload(CreateSimpleUploadData(kData));
[email protected]71c64f62008-11-15 04:36:511326
1327 // Set headers (some of which are specific to the POST).
[email protected]71c64f62008-11-15 04:36:511328 req.SetExtraRequestHeaders(
[email protected]dd265012009-01-08 20:45:271329 "Content-Type: multipart/form-data; "
1330 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
1331 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
1332 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
[email protected]71c64f62008-11-15 04:36:511333 "Accept-Language: en-US,en\r\n"
1334 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
[email protected]195e77d2009-07-23 19:10:231335 "Content-Length: 11\r\n"
[email protected]a86c97cc2009-06-24 21:26:271336 "Origin: http://localhost:1337/");
[email protected]71c64f62008-11-15 04:36:511337 req.Start();
1338 MessageLoop::current()->Run();
1339
1340 std::string mime_type;
1341 req.GetMimeType(&mime_type);
1342 EXPECT_EQ("text/html", mime_type);
1343
1344 const std::string& data = d.data_received();
1345
1346 // Check that the post-specific headers were stripped:
1347 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
1348 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
1349 EXPECT_FALSE(ContainsString(data, "Origin:"));
1350
1351 // These extra request headers should not have been stripped.
1352 EXPECT_TRUE(ContainsString(data, "Accept:"));
1353 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
1354 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
1355}
1356
[email protected]37314622009-08-17 20:29:391357TEST_F(URLRequestTestHTTP, Post307RedirectPost) {
[email protected]195e77d2009-07-23 19:10:231358 const char kData[] = "hello world";
[email protected]37314622009-08-17 20:29:391359 ASSERT_TRUE(NULL != server_.get());
[email protected]140932f2008-12-12 03:58:061360 TestDelegate d;
[email protected]37314622009-08-17 20:29:391361 TestURLRequest req(server_->TestServerPage("files/redirect307-to-echo"),
[email protected]dd265012009-01-08 20:45:271362 &d);
[email protected]140932f2008-12-12 03:58:061363 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:231364 req.set_upload(CreateSimpleUploadData(kData).get());
1365 req.SetExtraRequestHeaders(
1366 "Content-Length: " + UintToString(sizeof(kData) - 1));
[email protected]140932f2008-12-12 03:58:061367 req.Start();
1368 MessageLoop::current()->Run();
[email protected]195e77d2009-07-23 19:10:231369 EXPECT_EQ("POST", req.method());
1370 EXPECT_EQ(kData, d.data_received());
[email protected]140932f2008-12-12 03:58:061371}
[email protected]dd265012009-01-08 20:45:271372
[email protected]a5c713f2009-04-16 21:05:471373// Custom URLRequestJobs for use with interceptor tests
1374class RestartTestJob : public URLRequestTestJob {
1375 public:
[email protected]a86c97cc2009-06-24 21:26:271376 explicit RestartTestJob(URLRequest* request)
1377 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:471378 protected:
1379 virtual void StartAsync() {
1380 this->NotifyRestartRequired();
1381 }
[email protected]5389bc72009-11-05 23:34:241382 private:
1383 ~RestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471384};
1385
1386class CancelTestJob : public URLRequestTestJob {
1387 public:
[email protected]a86c97cc2009-06-24 21:26:271388 explicit CancelTestJob(URLRequest* request)
1389 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:471390 protected:
1391 virtual void StartAsync() {
1392 request_->Cancel();
1393 }
[email protected]5389bc72009-11-05 23:34:241394 private:
1395 ~CancelTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471396};
1397
1398class CancelThenRestartTestJob : public URLRequestTestJob {
1399 public:
[email protected]a86c97cc2009-06-24 21:26:271400 explicit CancelThenRestartTestJob(URLRequest* request)
[email protected]a5c713f2009-04-16 21:05:471401 : URLRequestTestJob(request, true) {
1402 }
1403 protected:
1404 virtual void StartAsync() {
1405 request_->Cancel();
1406 this->NotifyRestartRequired();
1407 }
[email protected]5389bc72009-11-05 23:34:241408 private:
1409 ~CancelThenRestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471410};
1411
1412// An Interceptor for use with interceptor tests
1413class TestInterceptor : URLRequest::Interceptor {
1414 public:
1415 TestInterceptor()
1416 : intercept_main_request_(false), restart_main_request_(false),
1417 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1418 simulate_main_network_error_(false),
1419 intercept_redirect_(false), cancel_redirect_request_(false),
1420 intercept_final_response_(false), cancel_final_request_(false),
1421 did_intercept_main_(false), did_restart_main_(false),
1422 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1423 did_simulate_error_main_(false),
1424 did_intercept_redirect_(false), did_cancel_redirect_(false),
1425 did_intercept_final_(false), did_cancel_final_(false) {
1426 URLRequest::RegisterRequestInterceptor(this);
1427 }
1428
1429 ~TestInterceptor() {
1430 URLRequest::UnregisterRequestInterceptor(this);
1431 }
1432
1433 virtual URLRequestJob* MaybeIntercept(URLRequest* request) {
1434 if (restart_main_request_) {
1435 restart_main_request_ = false;
1436 did_restart_main_ = true;
1437 return new RestartTestJob(request);
1438 }
1439 if (cancel_main_request_) {
1440 cancel_main_request_ = false;
1441 did_cancel_main_ = true;
1442 return new CancelTestJob(request);
1443 }
1444 if (cancel_then_restart_main_request_) {
1445 cancel_then_restart_main_request_ = false;
1446 did_cancel_then_restart_main_ = true;
1447 return new CancelThenRestartTestJob(request);
1448 }
1449 if (simulate_main_network_error_) {
1450 simulate_main_network_error_ = false;
1451 did_simulate_error_main_ = true;
1452 // will error since the requeted url is not one of its canned urls
1453 return new URLRequestTestJob(request, true);
1454 }
1455 if (!intercept_main_request_)
1456 return NULL;
1457 intercept_main_request_ = false;
1458 did_intercept_main_ = true;
1459 return new URLRequestTestJob(request,
1460 main_headers_,
1461 main_data_,
1462 true);
1463 }
1464
1465 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
1466 const GURL& location) {
1467 if (cancel_redirect_request_) {
1468 cancel_redirect_request_ = false;
1469 did_cancel_redirect_ = true;
1470 return new CancelTestJob(request);
1471 }
1472 if (!intercept_redirect_)
1473 return NULL;
1474 intercept_redirect_ = false;
1475 did_intercept_redirect_ = true;
1476 return new URLRequestTestJob(request,
1477 redirect_headers_,
1478 redirect_data_,
1479 true);
1480 }
1481
1482 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) {
1483 if (cancel_final_request_) {
1484 cancel_final_request_ = false;
1485 did_cancel_final_ = true;
1486 return new CancelTestJob(request);
1487 }
1488 if (!intercept_final_response_)
1489 return NULL;
1490 intercept_final_response_ = false;
1491 did_intercept_final_ = true;
1492 return new URLRequestTestJob(request,
1493 final_headers_,
1494 final_data_,
1495 true);
1496 }
1497
1498 // Whether to intercept the main request, and if so the response to return.
1499 bool intercept_main_request_;
1500 std::string main_headers_;
1501 std::string main_data_;
1502
1503 // Other actions we take at MaybeIntercept time
1504 bool restart_main_request_;
1505 bool cancel_main_request_;
1506 bool cancel_then_restart_main_request_;
1507 bool simulate_main_network_error_;
1508
1509 // Whether to intercept redirects, and if so the response to return.
1510 bool intercept_redirect_;
1511 std::string redirect_headers_;
1512 std::string redirect_data_;
1513
1514 // Other actions we can take at MaybeInterceptRedirect time
1515 bool cancel_redirect_request_;
1516
1517 // Whether to intercept final response, and if so the response to return.
1518 bool intercept_final_response_;
1519 std::string final_headers_;
1520 std::string final_data_;
1521
1522 // Other actions we can take at MaybeInterceptResponse time
1523 bool cancel_final_request_;
1524
1525 // If we did something or not
1526 bool did_intercept_main_;
1527 bool did_restart_main_;
1528 bool did_cancel_main_;
1529 bool did_cancel_then_restart_main_;
1530 bool did_simulate_error_main_;
1531 bool did_intercept_redirect_;
1532 bool did_cancel_redirect_;
1533 bool did_intercept_final_;
1534 bool did_cancel_final_;
1535
1536 // Static getters for canned response header and data strings
1537
1538 static std::string ok_data() {
1539 return URLRequestTestJob::test_data_1();
1540 }
1541
1542 static std::string ok_headers() {
1543 return URLRequestTestJob::test_headers();
1544 }
1545
1546 static std::string redirect_data() {
1547 return std::string();
1548 }
1549
1550 static std::string redirect_headers() {
1551 return URLRequestTestJob::test_redirect_headers();
1552 }
1553
1554 static std::string error_data() {
1555 return std::string("ohhh nooooo mr. bill!");
1556 }
1557
1558 static std::string error_headers() {
1559 return URLRequestTestJob::test_error_headers();
1560 }
1561};
1562
1563TEST_F(URLRequestTest, Intercept) {
1564 TestInterceptor interceptor;
1565
1566 // intercept the main request and respond with a simple response
1567 interceptor.intercept_main_request_ = true;
1568 interceptor.main_headers_ = TestInterceptor::ok_headers();
1569 interceptor.main_data_ = TestInterceptor::ok_data();
1570
1571 TestDelegate d;
1572 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1573 URLRequest::UserData* user_data0 = new URLRequest::UserData();
1574 URLRequest::UserData* user_data1 = new URLRequest::UserData();
1575 URLRequest::UserData* user_data2 = new URLRequest::UserData();
1576 req.SetUserData(NULL, user_data0);
1577 req.SetUserData(&user_data1, user_data1);
1578 req.SetUserData(&user_data2, user_data2);
1579 req.set_method("GET");
1580 req.Start();
1581 MessageLoop::current()->Run();
1582
1583 // Make sure we can retrieve our specific user data
1584 EXPECT_EQ(user_data0, req.GetUserData(NULL));
1585 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
1586 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
1587
1588 // Check the interceptor got called as expected
1589 EXPECT_TRUE(interceptor.did_intercept_main_);
1590
1591 // Check we got one good response
1592 EXPECT_TRUE(req.status().is_success());
1593 EXPECT_EQ(200, req.response_headers()->response_code());
1594 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1595 EXPECT_EQ(1, d.response_started_count());
1596 EXPECT_EQ(0, d.received_redirect_count());
1597}
1598
1599TEST_F(URLRequestTest, InterceptRedirect) {
1600 TestInterceptor interceptor;
1601
1602 // intercept the main request and respond with a redirect
1603 interceptor.intercept_main_request_ = true;
1604 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1605 interceptor.main_data_ = TestInterceptor::redirect_data();
1606
1607 // intercept that redirect and respond a final OK response
1608 interceptor.intercept_redirect_ = true;
1609 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1610 interceptor.redirect_data_ = TestInterceptor::ok_data();
1611
1612 TestDelegate d;
1613 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1614 req.set_method("GET");
1615 req.Start();
1616 MessageLoop::current()->Run();
1617
1618 // Check the interceptor got called as expected
1619 EXPECT_TRUE(interceptor.did_intercept_main_);
1620 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1621
1622 // Check we got one good response
1623 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:111624 if (req.status().is_success()) {
1625 EXPECT_EQ(200, req.response_headers()->response_code());
1626 }
[email protected]a5c713f2009-04-16 21:05:471627 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1628 EXPECT_EQ(1, d.response_started_count());
1629 EXPECT_EQ(0, d.received_redirect_count());
1630}
1631
1632TEST_F(URLRequestTest, InterceptServerError) {
1633 TestInterceptor interceptor;
1634
1635 // intercept the main request to generate a server error response
1636 interceptor.intercept_main_request_ = true;
1637 interceptor.main_headers_ = TestInterceptor::error_headers();
1638 interceptor.main_data_ = TestInterceptor::error_data();
1639
1640 // intercept that error and respond with an OK response
1641 interceptor.intercept_final_response_ = true;
1642 interceptor.final_headers_ = TestInterceptor::ok_headers();
1643 interceptor.final_data_ = TestInterceptor::ok_data();
1644
1645 TestDelegate d;
1646 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1647 req.set_method("GET");
1648 req.Start();
1649 MessageLoop::current()->Run();
1650
1651 // Check the interceptor got called as expected
1652 EXPECT_TRUE(interceptor.did_intercept_main_);
1653 EXPECT_TRUE(interceptor.did_intercept_final_);
1654
1655 // Check we got one good response
1656 EXPECT_TRUE(req.status().is_success());
1657 EXPECT_EQ(200, req.response_headers()->response_code());
1658 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1659 EXPECT_EQ(1, d.response_started_count());
1660 EXPECT_EQ(0, d.received_redirect_count());
1661}
1662
1663TEST_F(URLRequestTest, InterceptNetworkError) {
1664 TestInterceptor interceptor;
1665
1666 // intercept the main request to simulate a network error
1667 interceptor.simulate_main_network_error_ = true;
1668
1669 // intercept that error and respond with an OK response
1670 interceptor.intercept_final_response_ = true;
1671 interceptor.final_headers_ = TestInterceptor::ok_headers();
1672 interceptor.final_data_ = TestInterceptor::ok_data();
1673
1674 TestDelegate d;
1675 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1676 req.set_method("GET");
1677 req.Start();
1678 MessageLoop::current()->Run();
1679
1680 // Check the interceptor got called as expected
1681 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1682 EXPECT_TRUE(interceptor.did_intercept_final_);
1683
1684 // Check we received one good response
1685 EXPECT_TRUE(req.status().is_success());
1686 EXPECT_EQ(200, req.response_headers()->response_code());
1687 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1688 EXPECT_EQ(1, d.response_started_count());
1689 EXPECT_EQ(0, d.received_redirect_count());
1690}
1691
1692TEST_F(URLRequestTest, InterceptRestartRequired) {
1693 TestInterceptor interceptor;
1694
1695 // restart the main request
1696 interceptor.restart_main_request_ = true;
1697
1698 // then intercept the new main request and respond with an OK response
1699 interceptor.intercept_main_request_ = true;
1700 interceptor.main_headers_ = TestInterceptor::ok_headers();
1701 interceptor.main_data_ = TestInterceptor::ok_data();
1702
1703 TestDelegate d;
1704 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1705 req.set_method("GET");
1706 req.Start();
1707 MessageLoop::current()->Run();
1708
1709 // Check the interceptor got called as expected
1710 EXPECT_TRUE(interceptor.did_restart_main_);
1711 EXPECT_TRUE(interceptor.did_intercept_main_);
1712
1713 // Check we received one good response
1714 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:111715 if (req.status().is_success()) {
1716 EXPECT_EQ(200, req.response_headers()->response_code());
1717 }
[email protected]a5c713f2009-04-16 21:05:471718 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1719 EXPECT_EQ(1, d.response_started_count());
1720 EXPECT_EQ(0, d.received_redirect_count());
1721}
1722
1723TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1724 TestInterceptor interceptor;
1725
1726 // intercept the main request and cancel from within the restarted job
1727 interceptor.cancel_main_request_ = true;
1728
1729 // setup to intercept final response and override it with an OK response
1730 interceptor.intercept_final_response_ = true;
1731 interceptor.final_headers_ = TestInterceptor::ok_headers();
1732 interceptor.final_data_ = TestInterceptor::ok_data();
1733
1734 TestDelegate d;
1735 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1736 req.set_method("GET");
1737 req.Start();
1738 MessageLoop::current()->Run();
1739
1740 // Check the interceptor got called as expected
1741 EXPECT_TRUE(interceptor.did_cancel_main_);
1742 EXPECT_FALSE(interceptor.did_intercept_final_);
1743
1744 // Check we see a canceled request
1745 EXPECT_FALSE(req.status().is_success());
1746 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1747}
1748
1749TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1750 TestInterceptor interceptor;
1751
1752 // intercept the main request and respond with a redirect
1753 interceptor.intercept_main_request_ = true;
1754 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1755 interceptor.main_data_ = TestInterceptor::redirect_data();
1756
1757 // intercept the redirect and cancel from within that job
1758 interceptor.cancel_redirect_request_ = true;
1759
1760 // setup to intercept final response and override it with an OK response
1761 interceptor.intercept_final_response_ = true;
1762 interceptor.final_headers_ = TestInterceptor::ok_headers();
1763 interceptor.final_data_ = TestInterceptor::ok_data();
1764
1765 TestDelegate d;
1766 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1767 req.set_method("GET");
1768 req.Start();
1769 MessageLoop::current()->Run();
1770
1771 // Check the interceptor got called as expected
1772 EXPECT_TRUE(interceptor.did_intercept_main_);
1773 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1774 EXPECT_FALSE(interceptor.did_intercept_final_);
1775
1776 // Check we see a canceled request
1777 EXPECT_FALSE(req.status().is_success());
1778 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1779}
1780
1781TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1782 TestInterceptor interceptor;
1783
1784 // intercept the main request to simulate a network error
1785 interceptor.simulate_main_network_error_ = true;
1786
1787 // setup to intercept final response and cancel from within that job
1788 interceptor.cancel_final_request_ = true;
1789
1790 TestDelegate d;
1791 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1792 req.set_method("GET");
1793 req.Start();
1794 MessageLoop::current()->Run();
1795
1796 // Check the interceptor got called as expected
1797 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1798 EXPECT_TRUE(interceptor.did_cancel_final_);
1799
1800 // Check we see a canceled request
1801 EXPECT_FALSE(req.status().is_success());
1802 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1803}
1804
1805TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1806 TestInterceptor interceptor;
1807
1808 // intercept the main request and cancel then restart from within that job
1809 interceptor.cancel_then_restart_main_request_ = true;
1810
1811 // setup to intercept final response and override it with an OK response
1812 interceptor.intercept_final_response_ = true;
1813 interceptor.final_headers_ = TestInterceptor::ok_headers();
1814 interceptor.final_data_ = TestInterceptor::ok_data();
1815
1816 TestDelegate d;
1817 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1818 req.set_method("GET");
1819 req.Start();
1820 MessageLoop::current()->Run();
1821
1822 // Check the interceptor got called as expected
1823 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1824 EXPECT_FALSE(interceptor.did_intercept_final_);
1825
1826 // Check we see a canceled request
1827 EXPECT_FALSE(req.status().is_success());
1828 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1829}
1830
[email protected]b89ca032009-08-31 21:41:311831class URLRequestTestFTP : public URLRequestTest {
1832 protected:
1833 static void SetUpTestCase() {
1834 server_ = FTPTestServer::CreateServer(L"");
1835 }
1836
1837 static void TearDownTestCase() {
1838 server_ = NULL;
1839 }
1840
1841 static scoped_refptr<FTPTestServer> server_;
1842};
1843
1844// static
1845scoped_refptr<FTPTestServer> URLRequestTestFTP::server_;
1846
[email protected]5accf7332009-11-24 03:41:381847// Flaky, see http://crbug.com/25045.
1848TEST_F(URLRequestTestFTP, FLAKY_FTPDirectoryListing) {
[email protected]a25e90e2009-09-09 17:05:371849 ASSERT_TRUE(NULL != server_.get());
1850 TestDelegate d;
1851 {
1852 TestURLRequest r(server_->TestServerPage("/"), &d);
1853 r.Start();
1854 EXPECT_TRUE(r.is_pending());
1855
1856 MessageLoop::current()->Run();
1857
1858 EXPECT_FALSE(r.is_pending());
1859 EXPECT_EQ(1, d.response_started_count());
1860 EXPECT_FALSE(d.received_data_before_response());
1861 EXPECT_LT(0, d.bytes_received());
1862 }
1863}
1864
[email protected]b89ca032009-08-31 21:41:311865TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) {
1866 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:021867 FilePath app_path;
[email protected]dd265012009-01-08 20:45:271868 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:021869 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:271870 TestDelegate d;
1871 {
[email protected]b89ca032009-08-31 21:41:311872 TestURLRequest r(server_->TestServerPage("/LICENSE"), &d);
[email protected]dd265012009-01-08 20:45:271873 r.Start();
1874 EXPECT_TRUE(r.is_pending());
1875
1876 MessageLoop::current()->Run();
1877
1878 int64 file_size = 0;
1879 file_util::GetFileSize(app_path, &file_size);
1880
[email protected]ba2f3342009-07-30 18:08:421881 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:271882 EXPECT_EQ(1, d.response_started_count());
1883 EXPECT_FALSE(d.received_data_before_response());
1884 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
1885 }
1886}
1887
[email protected]e9ecbd12009-12-20 18:44:401888// Flaky, see http://crbug.com/25045.
1889TEST_F(URLRequestTestFTP, FLAKY_FTPGetTest) {
[email protected]b89ca032009-08-31 21:41:311890 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:021891 FilePath app_path;
[email protected]dd265012009-01-08 20:45:271892 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:021893 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:271894 TestDelegate d;
1895 {
[email protected]b89ca032009-08-31 21:41:311896 TestURLRequest r(server_->TestServerPage("/LICENSE", "chrome", "chrome"),
1897 &d);
[email protected]dd265012009-01-08 20:45:271898 r.Start();
1899 EXPECT_TRUE(r.is_pending());
1900
1901 MessageLoop::current()->Run();
1902
1903 int64 file_size = 0;
1904 file_util::GetFileSize(app_path, &file_size);
1905
[email protected]ba2f3342009-07-30 18:08:421906 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:271907 EXPECT_EQ(1, d.response_started_count());
1908 EXPECT_FALSE(d.received_data_before_response());
1909 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
1910 }
1911}
1912
[email protected]b89ca032009-08-31 21:41:311913TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) {
1914 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:021915 FilePath app_path;
[email protected]dd265012009-01-08 20:45:271916 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:021917 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:271918 TestDelegate d;
1919 {
[email protected]b89ca032009-08-31 21:41:311920 TestURLRequest r(server_->TestServerPage("/LICENSE",
1921 "chrome", "wrong_password"), &d);
[email protected]dd265012009-01-08 20:45:271922 r.Start();
1923 EXPECT_TRUE(r.is_pending());
1924
1925 MessageLoop::current()->Run();
1926
1927 int64 file_size = 0;
1928 file_util::GetFileSize(app_path, &file_size);
1929
[email protected]ba2f3342009-07-30 18:08:421930 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:271931 EXPECT_EQ(1, d.response_started_count());
1932 EXPECT_FALSE(d.received_data_before_response());
1933 EXPECT_EQ(d.bytes_received(), 0);
1934 }
1935}
1936
[email protected]cde4e80d2009-10-16 19:58:151937// Flaky, see http://crbug.com/25045.
1938TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPasswordRestart) {
[email protected]b89ca032009-08-31 21:41:311939 ASSERT_TRUE(NULL != server_.get());
[email protected]8b8a197d2009-08-26 15:57:581940 FilePath app_path;
1941 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1942 app_path = app_path.AppendASCII("LICENSE");
1943 TestDelegate d;
1944 // Set correct login credentials. The delegate will be asked for them when
1945 // the initial login with wrong credentials will fail.
1946 d.set_username(L"chrome");
1947 d.set_password(L"chrome");
1948 {
[email protected]b89ca032009-08-31 21:41:311949 TestURLRequest r(server_->TestServerPage("/LICENSE",
1950 "chrome", "wrong_password"), &d);
[email protected]8b8a197d2009-08-26 15:57:581951 r.Start();
1952 EXPECT_TRUE(r.is_pending());
1953
1954 MessageLoop::current()->Run();
1955
1956 int64 file_size = 0;
1957 file_util::GetFileSize(app_path, &file_size);
1958
1959 EXPECT_FALSE(r.is_pending());
1960 EXPECT_EQ(1, d.response_started_count());
1961 EXPECT_FALSE(d.received_data_before_response());
1962 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
1963 }
1964}
1965
[email protected]b89ca032009-08-31 21:41:311966TEST_F(URLRequestTestFTP, FTPCheckWrongUser) {
1967 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:021968 FilePath app_path;
[email protected]dd265012009-01-08 20:45:271969 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:021970 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:271971 TestDelegate d;
1972 {
[email protected]b89ca032009-08-31 21:41:311973 TestURLRequest r(server_->TestServerPage("/LICENSE",
1974 "wrong_user", "chrome"), &d);
[email protected]dd265012009-01-08 20:45:271975 r.Start();
1976 EXPECT_TRUE(r.is_pending());
1977
1978 MessageLoop::current()->Run();
1979
1980 int64 file_size = 0;
1981 file_util::GetFileSize(app_path, &file_size);
1982
[email protected]ba2f3342009-07-30 18:08:421983 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:271984 EXPECT_EQ(1, d.response_started_count());
1985 EXPECT_FALSE(d.received_data_before_response());
1986 EXPECT_EQ(d.bytes_received(), 0);
1987 }
1988}
[email protected]8b8a197d2009-08-26 15:57:581989
[email protected]cde4e80d2009-10-16 19:58:151990// Flaky, see http://crbug.com/25045.
1991TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUserRestart) {
[email protected]b89ca032009-08-31 21:41:311992 ASSERT_TRUE(NULL != server_.get());
[email protected]8b8a197d2009-08-26 15:57:581993 FilePath app_path;
1994 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1995 app_path = app_path.AppendASCII("LICENSE");
1996 TestDelegate d;
1997 // Set correct login credentials. The delegate will be asked for them when
1998 // the initial login with wrong credentials will fail.
1999 d.set_username(L"chrome");
2000 d.set_password(L"chrome");
2001 {
[email protected]b89ca032009-08-31 21:41:312002 TestURLRequest r(server_->TestServerPage("/LICENSE",
2003 "wrong_user", "chrome"), &d);
[email protected]8b8a197d2009-08-26 15:57:582004 r.Start();
2005 EXPECT_TRUE(r.is_pending());
2006
2007 MessageLoop::current()->Run();
2008
2009 int64 file_size = 0;
2010 file_util::GetFileSize(app_path, &file_size);
2011
2012 EXPECT_FALSE(r.is_pending());
2013 EXPECT_EQ(1, d.response_started_count());
2014 EXPECT_FALSE(d.received_data_before_response());
2015 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2016 }
2017}
[email protected]60a3df52009-09-22 16:13:242018
[email protected]cde4e80d2009-10-16 19:58:152019// Flaky, see http://crbug.com/25045.
2020TEST_F(URLRequestTestFTP, FLAKY_FTPCacheURLCredentials) {
[email protected]60a3df52009-09-22 16:13:242021 ASSERT_TRUE(NULL != server_.get());
2022 FilePath app_path;
2023 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2024 app_path = app_path.AppendASCII("LICENSE");
2025
2026 scoped_ptr<TestDelegate> d(new TestDelegate);
2027 {
2028 // Pass correct login identity in the URL.
2029 TestURLRequest r(server_->TestServerPage("/LICENSE",
2030 "chrome", "chrome"),
2031 d.get());
2032 r.Start();
2033 EXPECT_TRUE(r.is_pending());
2034
2035 MessageLoop::current()->Run();
2036
2037 int64 file_size = 0;
2038 file_util::GetFileSize(app_path, &file_size);
2039
2040 EXPECT_FALSE(r.is_pending());
2041 EXPECT_EQ(1, d->response_started_count());
2042 EXPECT_FALSE(d->received_data_before_response());
2043 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2044 }
2045
2046 d.reset(new TestDelegate);
2047 {
2048 // This request should use cached identity from previous request.
2049 TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get());
2050 r.Start();
2051 EXPECT_TRUE(r.is_pending());
2052
2053 MessageLoop::current()->Run();
2054
2055 int64 file_size = 0;
2056 file_util::GetFileSize(app_path, &file_size);
2057
2058 EXPECT_FALSE(r.is_pending());
2059 EXPECT_EQ(1, d->response_started_count());
2060 EXPECT_FALSE(d->received_data_before_response());
2061 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2062 }
2063}
2064
[email protected]cde4e80d2009-10-16 19:58:152065// Flaky, see http://crbug.com/25045.
2066TEST_F(URLRequestTestFTP, FLAKY_FTPCacheLoginBoxCredentials) {
[email protected]60a3df52009-09-22 16:13:242067 ASSERT_TRUE(NULL != server_.get());
2068 FilePath app_path;
2069 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2070 app_path = app_path.AppendASCII("LICENSE");
2071
2072 scoped_ptr<TestDelegate> d(new TestDelegate);
2073 // Set correct login credentials. The delegate will be asked for them when
2074 // the initial login with wrong credentials will fail.
2075 d->set_username(L"chrome");
2076 d->set_password(L"chrome");
2077 {
2078 TestURLRequest r(server_->TestServerPage("/LICENSE",
2079 "chrome", "wrong_password"),
2080 d.get());
2081 r.Start();
2082 EXPECT_TRUE(r.is_pending());
2083
2084 MessageLoop::current()->Run();
2085
2086 int64 file_size = 0;
2087 file_util::GetFileSize(app_path, &file_size);
2088
2089 EXPECT_FALSE(r.is_pending());
2090 EXPECT_EQ(1, d->response_started_count());
2091 EXPECT_FALSE(d->received_data_before_response());
2092 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2093 }
2094
2095 // Use a new delegate without explicit credentials. The cached ones should be
2096 // used.
2097 d.reset(new TestDelegate);
2098 {
2099 // Don't pass wrong credentials in the URL, they would override valid cached
2100 // ones.
2101 TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get());
2102 r.Start();
2103 EXPECT_TRUE(r.is_pending());
2104
2105 MessageLoop::current()->Run();
2106
2107 int64 file_size = 0;
2108 file_util::GetFileSize(app_path, &file_size);
2109
2110 EXPECT_FALSE(r.is_pending());
2111 EXPECT_EQ(1, d->response_started_count());
2112 EXPECT_FALSE(d->received_data_before_response());
2113 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2114 }
2115}
[email protected]09a362d32009-09-24 18:01:332116
2117// Check that default A-L header is sent.
2118TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
2119 ASSERT_TRUE(NULL != server_.get());
2120 TestDelegate d;
2121 TestURLRequest req(server_->TestServerPage("echoheader?Accept-Language"), &d);
2122 req.set_context(new URLRequestTestContext());
2123 req.Start();
2124 MessageLoop::current()->Run();
2125 EXPECT_EQ(req.context()->accept_language(), d.data_received());
2126}
2127
2128// Check that if request overrides the A-L header, the default is not appended.
2129// See http://crbug.com/20894
2130TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
2131 ASSERT_TRUE(NULL != server_.get());
2132 TestDelegate d;
[email protected]eb834fb2009-10-23 16:09:412133 TestURLRequest
2134 req(server_->TestServerPage("echoheaderoverride?Accept-Language"), &d);
[email protected]09a362d32009-09-24 18:01:332135 req.set_context(new URLRequestTestContext());
2136 req.SetExtraRequestHeaders("Accept-Language: ru");
2137 req.Start();
2138 MessageLoop::current()->Run();
2139 EXPECT_EQ(std::string("ru"), d.data_received());
2140}
2141
2142// Check that default A-C header is sent.
2143TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) {
2144 ASSERT_TRUE(NULL != server_.get());
2145 TestDelegate d;
2146 TestURLRequest req(server_->TestServerPage("echoheader?Accept-Charset"), &d);
2147 req.set_context(new URLRequestTestContext());
2148 req.Start();
2149 MessageLoop::current()->Run();
2150 EXPECT_EQ(req.context()->accept_charset(), d.data_received());
2151}
2152
2153// Check that if request overrides the A-C header, the default is not appended.
2154// See http://crbug.com/20894
2155TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) {
2156 ASSERT_TRUE(NULL != server_.get());
2157 TestDelegate d;
[email protected]eb834fb2009-10-23 16:09:412158 TestURLRequest
2159 req(server_->TestServerPage("echoheaderoverride?Accept-Charset"), &d);
[email protected]09a362d32009-09-24 18:01:332160 req.set_context(new URLRequestTestContext());
2161 req.SetExtraRequestHeaders("Accept-Charset: koi-8r");
2162 req.Start();
2163 MessageLoop::current()->Run();
2164 EXPECT_EQ(std::string("koi-8r"), d.data_received());
2165}