blob: eb4a9bb117f40e1666564707831885edeb49f632 [file] [log] [blame]
[email protected]13c8a092010-07-29 06:15:441// Copyright (c) 2010 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"
[email protected]528c56d2010-07-30 19:28:4425#include "base/string_number_conversions.h"
[email protected]be1ce6a72010-08-03 14:35:2226#include "base/utf_string_conversions.h"
[email protected]0757e7702009-03-27 04:00:2227#include "net/base/cookie_monster.h"
[email protected]34602282010-02-03 22:14:1528#include "net/base/cookie_policy.h"
initial.commit586acc5fe2008-07-26 22:42:5229#include "net/base/load_flags.h"
[email protected]9e743cd2010-03-16 07:03:5330#include "net/base/net_log.h"
31#include "net/base/net_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5232#include "net/base/net_errors.h"
33#include "net/base/net_module.h"
34#include "net/base/net_util.h"
[email protected]195e77d2009-07-23 19:10:2335#include "net/base/upload_data.h"
initial.commit586acc5fe2008-07-26 22:42:5236#include "net/disk_cache/disk_cache.h"
[email protected]ba2f3342009-07-30 18:08:4237#include "net/ftp/ftp_network_layer.h"
initial.commit586acc5fe2008-07-26 22:42:5238#include "net/http/http_cache.h"
39#include "net/http/http_network_layer.h"
[email protected]88e6b6f32010-05-07 23:14:2540#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2141#include "net/http/http_response_headers.h"
[email protected]63de95b2008-12-10 04:11:2742#include "net/proxy/proxy_service.h"
[email protected]1b9565c2010-07-21 01:19:3143#include "net/test/test_server.h"
initial.commit586acc5fe2008-07-26 22:42:5244#include "net/url_request/url_request.h"
[email protected]7886a8c2009-08-21 04:11:0945#include "net/url_request/url_request_file_dir_job.h"
[email protected]bcb84f8b2009-08-31 16:20:1446#include "net/url_request/url_request_http_job.h"
[email protected]a5c713f2009-04-16 21:05:4747#include "net/url_request/url_request_test_job.h"
initial.commit586acc5fe2008-07-26 22:42:5248#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1549#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5250
[email protected]e1acf6f2008-10-27 20:43:3351using base::Time;
[email protected]3985ba82010-07-29 21:44:1252using net::kHTTPDefaultPort;
53using net::FTPTestServer;
54using net::HTTPTestServer;
55using net::HTTPSTestServer;
[email protected]e1acf6f2008-10-27 20:43:3356
initial.commit586acc5fe2008-07-26 22:42:5257namespace {
58
[email protected]13c8a092010-07-29 06:15:4459const string16 kChrome(ASCIIToUTF16("chrome"));
60const string16 kSecret(ASCIIToUTF16("secret"));
61const string16 kUser(ASCIIToUTF16("user"));
62
[email protected]8a16266e2009-09-10 21:08:3963base::StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:5264 return "header";
65}
66
[email protected]71c64f62008-11-15 04:36:5167// Do a case-insensitive search through |haystack| for |needle|.
68bool ContainsString(const std::string& haystack, const char* needle) {
69 std::string::const_iterator it =
70 std::search(haystack.begin(),
71 haystack.end(),
72 needle,
73 needle + strlen(needle),
74 CaseInsensitiveCompare<char>());
75 return it != haystack.end();
76}
77
[email protected]661376a2009-04-29 02:04:2378void FillBuffer(char* buffer, size_t len) {
79 static bool called = false;
80 if (!called) {
81 called = true;
82 int seed = static_cast<int>(Time::Now().ToInternalValue());
83 srand(seed);
84 }
85
86 for (size_t i = 0; i < len; i++) {
87 buffer[i] = static_cast<char>(rand());
88 if (!buffer[i])
89 buffer[i] = 'g';
90 }
91}
92
[email protected]195e77d2009-07-23 19:10:2393scoped_refptr<net::UploadData> CreateSimpleUploadData(const char* data) {
94 scoped_refptr<net::UploadData> upload = new net::UploadData;
95 upload->AppendBytes(data, strlen(data));
96 return upload;
97}
98
[email protected]9396b252008-09-29 17:29:3899} // namespace
initial.commit586acc5fe2008-07-26 22:42:52100
[email protected]7a0bb4bf2008-11-19 21:41:48101// Inherit PlatformTest since we require the autorelease pool on Mac OS X.f
102class URLRequestTest : public PlatformTest {
103};
104
[email protected]b89290212009-08-14 22:37:35105class URLRequestTestHTTP : public URLRequestTest {
106 protected:
107 static void SetUpTestCase() {
[email protected]e70c6a82010-07-23 20:38:56108 server_ = HTTPTestServer::CreateServer(L"net/data/url_request_unittest/");
[email protected]b89290212009-08-14 22:37:35109 }
110
111 static void TearDownTestCase() {
112 server_ = NULL;
113 }
114
[email protected]762d2db2010-01-11 19:03:01115 void HTTPUploadDataOperationTest(const std::string& method) {
116 ASSERT_TRUE(NULL != server_.get());
117 const int kMsgSize = 20000; // multiple of 10
118 const int kIterations = 50;
119 char *uploadBytes = new char[kMsgSize+1];
120 char *ptr = uploadBytes;
121 char marker = 'a';
122 for (int idx = 0; idx < kMsgSize/10; idx++) {
123 memcpy(ptr, "----------", 10);
124 ptr += 10;
125 if (idx % 100 == 0) {
126 ptr--;
127 *ptr++ = marker;
128 if (++marker > 'z')
129 marker = 'a';
130 }
131 }
132 uploadBytes[kMsgSize] = '\0';
133
[email protected]f72a1cc2010-04-30 07:17:30134 scoped_refptr<URLRequestContext> context = new TestURLRequestContext();
[email protected]762d2db2010-01-11 19:03:01135
136 for (int i = 0; i < kIterations; ++i) {
137 TestDelegate d;
138 URLRequest r(server_->TestServerPage("echo"), &d);
139 r.set_context(context);
140 r.set_method(method.c_str());
141
142 r.AppendBytesToUpload(uploadBytes, kMsgSize);
143
144 r.Start();
145 EXPECT_TRUE(r.is_pending());
146
147 MessageLoop::current()->Run();
148
149 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
150 (int) r.status().status() << ", os error: " << r.status().os_error();
151
152 EXPECT_FALSE(d.received_data_before_response());
153 EXPECT_EQ(uploadBytes, d.data_received());
154 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0);
155 EXPECT_EQ(d.data_received().compare(uploadBytes), 0);
156 }
157 delete[] uploadBytes;
158 }
159
[email protected]b89290212009-08-14 22:37:35160 static scoped_refptr<HTTPTestServer> server_;
161};
162
163// static
164scoped_refptr<HTTPTestServer> URLRequestTestHTTP::server_;
165
166TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]d1ec59082009-02-11 02:48:15167 // In this unit test, we're using the HTTPTestServer as a proxy server and
[email protected]dc651782009-02-14 01:45:08168 // issuing a CONNECT request with the magic host name "www.redirect.com".
169 // The HTTPTestServer will return a 302 response, which we should not
[email protected]d1ec59082009-02-11 02:48:15170 // follow.
[email protected]b89290212009-08-14 22:37:35171 ASSERT_TRUE(NULL != server_.get());
[email protected]d1ec59082009-02-11 02:48:15172 TestDelegate d;
173 {
174 URLRequest r(GURL("https://www.redirect.com/"), &d);
175 std::string proxy("localhost:");
[email protected]528c56d2010-07-30 19:28:44176 proxy.append(base::IntToString(kHTTPDefaultPort));
[email protected]d1ec59082009-02-11 02:48:15177 r.set_context(new TestURLRequestContext(proxy));
178
179 r.Start();
180 EXPECT_TRUE(r.is_pending());
181
182 MessageLoop::current()->Run();
183
[email protected]c744cf22009-02-27 07:28:08184 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
185 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08186 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:15187 // We should not have followed the redirect.
188 EXPECT_EQ(0, d.received_redirect_count());
189 }
190}
191
[email protected]b89290212009-08-14 22:37:35192TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]dc651782009-02-14 01:45:08193 // In this unit test, we're using the HTTPTestServer as a proxy server and
194 // issuing a CONNECT request with the magic host name "www.server-auth.com".
195 // The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:35196 ASSERT_TRUE(NULL != server_.get());
[email protected]dc651782009-02-14 01:45:08197 TestDelegate d;
198 {
199 URLRequest r(GURL("https://www.server-auth.com/"), &d);
200 std::string proxy("localhost:");
[email protected]528c56d2010-07-30 19:28:44201 proxy.append(base::IntToString(kHTTPDefaultPort));
[email protected]dc651782009-02-14 01:45:08202 r.set_context(new TestURLRequestContext(proxy));
203
204 r.Start();
205 EXPECT_TRUE(r.is_pending());
206
207 MessageLoop::current()->Run();
208
209 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]c744cf22009-02-27 07:28:08210 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08211 }
212}
213
[email protected]b89290212009-08-14 22:37:35214TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
215 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52216 TestDelegate d;
217 {
[email protected]b89290212009-08-14 22:37:35218 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52219
220 r.Start();
221 EXPECT_TRUE(r.is_pending());
222
223 MessageLoop::current()->Run();
224
225 EXPECT_EQ(1, d.response_started_count());
226 EXPECT_FALSE(d.received_data_before_response());
227 EXPECT_NE(0, d.bytes_received());
[email protected]c31a54592009-09-04 02:36:16228
[email protected]9e743cd2010-03-16 07:03:53229 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:52230 }
initial.commit586acc5fe2008-07-26 22:42:52231}
232
[email protected]b89290212009-08-14 22:37:35233TEST_F(URLRequestTestHTTP, GetTest) {
234 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52235 TestDelegate d;
236 {
[email protected]b89290212009-08-14 22:37:35237 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52238
239 r.Start();
240 EXPECT_TRUE(r.is_pending());
241
242 MessageLoop::current()->Run();
243
244 EXPECT_EQ(1, d.response_started_count());
245 EXPECT_FALSE(d.received_data_before_response());
246 EXPECT_NE(0, d.bytes_received());
247 }
[email protected]5d7b373e2009-09-02 07:19:03248}
249
[email protected]7844480a2009-12-16 21:18:58250TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
251 scoped_refptr<HTTPSTestServer> https_server =
252 HTTPSTestServer::CreateGoodServer(L"net/data/ssl/");
253 ASSERT_TRUE(NULL != https_server.get());
254 ASSERT_TRUE(NULL != server_.get());
255
256 // An https server is sent a request with an https referer,
257 // and responds with a redirect to an http url. The http
258 // server should not be sent the referer.
259 GURL http_destination = server_->TestServerPage("");
260 TestDelegate d;
261 TestURLRequest req(https_server->TestServerPage(
262 "server-redirect?" + http_destination.spec()), &d);
263 req.set_referrer("https://www.referrer.com/");
264 req.Start();
265 MessageLoop::current()->Run();
266
267 EXPECT_EQ(1, d.response_started_count());
268 EXPECT_EQ(1, d.received_redirect_count());
269 EXPECT_EQ(http_destination, req.url());
270 EXPECT_EQ(std::string(), req.referrer());
271}
272
[email protected]e70c6a82010-07-23 20:38:56273namespace {
274
275// Used by MakeGETRequest to implement sync load behavior.
276class SyncTestDelegate : public TestDelegate {
277 public:
278 SyncTestDelegate() : event_(false, false), success_(false) {
279 }
280 virtual void OnResponseCompleted(URLRequest* request) {
281 MessageLoop::current()->DeleteSoon(FROM_HERE, request);
282 success_ = request->status().is_success();
283 event_.Signal();
284 }
285 bool Wait(int64 secs) {
286 return event_.TimedWait(TimeDelta::FromSeconds(secs));
287 }
288 bool did_succeed() const { return success_; }
289 private:
290 base::WaitableEvent event_;
291 bool success_;
292 DISALLOW_COPY_AND_ASSIGN(SyncTestDelegate);
293};
294
295void StartGETRequest(const GURL& url, URLRequest::Delegate* delegate) {
296 URLRequest* request = new URLRequest(url, delegate);
297 request->set_context(new TestURLRequestContext());
298 request->set_method("GET");
299 request->Start();
300 EXPECT_TRUE(request->is_pending());
301}
302
303bool MakeGETRequest(const GURL& url) {
304 // Spin up a background thread for this request so that we have access to
305 // an IO message loop, and in cases where this thread already has an IO
306 // message loop, we also want to avoid spinning a nested message loop.
307 SyncTestDelegate d;
308 {
309 base::Thread io_thread("MakeGETRequest");
310 base::Thread::Options options;
311 options.message_loop_type = MessageLoop::TYPE_IO;
312 io_thread.StartWithOptions(options);
313 io_thread.message_loop()->PostTask(FROM_HERE, NewRunnableFunction(
314 &StartGETRequest, url, &d));
315
316 const int kWaitSeconds = 30;
317 if (!d.Wait(kWaitSeconds))
318 return false;
319 }
320 return d.did_succeed();
321}
322
323} // namespace
324
325// Some tests use browser javascript to fetch a 'kill' url that causes
326// the server to exit by itself (rather than letting TestServerLauncher's
327// destructor kill it). We now unit test this mechanism.
[email protected]73e0bba2009-02-19 22:57:09328TEST_F(URLRequestTest, QuitTest) {
[email protected]e70c6a82010-07-23 20:38:56329 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]73e0bba2009-02-19 22:57:09330 ASSERT_TRUE(NULL != server.get());
[email protected]e70c6a82010-07-23 20:38:56331 // Append the time to avoid problems where the kill page
332 // is being cached rather than being executed on the server
333 std::string page_name = StringPrintf("kill?%u",
334 static_cast<int>(base::Time::Now().ToInternalValue()));
335 int retry_count = 5;
336 while (retry_count > 0) {
337 bool r = MakeGETRequest(server->TestServerPage(page_name));
338 // BUG #1048625 causes the kill GET to fail. For now we just retry.
339 // Once the bug is fixed, we should remove the while loop and put back
340 // the following DCHECK.
341 // DCHECK(r);
342 if (r)
343 break;
344 retry_count--;
345 }
346 // Make sure we were successful in stopping the testserver.
347 EXPECT_LT(0, retry_count);
[email protected]73e0bba2009-02-19 22:57:09348 EXPECT_TRUE(server->WaitToFinish(20000));
[email protected]73e0bba2009-02-19 22:57:09349}
350
351class HTTPSRequestTest : public testing::Test {
[email protected]ea224582008-12-07 20:25:46352};
353
[email protected]ea224582008-12-07 20:25:46354
[email protected]5774ada2010-07-15 06:30:54355TEST_F(HTTPSRequestTest, HTTPSGetTest) {
[email protected]ea224582008-12-07 20:25:46356 // Note: tools/testserver/testserver.py does not need
357 // a working document root to server the pages / and /hello.html,
358 // so this test doesn't really need to specify a document root.
359 // But if it did, a good one would be net/data/ssl.
[email protected]dd265012009-01-08 20:45:27360 scoped_refptr<HTTPSTestServer> server =
[email protected]73e0bba2009-02-19 22:57:09361 HTTPSTestServer::CreateGoodServer(L"net/data/ssl");
[email protected]dd265012009-01-08 20:45:27362 ASSERT_TRUE(NULL != server.get());
[email protected]ea224582008-12-07 20:25:46363
[email protected]ea224582008-12-07 20:25:46364 TestDelegate d;
365 {
[email protected]dd265012009-01-08 20:45:27366 TestURLRequest r(server->TestServerPage(""), &d);
[email protected]ea224582008-12-07 20:25:46367
368 r.Start();
369 EXPECT_TRUE(r.is_pending());
370
371 MessageLoop::current()->Run();
372
373 EXPECT_EQ(1, d.response_started_count());
374 EXPECT_FALSE(d.received_data_before_response());
375 EXPECT_NE(0, d.bytes_received());
376 }
[email protected]ea224582008-12-07 20:25:46377}
378
[email protected]5774ada2010-07-15 06:30:54379TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]bacff652009-03-31 17:50:33380 scoped_refptr<HTTPSTestServer> server =
381 HTTPSTestServer::CreateMismatchedServer(L"net/data/ssl");
382 ASSERT_TRUE(NULL != server.get());
383
384 bool err_allowed = true;
385 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
386 TestDelegate d;
387 {
388 d.set_allow_certificate_errors(err_allowed);
389 TestURLRequest r(server->TestServerPage(""), &d);
390
391 r.Start();
392 EXPECT_TRUE(r.is_pending());
393
394 MessageLoop::current()->Run();
395
396 EXPECT_EQ(1, d.response_started_count());
397 EXPECT_FALSE(d.received_data_before_response());
398 EXPECT_TRUE(d.have_certificate_errors());
399 if (err_allowed)
400 EXPECT_NE(0, d.bytes_received());
401 else
402 EXPECT_EQ(0, d.bytes_received());
403 }
404 }
405}
406
[email protected]5774ada2010-07-15 06:30:54407TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]bacff652009-03-31 17:50:33408 scoped_refptr<HTTPSTestServer> server =
409 HTTPSTestServer::CreateExpiredServer(L"net/data/ssl");
410 ASSERT_TRUE(NULL != server.get());
411
412 // Iterate from false to true, just so that we do the opposite of the
413 // previous test in order to increase test coverage.
414 bool err_allowed = false;
415 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
416 TestDelegate d;
417 {
418 d.set_allow_certificate_errors(err_allowed);
419 TestURLRequest r(server->TestServerPage(""), &d);
420
421 r.Start();
422 EXPECT_TRUE(r.is_pending());
423
424 MessageLoop::current()->Run();
425
426 EXPECT_EQ(1, d.response_started_count());
427 EXPECT_FALSE(d.received_data_before_response());
428 EXPECT_TRUE(d.have_certificate_errors());
429 if (err_allowed)
430 EXPECT_NE(0, d.bytes_received());
431 else
432 EXPECT_EQ(0, d.bytes_received());
433 }
434 }
435}
[email protected]73e0bba2009-02-19 22:57:09436
[email protected]8df162a2010-08-07 01:10:02437namespace {
438
439class SSLClientAuthTestDelegate : public TestDelegate {
440 public:
441 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
442 }
443 virtual void OnCertificateRequested(
444 URLRequest* request,
445 net::SSLCertRequestInfo* cert_request_info) {
446 on_certificate_requested_count_++;
447 MessageLoop::current()->Quit();
448 }
449 int on_certificate_requested_count() {
450 return on_certificate_requested_count_;
451 }
452 private:
453 int on_certificate_requested_count_;
454};
455
456} // namespace
457
458// TODO(davidben): Test the rest of the code. Specifically,
459// - Filtering which certificates to select.
460// - Sending a certificate back.
461// - Getting a certificate request in an SSL renegotiation sending the
462// HTTP request.
463TEST_F(HTTPSRequestTest, ClientAuthTest) {
464 scoped_refptr<HTTPSTestServer> server =
465 HTTPSTestServer::CreateClientAuthServer(L"net/data/ssl");
466 ASSERT_TRUE(NULL != server.get());
467
468 SSLClientAuthTestDelegate d;
469 {
470 TestURLRequest r(server->TestServerPage(""), &d);
471
472 r.Start();
473 EXPECT_TRUE(r.is_pending());
474
475 MessageLoop::current()->Run();
476
477 EXPECT_EQ(1, d.on_certificate_requested_count());
478 EXPECT_FALSE(d.received_data_before_response());
479 EXPECT_EQ(0, d.bytes_received());
480 }
481}
482
[email protected]37314622009-08-17 20:29:39483TEST_F(URLRequestTestHTTP, CancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52484 TestDelegate d;
485 {
486 TestURLRequest r(GURL("http://www.google.com/"), &d);
487
488 r.Start();
489 EXPECT_TRUE(r.is_pending());
490
491 r.Cancel();
492
493 MessageLoop::current()->Run();
494
495 // We expect to receive OnResponseStarted even though the request has been
496 // cancelled.
497 EXPECT_EQ(1, d.response_started_count());
498 EXPECT_EQ(0, d.bytes_received());
499 EXPECT_FALSE(d.received_data_before_response());
500 }
initial.commit586acc5fe2008-07-26 22:42:52501}
502
[email protected]37314622009-08-17 20:29:39503TEST_F(URLRequestTestHTTP, CancelTest2) {
504 ASSERT_TRUE(NULL != server_.get());
[email protected]dd265012009-01-08 20:45:27505
506 // error C2446: '!=' : no conversion from 'HTTPTestServer *const '
507 // to 'const int'
508
initial.commit586acc5fe2008-07-26 22:42:52509 TestDelegate d;
510 {
[email protected]37314622009-08-17 20:29:39511 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52512
513 d.set_cancel_in_response_started(true);
514
515 r.Start();
516 EXPECT_TRUE(r.is_pending());
517
518 MessageLoop::current()->Run();
519
520 EXPECT_EQ(1, d.response_started_count());
521 EXPECT_EQ(0, d.bytes_received());
522 EXPECT_FALSE(d.received_data_before_response());
523 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
524 }
initial.commit586acc5fe2008-07-26 22:42:52525}
526
[email protected]37314622009-08-17 20:29:39527TEST_F(URLRequestTestHTTP, CancelTest3) {
528 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52529 TestDelegate d;
530 {
[email protected]37314622009-08-17 20:29:39531 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52532
533 d.set_cancel_in_received_data(true);
534
535 r.Start();
536 EXPECT_TRUE(r.is_pending());
537
538 MessageLoop::current()->Run();
539
540 EXPECT_EQ(1, d.response_started_count());
541 // There is no guarantee about how much data was received
542 // before the cancel was issued. It could have been 0 bytes,
543 // or it could have been all the bytes.
544 // EXPECT_EQ(0, d.bytes_received());
545 EXPECT_FALSE(d.received_data_before_response());
546 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
547 }
initial.commit586acc5fe2008-07-26 22:42:52548}
549
[email protected]37314622009-08-17 20:29:39550TEST_F(URLRequestTestHTTP, CancelTest4) {
551 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52552 TestDelegate d;
553 {
[email protected]37314622009-08-17 20:29:39554 TestURLRequest r(server_->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52555
556 r.Start();
557 EXPECT_TRUE(r.is_pending());
558
559 // The request will be implicitly canceled when it is destroyed. The
560 // test delegate must not post a quit message when this happens because
561 // this test doesn't actually have a message loop. The quit message would
562 // get put on this thread's message queue and the next test would exit
563 // early, causing problems.
564 d.set_quit_on_complete(false);
565 }
566 // expect things to just cleanup properly.
567
568 // we won't actually get a received reponse here because we've never run the
569 // message loop
570 EXPECT_FALSE(d.received_data_before_response());
571 EXPECT_EQ(0, d.bytes_received());
572}
573
[email protected]37314622009-08-17 20:29:39574TEST_F(URLRequestTestHTTP, CancelTest5) {
575 ASSERT_TRUE(NULL != server_.get());
[email protected]f72a1cc2010-04-30 07:17:30576 scoped_refptr<URLRequestContext> context = new TestURLRequestContext();
initial.commit586acc5fe2008-07-26 22:42:52577
578 // populate cache
579 {
580 TestDelegate d;
[email protected]37314622009-08-17 20:29:39581 URLRequest r(server_->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52582 r.set_context(context);
583 r.Start();
584 MessageLoop::current()->Run();
585 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
586 }
587
588 // cancel read from cache (see bug 990242)
589 {
590 TestDelegate d;
[email protected]37314622009-08-17 20:29:39591 URLRequest r(server_->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52592 r.set_context(context);
593 r.Start();
594 r.Cancel();
595 MessageLoop::current()->Run();
596
597 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
598 EXPECT_EQ(1, d.response_started_count());
599 EXPECT_EQ(0, d.bytes_received());
600 EXPECT_FALSE(d.received_data_before_response());
601 }
initial.commit586acc5fe2008-07-26 22:42:52602}
603
[email protected]37314622009-08-17 20:29:39604TEST_F(URLRequestTestHTTP, PostTest) {
[email protected]762d2db2010-01-11 19:03:01605 HTTPUploadDataOperationTest("POST");
606}
initial.commit586acc5fe2008-07-26 22:42:52607
[email protected]762d2db2010-01-11 19:03:01608TEST_F(URLRequestTestHTTP, PutTest) {
609 HTTPUploadDataOperationTest("PUT");
initial.commit586acc5fe2008-07-26 22:42:52610}
611
[email protected]37314622009-08-17 20:29:39612TEST_F(URLRequestTestHTTP, PostEmptyTest) {
613 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52614 TestDelegate d;
615 {
[email protected]37314622009-08-17 20:29:39616 TestURLRequest r(server_->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52617 r.set_method("POST");
618
619 r.Start();
620 EXPECT_TRUE(r.is_pending());
621
622 MessageLoop::current()->Run();
623
624 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
625 (int) r.status().status() << ", os error: " << r.status().os_error();
626
627 EXPECT_FALSE(d.received_data_before_response());
628 EXPECT_TRUE(d.data_received().empty());
629 }
initial.commit586acc5fe2008-07-26 22:42:52630}
631
[email protected]37314622009-08-17 20:29:39632TEST_F(URLRequestTestHTTP, PostFileTest) {
633 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52634 TestDelegate d;
635 {
[email protected]37314622009-08-17 20:29:39636 TestURLRequest r(server_->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52637 r.set_method("POST");
638
[email protected]399b8702009-05-01 20:34:02639 FilePath dir;
initial.commit586acc5fe2008-07-26 22:42:52640 PathService::Get(base::DIR_EXE, &dir);
[email protected]9396b252008-09-29 17:29:38641 file_util::SetCurrentDirectory(dir);
initial.commit586acc5fe2008-07-26 22:42:52642
[email protected]72cbd322009-04-07 10:17:12643 FilePath path;
initial.commit586acc5fe2008-07-26 22:42:52644 PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]72cbd322009-04-07 10:17:12645 path = path.Append(FILE_PATH_LITERAL("net"));
646 path = path.Append(FILE_PATH_LITERAL("data"));
647 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
648 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
initial.commit586acc5fe2008-07-26 22:42:52649 r.AppendFileToUpload(path);
650
651 // This file should just be ignored in the upload stream.
[email protected]72cbd322009-04-07 10:17:12652 r.AppendFileToUpload(FilePath(FILE_PATH_LITERAL(
653 "c:\\path\\to\\non\\existant\\file.randomness.12345")));
initial.commit586acc5fe2008-07-26 22:42:52654
655 r.Start();
656 EXPECT_TRUE(r.is_pending());
657
658 MessageLoop::current()->Run();
659
[email protected]10a1fe92008-11-04 21:47:02660 int64 longsize;
661 ASSERT_EQ(true, file_util::GetFileSize(path, &longsize));
662 int size = static_cast<int>(longsize);
initial.commit586acc5fe2008-07-26 22:42:52663 scoped_array<char> buf(new char[size]);
664
[email protected]dd265012009-01-08 20:45:27665 int size_read = static_cast<int>(file_util::ReadFile(path,
666 buf.get(), size));
[email protected]eac0709a2008-11-04 21:00:46667 ASSERT_EQ(size, size_read);
initial.commit586acc5fe2008-07-26 22:42:52668
669 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
670 (int) r.status().status() << ", os error: " << r.status().os_error();
671
672 EXPECT_FALSE(d.received_data_before_response());
673
674 ASSERT_EQ(size, d.bytes_received());
675 EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size));
676 }
initial.commit586acc5fe2008-07-26 22:42:52677}
678
[email protected]7a0bb4bf2008-11-19 21:41:48679TEST_F(URLRequestTest, AboutBlankTest) {
initial.commit586acc5fe2008-07-26 22:42:52680 TestDelegate d;
681 {
682 TestURLRequest r(GURL("about:blank"), &d);
683
684 r.Start();
685 EXPECT_TRUE(r.is_pending());
686
687 MessageLoop::current()->Run();
688
689 EXPECT_TRUE(!r.is_pending());
690 EXPECT_FALSE(d.received_data_before_response());
691 EXPECT_EQ(d.bytes_received(), 0);
692 }
initial.commit586acc5fe2008-07-26 22:42:52693}
694
[email protected]fcb7fd92009-10-22 04:18:58695TEST_F(URLRequestTest, DataURLImageTest) {
696 TestDelegate d;
697 {
698 // Use our nice little Chrome logo.
699 TestURLRequest r(GURL(
700 "data:image/png;base64,"
701 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
702 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
703 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
704 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
705 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
706 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
707 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
708 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
709 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
710 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
711 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
712 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
713 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
714 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
715 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
716 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
717 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
718 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
719 &d);
720
721 r.Start();
722 EXPECT_TRUE(r.is_pending());
723
724 MessageLoop::current()->Run();
725
726 EXPECT_TRUE(!r.is_pending());
727 EXPECT_FALSE(d.received_data_before_response());
728 EXPECT_EQ(d.bytes_received(), 911);
729 }
730}
731
[email protected]7a0bb4bf2008-11-19 21:41:48732TEST_F(URLRequestTest, FileTest) {
[email protected]b9e04f02008-11-27 04:03:57733 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52734 PathService::Get(base::FILE_EXE, &app_path);
[email protected]72cbd322009-04-07 10:17:12735 GURL app_url = net::FilePathToFileURL(app_path);
initial.commit586acc5fe2008-07-26 22:42:52736
737 TestDelegate d;
738 {
[email protected]b9e04f02008-11-27 04:03:57739 TestURLRequest r(app_url, &d);
initial.commit586acc5fe2008-07-26 22:42:52740
741 r.Start();
742 EXPECT_TRUE(r.is_pending());
743
744 MessageLoop::current()->Run();
745
[email protected]b77280c2009-03-20 17:27:47746 int64 file_size = -1;
747 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
initial.commit586acc5fe2008-07-26 22:42:52748
749 EXPECT_TRUE(!r.is_pending());
750 EXPECT_EQ(1, d.response_started_count());
751 EXPECT_FALSE(d.received_data_before_response());
[email protected]9396b252008-09-29 17:29:38752 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
initial.commit586acc5fe2008-07-26 22:42:52753 }
initial.commit586acc5fe2008-07-26 22:42:52754}
755
[email protected]661376a2009-04-29 02:04:23756TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
757 const size_t buffer_size = 4000;
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 const size_t first_byte_position = 500;
770 const size_t last_byte_position = buffer_size - first_byte_position;
771 const size_t content_length = last_byte_position - first_byte_position + 1;
772 std::string partial_buffer_string(buffer.get() + first_byte_position,
773 buffer.get() + last_byte_position + 1);
774
775 TestDelegate d;
776 {
777 TestURLRequest r(temp_url, &d);
778
[email protected]88e6b6f32010-05-07 23:14:25779 net::HttpRequestHeaders headers;
780 headers.SetHeader(net::HttpRequestHeaders::kRange,
781 StringPrintf("bytes=%" PRIuS "-%" PRIuS,
782 first_byte_position, last_byte_position));
783 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:23784 r.Start();
785 EXPECT_TRUE(r.is_pending());
786
787 MessageLoop::current()->Run();
788 EXPECT_TRUE(!r.is_pending());
789 EXPECT_EQ(1, d.response_started_count());
790 EXPECT_FALSE(d.received_data_before_response());
791 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
792 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
793 EXPECT_TRUE(partial_buffer_string == d.data_received());
794 }
795
796 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23797}
798
799TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
800 const size_t buffer_size = 4000;
801 scoped_array<char> buffer(new char[buffer_size]);
802 FillBuffer(buffer.get(), buffer_size);
803
804 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55805 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23806 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53807 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23808
809 int64 file_size;
810 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
811
812 const size_t first_byte_position = 500;
813 const size_t last_byte_position = buffer_size - 1;
814 const size_t content_length = last_byte_position - first_byte_position + 1;
815 std::string partial_buffer_string(buffer.get() + first_byte_position,
816 buffer.get() + last_byte_position + 1);
817
818 TestDelegate d;
819 {
820 TestURLRequest r(temp_url, &d);
821
[email protected]88e6b6f32010-05-07 23:14:25822 net::HttpRequestHeaders headers;
823 headers.SetHeader(net::HttpRequestHeaders::kRange,
824 StringPrintf("bytes=%" PRIuS "-",
825 first_byte_position));
826 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:23827 r.Start();
828 EXPECT_TRUE(r.is_pending());
829
830 MessageLoop::current()->Run();
831 EXPECT_TRUE(!r.is_pending());
832 EXPECT_EQ(1, d.response_started_count());
833 EXPECT_FALSE(d.received_data_before_response());
834 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
835 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
836 EXPECT_TRUE(partial_buffer_string == d.data_received());
837 }
838
839 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23840}
841
842TEST_F(URLRequestTest, FileTestMultipleRanges) {
843 const size_t buffer_size = 400000;
844 scoped_array<char> buffer(new char[buffer_size]);
845 FillBuffer(buffer.get(), buffer_size);
846
847 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55848 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23849 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53850 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23851
852 int64 file_size;
853 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
854
855 TestDelegate d;
856 {
857 TestURLRequest r(temp_url, &d);
858
[email protected]88e6b6f32010-05-07 23:14:25859 net::HttpRequestHeaders headers;
860 headers.SetHeader(net::HttpRequestHeaders::kRange,
861 "bytes=0-0,10-200,200-300");
862 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:23863 r.Start();
864 EXPECT_TRUE(r.is_pending());
865
866 MessageLoop::current()->Run();
867 EXPECT_TRUE(d.request_failed());
868 }
869
870 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23871}
872
[email protected]7a0bb4bf2008-11-19 21:41:48873TEST_F(URLRequestTest, InvalidUrlTest) {
initial.commit586acc5fe2008-07-26 22:42:52874 TestDelegate d;
875 {
876 TestURLRequest r(GURL("invalid url"), &d);
877
878 r.Start();
879 EXPECT_TRUE(r.is_pending());
880
881 MessageLoop::current()->Run();
882 EXPECT_TRUE(d.request_failed());
883 }
initial.commit586acc5fe2008-07-26 22:42:52884}
885
[email protected]37314622009-08-17 20:29:39886TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
887 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:52888 TestDelegate d;
[email protected]37314622009-08-17 20:29:39889 TestURLRequest req(server_->TestServerPage("files/with-headers.html"), &d);
initial.commit586acc5fe2008-07-26 22:42:52890 req.Start();
891 MessageLoop::current()->Run();
892
893 const net::HttpResponseHeaders* headers = req.response_headers();
[email protected]589deddb2009-10-05 23:41:40894
895 // Simple sanity check that response_info() accesses the same data.
896 EXPECT_EQ(headers, req.response_info().headers.get());
897
initial.commit586acc5fe2008-07-26 22:42:52898 std::string header;
899 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
900 EXPECT_EQ("private", header);
901
902 header.clear();
903 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
904 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
905
906 // The response has two "X-Multiple-Entries" headers.
907 // This verfies our output has them concatenated together.
908 header.clear();
909 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
910 EXPECT_EQ("a, b", header);
911}
912
[email protected]9396b252008-09-29 17:29:38913#if defined(OS_WIN)
[email protected]7a0bb4bf2008-11-19 21:41:48914TEST_F(URLRequestTest, ResolveShortcutTest) {
[email protected]399b8702009-05-01 20:34:02915 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52916 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:02917 app_path = app_path.AppendASCII("net");
918 app_path = app_path.AppendASCII("data");
919 app_path = app_path.AppendASCII("url_request_unittest");
920 app_path = app_path.AppendASCII("with-headers.html");
initial.commit586acc5fe2008-07-26 22:42:52921
[email protected]399b8702009-05-01 20:34:02922 std::wstring lnk_path = app_path.value() + L".lnk";
initial.commit586acc5fe2008-07-26 22:42:52923
924 HRESULT result;
925 IShellLink *shell = NULL;
926 IPersistFile *persist = NULL;
927
928 CoInitialize(NULL);
929 // Temporarily create a shortcut for test
930 result = CoCreateInstance(CLSID_ShellLink, NULL,
[email protected]5d7b373e2009-09-02 07:19:03931 CLSCTX_INPROC_SERVER, IID_IShellLink,
932 reinterpret_cast<LPVOID*>(&shell));
[email protected]d149ce82009-07-01 23:57:02933 ASSERT_TRUE(SUCCEEDED(result));
initial.commit586acc5fe2008-07-26 22:42:52934 result = shell->QueryInterface(IID_IPersistFile,
[email protected]5d7b373e2009-09-02 07:19:03935 reinterpret_cast<LPVOID*>(&persist));
[email protected]d149ce82009-07-01 23:57:02936 ASSERT_TRUE(SUCCEEDED(result));
[email protected]399b8702009-05-01 20:34:02937 result = shell->SetPath(app_path.value().c_str());
initial.commit586acc5fe2008-07-26 22:42:52938 EXPECT_TRUE(SUCCEEDED(result));
939 result = shell->SetDescription(L"ResolveShortcutTest");
940 EXPECT_TRUE(SUCCEEDED(result));
941 result = persist->Save(lnk_path.c_str(), TRUE);
942 EXPECT_TRUE(SUCCEEDED(result));
943 if (persist)
944 persist->Release();
945 if (shell)
946 shell->Release();
947
948 TestDelegate d;
949 {
[email protected]72cbd322009-04-07 10:17:12950 TestURLRequest r(net::FilePathToFileURL(FilePath(lnk_path)), &d);
initial.commit586acc5fe2008-07-26 22:42:52951
952 r.Start();
953 EXPECT_TRUE(r.is_pending());
954
955 MessageLoop::current()->Run();
956
957 WIN32_FILE_ATTRIBUTE_DATA data;
[email protected]399b8702009-05-01 20:34:02958 GetFileAttributesEx(app_path.value().c_str(),
959 GetFileExInfoStandard, &data);
960 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
initial.commit586acc5fe2008-07-26 22:42:52961 FILE_SHARE_READ, NULL, OPEN_EXISTING,
962 FILE_ATTRIBUTE_NORMAL, NULL);
963 EXPECT_NE(INVALID_HANDLE_VALUE, file);
964 scoped_array<char> buffer(new char[data.nFileSizeLow]);
965 DWORD read_size;
966 BOOL result;
967 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
968 &read_size, NULL);
969 std::string content(buffer.get(), read_size);
970 CloseHandle(file);
971
972 EXPECT_TRUE(!r.is_pending());
973 EXPECT_EQ(1, d.received_redirect_count());
974 EXPECT_EQ(content, d.data_received());
975 }
976
977 // Clean the shortcut
978 DeleteFile(lnk_path.c_str());
979 CoUninitialize();
initial.commit586acc5fe2008-07-26 22:42:52980}
[email protected]9396b252008-09-29 17:29:38981#endif // defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:52982
[email protected]37314622009-08-17 20:29:39983TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
984 ASSERT_TRUE(NULL != server_.get());
[email protected]dd265012009-01-08 20:45:27985
initial.commit586acc5fe2008-07-26 22:42:52986 TestDelegate d;
[email protected]37314622009-08-17 20:29:39987 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52988 "files/content-type-normalization.html"), &d);
989 req.Start();
990 MessageLoop::current()->Run();
991
992 std::string mime_type;
993 req.GetMimeType(&mime_type);
994 EXPECT_EQ("text/html", mime_type);
995
996 std::string charset;
997 req.GetCharset(&charset);
998 EXPECT_EQ("utf-8", charset);
999 req.Cancel();
1000}
1001
[email protected]7a0bb4bf2008-11-19 21:41:481002TEST_F(URLRequestTest, FileDirCancelTest) {
initial.commit586acc5fe2008-07-26 22:42:521003 // Put in mock resource provider.
[email protected]8ac1a752008-07-31 19:40:371004 net::NetModule::SetResourceProvider(TestNetResourceProvider);
initial.commit586acc5fe2008-07-26 22:42:521005
1006 TestDelegate d;
1007 {
[email protected]72cbd322009-04-07 10:17:121008 FilePath file_path;
initial.commit586acc5fe2008-07-26 22:42:521009 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
[email protected]72cbd322009-04-07 10:17:121010 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
1011 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
initial.commit586acc5fe2008-07-26 22:42:521012
[email protected]8ac1a752008-07-31 19:40:371013 TestURLRequest req(net::FilePathToFileURL(file_path), &d);
initial.commit586acc5fe2008-07-26 22:42:521014 req.Start();
1015 EXPECT_TRUE(req.is_pending());
1016
1017 d.set_cancel_in_received_data_pending(true);
1018
1019 MessageLoop::current()->Run();
1020 }
initial.commit586acc5fe2008-07-26 22:42:521021
1022 // Take out mock resource provider.
[email protected]8ac1a752008-07-31 19:40:371023 net::NetModule::SetResourceProvider(NULL);
initial.commit586acc5fe2008-07-26 22:42:521024}
1025
[email protected]7886a8c2009-08-21 04:11:091026TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
1027 // There is an implicit redirect when loading a file path that matches a
1028 // directory and does not end with a slash. Ensure that following such
1029 // redirects does not crash. See http://crbug.com/18686.
1030
1031 FilePath path;
1032 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1033 path = path.Append(FILE_PATH_LITERAL("net"));
1034 path = path.Append(FILE_PATH_LITERAL("data"));
1035 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1036
1037 TestDelegate d;
[email protected]7886a8c2009-08-21 04:11:091038 TestURLRequest req(net::FilePathToFileURL(path), &d);
1039 req.Start();
1040 MessageLoop::current()->Run();
1041
[email protected]0db55772010-06-14 22:27:441042 ASSERT_EQ(1, d.received_redirect_count());
1043 ASSERT_LT(0, d.bytes_received());
1044 ASSERT_FALSE(d.request_failed());
1045 ASSERT_TRUE(req.status().is_success());
[email protected]7886a8c2009-08-21 04:11:091046}
1047
[email protected]0db55772010-06-14 22:27:441048#if defined(OS_WIN)
1049// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
1050TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
1051 TestDelegate d;
1052 TestURLRequest req(GURL("file:///"), &d);
1053 req.Start();
1054 MessageLoop::current()->Run();
1055
1056 ASSERT_EQ(1, d.received_redirect_count());
1057 ASSERT_FALSE(req.status().is_success());
1058}
1059#endif
1060
[email protected]37314622009-08-17 20:29:391061TEST_F(URLRequestTestHTTP, RestrictRedirects) {
1062 ASSERT_TRUE(NULL != server_.get());
[email protected]dd265012009-01-08 20:45:271063
initial.commit586acc5fe2008-07-26 22:42:521064 TestDelegate d;
[email protected]37314622009-08-17 20:29:391065 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:521066 "files/redirect-to-file.html"), &d);
1067 req.Start();
1068 MessageLoop::current()->Run();
1069
1070 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1071 EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error());
1072}
1073
[email protected]37314622009-08-17 20:29:391074TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
1075 ASSERT_TRUE(NULL != server_.get());
[email protected]4e66ed12009-07-21 23:38:421076
1077 TestDelegate d;
[email protected]37314622009-08-17 20:29:391078 TestURLRequest req(server_->TestServerPage(
[email protected]4e66ed12009-07-21 23:38:421079 "files/redirect-to-invalid-url.html"), &d);
1080 req.Start();
1081 MessageLoop::current()->Run();
1082
1083 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1084 EXPECT_EQ(net::ERR_INVALID_URL, req.status().os_error());
1085}
1086
[email protected]37314622009-08-17 20:29:391087TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
1088 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521089 TestDelegate d;
[email protected]37314622009-08-17 20:29:391090 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:521091 "echoheader?Referer"), &d);
1092 req.set_referrer("http://user:[email protected]/");
1093 req.Start();
1094 MessageLoop::current()->Run();
1095
1096 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
1097}
1098
[email protected]37314622009-08-17 20:29:391099TEST_F(URLRequestTestHTTP, CancelRedirect) {
1100 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521101 TestDelegate d;
1102 {
1103 d.set_cancel_in_received_redirect(true);
[email protected]37314622009-08-17 20:29:391104 TestURLRequest req(server_->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:521105 "files/redirect-test.html"), &d);
1106 req.Start();
1107 MessageLoop::current()->Run();
1108
1109 EXPECT_EQ(1, d.response_started_count());
1110 EXPECT_EQ(0, d.bytes_received());
1111 EXPECT_FALSE(d.received_data_before_response());
1112 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1113 }
1114}
1115
[email protected]37314622009-08-17 20:29:391116TEST_F(URLRequestTestHTTP, DeferredRedirect) {
1117 ASSERT_TRUE(NULL != server_.get());
[email protected]195e77d2009-07-23 19:10:231118 TestDelegate d;
1119 {
1120 d.set_quit_on_redirect(true);
[email protected]37314622009-08-17 20:29:391121 TestURLRequest req(server_->TestServerPage(
[email protected]195e77d2009-07-23 19:10:231122 "files/redirect-test.html"), &d);
1123 req.Start();
1124 MessageLoop::current()->Run();
1125
1126 EXPECT_EQ(1, d.received_redirect_count());
1127
1128 req.FollowDeferredRedirect();
1129 MessageLoop::current()->Run();
1130
1131 EXPECT_EQ(1, d.response_started_count());
1132 EXPECT_FALSE(d.received_data_before_response());
1133 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
1134
1135 FilePath path;
1136 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1137 path = path.Append(FILE_PATH_LITERAL("net"));
1138 path = path.Append(FILE_PATH_LITERAL("data"));
1139 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1140 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
1141
1142 std::string contents;
1143 EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
1144 EXPECT_EQ(contents, d.data_received());
1145 }
1146}
1147
[email protected]37314622009-08-17 20:29:391148TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
1149 ASSERT_TRUE(NULL != server_.get());
[email protected]195e77d2009-07-23 19:10:231150 TestDelegate d;
1151 {
1152 d.set_quit_on_redirect(true);
[email protected]37314622009-08-17 20:29:391153 TestURLRequest req(server_->TestServerPage(
[email protected]195e77d2009-07-23 19:10:231154 "files/redirect-test.html"), &d);
1155 req.Start();
1156 MessageLoop::current()->Run();
1157
1158 EXPECT_EQ(1, d.received_redirect_count());
1159
1160 req.Cancel();
1161 MessageLoop::current()->Run();
1162
1163 EXPECT_EQ(1, d.response_started_count());
1164 EXPECT_EQ(0, d.bytes_received());
1165 EXPECT_FALSE(d.received_data_before_response());
1166 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1167 }
1168}
1169
[email protected]37314622009-08-17 20:29:391170TEST_F(URLRequestTestHTTP, VaryHeader) {
1171 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521172
[email protected]f72a1cc2010-04-30 07:17:301173 scoped_refptr<URLRequestContext> context = new TestURLRequestContext();
initial.commit586acc5fe2008-07-26 22:42:521174
initial.commit586acc5fe2008-07-26 22:42:521175 // populate the cache
1176 {
1177 TestDelegate d;
[email protected]37314622009-08-17 20:29:391178 URLRequest req(server_->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521179 req.set_context(context);
[email protected]88e6b6f32010-05-07 23:14:251180 net::HttpRequestHeaders headers;
1181 headers.SetHeader("foo", "1");
1182 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:521183 req.Start();
1184 MessageLoop::current()->Run();
initial.commit586acc5fe2008-07-26 22:42:521185 }
1186
initial.commit586acc5fe2008-07-26 22:42:521187 // expect a cache hit
1188 {
1189 TestDelegate d;
[email protected]37314622009-08-17 20:29:391190 URLRequest req(server_->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521191 req.set_context(context);
[email protected]88e6b6f32010-05-07 23:14:251192 net::HttpRequestHeaders headers;
1193 headers.SetHeader("foo", "1");
1194 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:521195 req.Start();
1196 MessageLoop::current()->Run();
1197
[email protected]cb4ff9d2009-09-04 22:51:531198 EXPECT_TRUE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521199 }
1200
1201 // expect a cache miss
1202 {
1203 TestDelegate d;
[email protected]37314622009-08-17 20:29:391204 URLRequest req(server_->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521205 req.set_context(context);
[email protected]88e6b6f32010-05-07 23:14:251206 net::HttpRequestHeaders headers;
1207 headers.SetHeader("foo", "2");
1208 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:521209 req.Start();
1210 MessageLoop::current()->Run();
1211
[email protected]cb4ff9d2009-09-04 22:51:531212 EXPECT_FALSE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521213 }
1214}
1215
[email protected]cb4ff9d2009-09-04 22:51:531216TEST_F(URLRequestTestHTTP, BasicAuth) {
[email protected]f72a1cc2010-04-30 07:17:301217 scoped_refptr<URLRequestContext> context = new TestURLRequestContext();
[email protected]37314622009-08-17 20:29:391218 ASSERT_TRUE(NULL != server_.get());
initial.commit586acc5fe2008-07-26 22:42:521219
initial.commit586acc5fe2008-07-26 22:42:521220 // populate the cache
1221 {
1222 TestDelegate d;
[email protected]13c8a092010-07-29 06:15:441223 d.set_username(kUser);
1224 d.set_password(kSecret);
initial.commit586acc5fe2008-07-26 22:42:521225
[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.Start();
1229
1230 MessageLoop::current()->Run();
1231
1232 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
initial.commit586acc5fe2008-07-26 22:42:521233 }
1234
initial.commit586acc5fe2008-07-26 22:42:521235 // repeat request with end-to-end validation. since auth-basic results in a
1236 // cachable page, we expect this test to result in a 304. in which case, the
1237 // response should be fetched from the cache.
1238 {
1239 TestDelegate d;
[email protected]13c8a092010-07-29 06:15:441240 d.set_username(kUser);
1241 d.set_password(kSecret);
initial.commit586acc5fe2008-07-26 22:42:521242
[email protected]37314622009-08-17 20:29:391243 URLRequest r(server_->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:521244 r.set_context(context);
1245 r.set_load_flags(net::LOAD_VALIDATE_CACHE);
1246 r.Start();
1247
1248 MessageLoop::current()->Run();
1249
1250 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1251
[email protected]cb4ff9d2009-09-04 22:51:531252 // Should be the same cached document.
1253 EXPECT_TRUE(r.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521254 }
1255}
license.botbf09a502008-08-24 00:55:551256
[email protected]0757e7702009-03-27 04:00:221257// Check that Set-Cookie headers in 401 responses are respected.
1258// http://crbug.com/6450
[email protected]be6fca6c2010-01-30 21:48:571259TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
[email protected]37314622009-08-17 20:29:391260 ASSERT_TRUE(NULL != server_.get());
[email protected]0757e7702009-03-27 04:00:221261
1262 GURL url_requiring_auth =
[email protected]37314622009-08-17 20:29:391263 server_->TestServerPage("auth-basic?set-cookie-if-challenged");
[email protected]0757e7702009-03-27 04:00:221264
1265 // Request a page that will give a 401 containing a Set-Cookie header.
1266 // Verify that when the transaction is restarted, it includes the new cookie.
1267 {
[email protected]f72a1cc2010-04-30 07:17:301268 scoped_refptr<URLRequestContext> context = new TestURLRequestContext();
[email protected]0757e7702009-03-27 04:00:221269 TestDelegate d;
[email protected]13c8a092010-07-29 06:15:441270 d.set_username(kUser);
1271 d.set_password(kSecret);
[email protected]0757e7702009-03-27 04:00:221272
1273 URLRequest r(url_requiring_auth, &d);
1274 r.set_context(context);
1275 r.Start();
1276
1277 MessageLoop::current()->Run();
1278
1279 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1280
1281 // Make sure we sent the cookie in the restarted transaction.
1282 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1283 != std::string::npos);
1284 }
1285
1286 // Same test as above, except this time the restart is initiated earlier
1287 // (without user intervention since identity is embedded in the URL).
1288 {
[email protected]f72a1cc2010-04-30 07:17:301289 scoped_refptr<URLRequestContext> context = new TestURLRequestContext();
[email protected]0757e7702009-03-27 04:00:221290 TestDelegate d;
1291
1292 GURL::Replacements replacements;
1293 std::string username("user2");
1294 std::string password("secret");
1295 replacements.SetUsernameStr(username);
1296 replacements.SetPasswordStr(password);
1297 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
1298
1299 URLRequest r(url_with_identity, &d);
1300 r.set_context(context);
1301 r.Start();
1302
1303 MessageLoop::current()->Run();
1304
1305 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
1306
1307 // Make sure we sent the cookie in the restarted transaction.
1308 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1309 != std::string::npos);
1310 }
1311}
1312
[email protected]be6fca6c2010-01-30 21:48:571313TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]e70c6a82010-07-23 20:38:561314 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]861fcd52009-08-26 02:33:461315 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301316 scoped_refptr<URLRequestContext> context = new TestURLRequestContext();
[email protected]861fcd52009-08-26 02:33:461317
1318 // Set up a cookie.
1319 {
1320 TestDelegate d;
1321 URLRequest req(server->TestServerPage("set-cookie?CookieToNotSend=1"), &d);
1322 req.set_context(context);
1323 req.Start();
1324 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201325 EXPECT_EQ(0, d.blocked_get_cookies_count());
1326 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461327 }
1328
1329 // Verify that the cookie is set.
1330 {
1331 TestDelegate d;
1332 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1333 req.set_context(context);
1334 req.Start();
1335 MessageLoop::current()->Run();
1336
1337 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1338 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201339 EXPECT_EQ(0, d.blocked_get_cookies_count());
1340 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461341 }
1342
1343 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
1344 {
1345 TestDelegate d;
1346 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1347 req.set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES);
1348 req.set_context(context);
1349 req.Start();
1350 MessageLoop::current()->Run();
1351
1352 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1353 == std::string::npos);
[email protected]abf03a02010-03-12 05:02:131354
[email protected]9fb83e82010-07-02 18:24:551355 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
[email protected]abf03a02010-03-12 05:02:131356 EXPECT_EQ(0, d.blocked_get_cookies_count());
[email protected]3dbb80b2010-02-09 22:41:201357 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461358 }
1359}
1360
[email protected]be6fca6c2010-01-30 21:48:571361TEST_F(URLRequestTest, DoNotSaveCookies) {
[email protected]e70c6a82010-07-23 20:38:561362 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]861fcd52009-08-26 02:33:461363 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301364 scoped_refptr<URLRequestContext> context = new TestURLRequestContext();
[email protected]861fcd52009-08-26 02:33:461365
1366 // Set up a cookie.
1367 {
1368 TestDelegate d;
1369 URLRequest req(server->TestServerPage("set-cookie?CookieToNotUpdate=2"),
1370 &d);
1371 req.set_context(context);
1372 req.Start();
1373 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201374
1375 EXPECT_EQ(0, d.blocked_get_cookies_count());
1376 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:551377 EXPECT_EQ(1, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461378 }
1379
1380 // Try to set-up another cookie and update the previous cookie.
1381 {
[email protected]861fcd52009-08-26 02:33:461382 TestDelegate d;
1383 URLRequest req(server->TestServerPage(
1384 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
1385 req.set_load_flags(net::LOAD_DO_NOT_SAVE_COOKIES);
1386 req.set_context(context);
1387 req.Start();
1388
1389 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201390
[email protected]9fb83e82010-07-02 18:24:551391 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
[email protected]3dbb80b2010-02-09 22:41:201392 EXPECT_EQ(0, d.blocked_get_cookies_count());
[email protected]abf03a02010-03-12 05:02:131393 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:551394 EXPECT_EQ(0, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461395 }
1396
1397 // Verify the cookies weren't saved or updated.
1398 {
1399 TestDelegate d;
1400 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1401 req.set_context(context);
1402 req.Start();
1403 MessageLoop::current()->Run();
1404
1405 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1406 == std::string::npos);
1407 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1408 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201409
1410 EXPECT_EQ(0, d.blocked_get_cookies_count());
1411 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:551412 EXPECT_EQ(0, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461413 }
1414}
1415
[email protected]34602282010-02-03 22:14:151416TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
[email protected]e70c6a82010-07-23 20:38:561417 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]34602282010-02-03 22:14:151418 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301419 scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext();
[email protected]34602282010-02-03 22:14:151420
1421 // Set up a cookie.
1422 {
1423 TestDelegate d;
1424 URLRequest req(server->TestServerPage("set-cookie?CookieToNotSend=1"), &d);
1425 req.set_context(context);
1426 req.Start();
1427 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201428
1429 EXPECT_EQ(0, d.blocked_get_cookies_count());
1430 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151431 }
1432
1433 // Verify that the cookie is set.
1434 {
1435 TestDelegate d;
1436 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1437 req.set_context(context);
1438 req.Start();
1439 MessageLoop::current()->Run();
1440
1441 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1442 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201443
1444 EXPECT_EQ(0, d.blocked_get_cookies_count());
1445 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151446 }
1447
1448 // Verify that the cookie isn't sent.
1449 {
1450 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES);
1451 context->set_cookie_policy(&cookie_policy);
1452
1453 TestDelegate d;
1454 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1455 req.set_context(context);
1456 req.Start();
1457 MessageLoop::current()->Run();
1458
1459 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1460 == std::string::npos);
1461
1462 context->set_cookie_policy(NULL);
[email protected]3dbb80b2010-02-09 22:41:201463
1464 EXPECT_EQ(1, d.blocked_get_cookies_count());
1465 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151466 }
1467}
1468
1469TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
[email protected]e70c6a82010-07-23 20:38:561470 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]34602282010-02-03 22:14:151471 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301472 scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext();
[email protected]34602282010-02-03 22:14:151473
1474 // Set up a cookie.
1475 {
1476 TestDelegate d;
1477 URLRequest req(server->TestServerPage("set-cookie?CookieToNotUpdate=2"),
1478 &d);
1479 req.set_context(context);
1480 req.Start();
1481 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201482
1483 EXPECT_EQ(0, d.blocked_get_cookies_count());
1484 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151485 }
1486
1487 // Try to set-up another cookie and update the previous cookie.
1488 {
1489 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE);
1490 context->set_cookie_policy(&cookie_policy);
1491
1492 TestDelegate d;
1493 URLRequest req(server->TestServerPage(
1494 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
1495 req.set_context(context);
1496 req.Start();
1497
1498 MessageLoop::current()->Run();
1499
1500 context->set_cookie_policy(NULL);
[email protected]3dbb80b2010-02-09 22:41:201501
1502 EXPECT_EQ(0, d.blocked_get_cookies_count());
1503 EXPECT_EQ(2, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151504 }
1505
1506
1507 // Verify the cookies weren't saved or updated.
1508 {
1509 TestDelegate d;
1510 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1511 req.set_context(context);
1512 req.Start();
1513 MessageLoop::current()->Run();
1514
1515 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1516 == std::string::npos);
1517 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1518 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201519
1520 EXPECT_EQ(0, d.blocked_get_cookies_count());
1521 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151522 }
1523}
1524
1525TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
[email protected]e70c6a82010-07-23 20:38:561526 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]34602282010-02-03 22:14:151527 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301528 scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext();
[email protected]34602282010-02-03 22:14:151529
1530 // Set up a cookie.
1531 {
1532 TestDelegate d;
1533 URLRequest req(server->TestServerPage("set-cookie?CookieToNotSend=1"), &d);
1534 req.set_context(context);
1535 req.Start();
1536 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201537
1538 EXPECT_EQ(0, d.blocked_get_cookies_count());
1539 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151540 }
1541
1542 // Verify that the cookie is set.
1543 {
1544 TestDelegate d;
1545 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1546 req.set_context(context);
1547 req.Start();
1548 MessageLoop::current()->Run();
1549
1550 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1551 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201552
1553 EXPECT_EQ(0, d.blocked_get_cookies_count());
1554 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151555 }
1556
1557 // Verify that the cookie isn't sent.
1558 {
1559 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES |
1560 TestCookiePolicy::ASYNC);
1561 context->set_cookie_policy(&cookie_policy);
1562
1563 TestDelegate d;
1564 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1565 req.set_context(context);
1566 req.Start();
1567 MessageLoop::current()->Run();
1568
1569 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1570 == std::string::npos);
1571
1572 context->set_cookie_policy(NULL);
[email protected]3dbb80b2010-02-09 22:41:201573
1574 EXPECT_EQ(1, d.blocked_get_cookies_count());
1575 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151576 }
1577}
1578
1579TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
[email protected]e70c6a82010-07-23 20:38:561580 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]34602282010-02-03 22:14:151581 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301582 scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext();
[email protected]34602282010-02-03 22:14:151583
1584 // Set up a cookie.
1585 {
1586 TestDelegate d;
1587 URLRequest req(server->TestServerPage("set-cookie?CookieToNotUpdate=2"),
1588 &d);
1589 req.set_context(context);
1590 req.Start();
1591 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201592
1593 EXPECT_EQ(0, d.blocked_get_cookies_count());
1594 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151595 }
1596
1597 // Try to set-up another cookie and update the previous cookie.
1598 {
1599 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE |
1600 TestCookiePolicy::ASYNC);
1601 context->set_cookie_policy(&cookie_policy);
1602
1603 TestDelegate d;
1604 URLRequest req(server->TestServerPage(
1605 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
1606 req.set_context(context);
1607 req.Start();
1608
1609 MessageLoop::current()->Run();
1610
1611 context->set_cookie_policy(NULL);
[email protected]3dbb80b2010-02-09 22:41:201612
1613 EXPECT_EQ(0, d.blocked_get_cookies_count());
1614 EXPECT_EQ(2, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151615 }
1616
1617 // Verify the cookies weren't saved or updated.
1618 {
1619 TestDelegate d;
1620 TestURLRequest req(server->TestServerPage("echoheader?Cookie"), &d);
1621 req.set_context(context);
1622 req.Start();
1623 MessageLoop::current()->Run();
1624
1625 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1626 == std::string::npos);
1627 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1628 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201629
1630 EXPECT_EQ(0, d.blocked_get_cookies_count());
1631 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151632 }
1633}
1634
[email protected]3dbb80b2010-02-09 22:41:201635TEST_F(URLRequestTest, CancelTest_During_CookiePolicy) {
[email protected]e70c6a82010-07-23 20:38:561636 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]34602282010-02-03 22:14:151637 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301638 scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext();
[email protected]34602282010-02-03 22:14:151639
1640 TestCookiePolicy cookie_policy(TestCookiePolicy::ASYNC);
1641 context->set_cookie_policy(&cookie_policy);
1642
1643 // Set up a cookie.
1644 {
1645 TestDelegate d;
1646 URLRequest req(server->TestServerPage("set-cookie?A=1&B=2&C=3"),
1647 &d);
1648 req.set_context(context);
1649 req.Start(); // Triggers an asynchronous cookie policy check.
1650
[email protected]3dbb80b2010-02-09 22:41:201651 // But, now we cancel the request by letting it go out of scope. This
1652 // should not cause a crash.
1653
1654 EXPECT_EQ(0, d.blocked_get_cookies_count());
1655 EXPECT_EQ(0, d.blocked_set_cookie_count());
1656 }
1657
1658 context->set_cookie_policy(NULL);
1659
1660 // Let the cookie policy complete. Make sure it handles the destruction of
1661 // the URLRequest properly.
1662 MessageLoop::current()->RunAllPending();
1663}
1664
[email protected]9fb83e82010-07-02 18:24:551665TEST_F(URLRequestTest, CancelTest_During_OnGetCookies) {
[email protected]e70c6a82010-07-23 20:38:561666 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]3dbb80b2010-02-09 22:41:201667 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301668 scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext();
[email protected]3dbb80b2010-02-09 22:41:201669
1670 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES);
1671 context->set_cookie_policy(&cookie_policy);
1672
1673 // Set up a cookie.
1674 {
1675 TestDelegate d;
1676 d.set_cancel_in_get_cookies_blocked(true);
1677 URLRequest req(server->TestServerPage("set-cookie?A=1&B=2&C=3"),
1678 &d);
1679 req.set_context(context);
1680 req.Start(); // Triggers an asynchronous cookie policy check.
1681
1682 MessageLoop::current()->Run();
1683
1684 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1685
1686 EXPECT_EQ(1, d.blocked_get_cookies_count());
1687 EXPECT_EQ(0, d.blocked_set_cookie_count());
1688 }
1689
1690 context->set_cookie_policy(NULL);
1691}
1692
[email protected]9fb83e82010-07-02 18:24:551693TEST_F(URLRequestTest, CancelTest_During_OnSetCookie) {
[email protected]e70c6a82010-07-23 20:38:561694 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]3dbb80b2010-02-09 22:41:201695 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301696 scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext();
[email protected]3dbb80b2010-02-09 22:41:201697
1698 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE);
1699 context->set_cookie_policy(&cookie_policy);
1700
1701 // Set up a cookie.
1702 {
1703 TestDelegate d;
1704 d.set_cancel_in_set_cookie_blocked(true);
1705 URLRequest req(server->TestServerPage("set-cookie?A=1&B=2&C=3"),
1706 &d);
1707 req.set_context(context);
1708 req.Start(); // Triggers an asynchronous cookie policy check.
1709
1710 MessageLoop::current()->Run();
1711
1712 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1713
1714 // Even though the response will contain 3 set-cookie headers, we expect
[email protected]9fb83e82010-07-02 18:24:551715 // only one to be blocked as that first one will cause OnSetCookie to be
1716 // called, which will cancel the request. Once canceled, it should not
1717 // attempt to set further cookies.
[email protected]3dbb80b2010-02-09 22:41:201718
1719 EXPECT_EQ(0, d.blocked_get_cookies_count());
1720 EXPECT_EQ(1, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151721 }
1722
1723 context->set_cookie_policy(NULL);
1724}
1725
[email protected]4f79b3f2010-02-05 04:27:471726TEST_F(URLRequestTest, CookiePolicy_ForceSession) {
[email protected]e70c6a82010-07-23 20:38:561727 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(L""));
[email protected]4f79b3f2010-02-05 04:27:471728 ASSERT_TRUE(NULL != server.get());
[email protected]f72a1cc2010-04-30 07:17:301729 scoped_refptr<TestURLRequestContext> context = new TestURLRequestContext();
[email protected]4f79b3f2010-02-05 04:27:471730
1731 TestCookiePolicy cookie_policy(TestCookiePolicy::FORCE_SESSION);
1732 context->set_cookie_policy(&cookie_policy);
1733
1734 // Set up a cookie.
1735 {
1736 TestDelegate d;
1737 URLRequest req(server->TestServerPage(
1738 "set-cookie?A=1;expires=\"Fri, 05 Feb 2010 23:42:01 GMT\""), &d);
1739 req.set_context(context);
1740 req.Start(); // Triggers an asynchronous cookie policy check.
1741
1742 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201743
1744 EXPECT_EQ(0, d.blocked_get_cookies_count());
1745 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]4f79b3f2010-02-05 04:27:471746 }
1747
1748 // Now, check the cookie store.
1749 net::CookieMonster::CookieList cookies =
1750 context->cookie_store()->GetCookieMonster()->GetAllCookies();
1751 EXPECT_EQ(1U, cookies.size());
[email protected]65781e92010-07-21 15:29:571752 EXPECT_FALSE(cookies[0].IsPersistent());
[email protected]4f79b3f2010-02-05 04:27:471753
1754 context->set_cookie_policy(NULL);
1755}
1756
[email protected]71c64f62008-11-15 04:36:511757// In this test, we do a POST which the server will 302 redirect.
1758// The subsequent transaction should use GET, and should not send the
1759// Content-Type header.
1760// http://code.google.com/p/chromium/issues/detail?id=843
[email protected]37314622009-08-17 20:29:391761TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
[email protected]195e77d2009-07-23 19:10:231762 const char kData[] = "hello world";
[email protected]37314622009-08-17 20:29:391763 ASSERT_TRUE(NULL != server_.get());
[email protected]71c64f62008-11-15 04:36:511764 TestDelegate d;
[email protected]37314622009-08-17 20:29:391765 TestURLRequest req(server_->TestServerPage("files/redirect-to-echoall"), &d);
[email protected]71c64f62008-11-15 04:36:511766 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:231767 req.set_upload(CreateSimpleUploadData(kData));
[email protected]71c64f62008-11-15 04:36:511768
1769 // Set headers (some of which are specific to the POST).
[email protected]88e6b6f32010-05-07 23:14:251770 net::HttpRequestHeaders headers;
1771 headers.AddHeadersFromString(
[email protected]dd265012009-01-08 20:45:271772 "Content-Type: multipart/form-data; "
1773 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
1774 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
1775 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
[email protected]71c64f62008-11-15 04:36:511776 "Accept-Language: en-US,en\r\n"
1777 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
[email protected]195e77d2009-07-23 19:10:231778 "Content-Length: 11\r\n"
[email protected]a86c97cc2009-06-24 21:26:271779 "Origin: http://localhost:1337/");
[email protected]88e6b6f32010-05-07 23:14:251780 req.SetExtraRequestHeaders(headers);
[email protected]71c64f62008-11-15 04:36:511781 req.Start();
1782 MessageLoop::current()->Run();
1783
1784 std::string mime_type;
1785 req.GetMimeType(&mime_type);
1786 EXPECT_EQ("text/html", mime_type);
1787
1788 const std::string& data = d.data_received();
1789
1790 // Check that the post-specific headers were stripped:
1791 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
1792 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
1793 EXPECT_FALSE(ContainsString(data, "Origin:"));
1794
1795 // These extra request headers should not have been stripped.
1796 EXPECT_TRUE(ContainsString(data, "Accept:"));
1797 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
1798 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
1799}
1800
[email protected]37314622009-08-17 20:29:391801TEST_F(URLRequestTestHTTP, Post307RedirectPost) {
[email protected]195e77d2009-07-23 19:10:231802 const char kData[] = "hello world";
[email protected]37314622009-08-17 20:29:391803 ASSERT_TRUE(NULL != server_.get());
[email protected]140932f2008-12-12 03:58:061804 TestDelegate d;
[email protected]37314622009-08-17 20:29:391805 TestURLRequest req(server_->TestServerPage("files/redirect307-to-echo"),
[email protected]dd265012009-01-08 20:45:271806 &d);
[email protected]140932f2008-12-12 03:58:061807 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:231808 req.set_upload(CreateSimpleUploadData(kData).get());
[email protected]88e6b6f32010-05-07 23:14:251809 net::HttpRequestHeaders headers;
1810 headers.SetHeader(net::HttpRequestHeaders::kContentLength,
[email protected]528c56d2010-07-30 19:28:441811 base::UintToString(arraysize(kData) - 1));
[email protected]88e6b6f32010-05-07 23:14:251812 req.SetExtraRequestHeaders(headers);
[email protected]140932f2008-12-12 03:58:061813 req.Start();
1814 MessageLoop::current()->Run();
[email protected]195e77d2009-07-23 19:10:231815 EXPECT_EQ("POST", req.method());
1816 EXPECT_EQ(kData, d.data_received());
[email protected]140932f2008-12-12 03:58:061817}
[email protected]dd265012009-01-08 20:45:271818
[email protected]a5c713f2009-04-16 21:05:471819// Custom URLRequestJobs for use with interceptor tests
1820class RestartTestJob : public URLRequestTestJob {
1821 public:
[email protected]a86c97cc2009-06-24 21:26:271822 explicit RestartTestJob(URLRequest* request)
1823 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:471824 protected:
1825 virtual void StartAsync() {
1826 this->NotifyRestartRequired();
1827 }
[email protected]5389bc72009-11-05 23:34:241828 private:
[email protected]13c8a092010-07-29 06:15:441829 ~RestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471830};
1831
1832class CancelTestJob : public URLRequestTestJob {
1833 public:
[email protected]a86c97cc2009-06-24 21:26:271834 explicit CancelTestJob(URLRequest* request)
1835 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:471836 protected:
1837 virtual void StartAsync() {
1838 request_->Cancel();
1839 }
[email protected]5389bc72009-11-05 23:34:241840 private:
1841 ~CancelTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471842};
1843
1844class CancelThenRestartTestJob : public URLRequestTestJob {
1845 public:
[email protected]a86c97cc2009-06-24 21:26:271846 explicit CancelThenRestartTestJob(URLRequest* request)
[email protected]a5c713f2009-04-16 21:05:471847 : URLRequestTestJob(request, true) {
1848 }
1849 protected:
1850 virtual void StartAsync() {
1851 request_->Cancel();
1852 this->NotifyRestartRequired();
1853 }
[email protected]5389bc72009-11-05 23:34:241854 private:
1855 ~CancelThenRestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471856};
1857
1858// An Interceptor for use with interceptor tests
1859class TestInterceptor : URLRequest::Interceptor {
1860 public:
1861 TestInterceptor()
1862 : intercept_main_request_(false), restart_main_request_(false),
1863 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1864 simulate_main_network_error_(false),
1865 intercept_redirect_(false), cancel_redirect_request_(false),
1866 intercept_final_response_(false), cancel_final_request_(false),
1867 did_intercept_main_(false), did_restart_main_(false),
1868 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1869 did_simulate_error_main_(false),
1870 did_intercept_redirect_(false), did_cancel_redirect_(false),
1871 did_intercept_final_(false), did_cancel_final_(false) {
1872 URLRequest::RegisterRequestInterceptor(this);
1873 }
1874
1875 ~TestInterceptor() {
1876 URLRequest::UnregisterRequestInterceptor(this);
1877 }
1878
1879 virtual URLRequestJob* MaybeIntercept(URLRequest* request) {
1880 if (restart_main_request_) {
1881 restart_main_request_ = false;
1882 did_restart_main_ = true;
1883 return new RestartTestJob(request);
1884 }
1885 if (cancel_main_request_) {
1886 cancel_main_request_ = false;
1887 did_cancel_main_ = true;
1888 return new CancelTestJob(request);
1889 }
1890 if (cancel_then_restart_main_request_) {
1891 cancel_then_restart_main_request_ = false;
1892 did_cancel_then_restart_main_ = true;
1893 return new CancelThenRestartTestJob(request);
1894 }
1895 if (simulate_main_network_error_) {
1896 simulate_main_network_error_ = false;
1897 did_simulate_error_main_ = true;
1898 // will error since the requeted url is not one of its canned urls
1899 return new URLRequestTestJob(request, true);
1900 }
1901 if (!intercept_main_request_)
1902 return NULL;
1903 intercept_main_request_ = false;
1904 did_intercept_main_ = true;
1905 return new URLRequestTestJob(request,
1906 main_headers_,
1907 main_data_,
1908 true);
1909 }
1910
1911 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
1912 const GURL& location) {
1913 if (cancel_redirect_request_) {
1914 cancel_redirect_request_ = false;
1915 did_cancel_redirect_ = true;
1916 return new CancelTestJob(request);
1917 }
1918 if (!intercept_redirect_)
1919 return NULL;
1920 intercept_redirect_ = false;
1921 did_intercept_redirect_ = true;
1922 return new URLRequestTestJob(request,
1923 redirect_headers_,
1924 redirect_data_,
1925 true);
1926 }
1927
1928 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) {
1929 if (cancel_final_request_) {
1930 cancel_final_request_ = false;
1931 did_cancel_final_ = true;
1932 return new CancelTestJob(request);
1933 }
1934 if (!intercept_final_response_)
1935 return NULL;
1936 intercept_final_response_ = false;
1937 did_intercept_final_ = true;
1938 return new URLRequestTestJob(request,
1939 final_headers_,
1940 final_data_,
1941 true);
1942 }
1943
1944 // Whether to intercept the main request, and if so the response to return.
1945 bool intercept_main_request_;
1946 std::string main_headers_;
1947 std::string main_data_;
1948
1949 // Other actions we take at MaybeIntercept time
1950 bool restart_main_request_;
1951 bool cancel_main_request_;
1952 bool cancel_then_restart_main_request_;
1953 bool simulate_main_network_error_;
1954
1955 // Whether to intercept redirects, and if so the response to return.
1956 bool intercept_redirect_;
1957 std::string redirect_headers_;
1958 std::string redirect_data_;
1959
1960 // Other actions we can take at MaybeInterceptRedirect time
1961 bool cancel_redirect_request_;
1962
1963 // Whether to intercept final response, and if so the response to return.
1964 bool intercept_final_response_;
1965 std::string final_headers_;
1966 std::string final_data_;
1967
1968 // Other actions we can take at MaybeInterceptResponse time
1969 bool cancel_final_request_;
1970
1971 // If we did something or not
1972 bool did_intercept_main_;
1973 bool did_restart_main_;
1974 bool did_cancel_main_;
1975 bool did_cancel_then_restart_main_;
1976 bool did_simulate_error_main_;
1977 bool did_intercept_redirect_;
1978 bool did_cancel_redirect_;
1979 bool did_intercept_final_;
1980 bool did_cancel_final_;
1981
1982 // Static getters for canned response header and data strings
1983
1984 static std::string ok_data() {
1985 return URLRequestTestJob::test_data_1();
1986 }
1987
1988 static std::string ok_headers() {
1989 return URLRequestTestJob::test_headers();
1990 }
1991
1992 static std::string redirect_data() {
1993 return std::string();
1994 }
1995
1996 static std::string redirect_headers() {
1997 return URLRequestTestJob::test_redirect_headers();
1998 }
1999
2000 static std::string error_data() {
2001 return std::string("ohhh nooooo mr. bill!");
2002 }
2003
2004 static std::string error_headers() {
2005 return URLRequestTestJob::test_error_headers();
2006 }
2007};
2008
2009TEST_F(URLRequestTest, Intercept) {
2010 TestInterceptor interceptor;
2011
2012 // intercept the main request and respond with a simple response
2013 interceptor.intercept_main_request_ = true;
2014 interceptor.main_headers_ = TestInterceptor::ok_headers();
2015 interceptor.main_data_ = TestInterceptor::ok_data();
2016
2017 TestDelegate d;
2018 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2019 URLRequest::UserData* user_data0 = new URLRequest::UserData();
2020 URLRequest::UserData* user_data1 = new URLRequest::UserData();
2021 URLRequest::UserData* user_data2 = new URLRequest::UserData();
2022 req.SetUserData(NULL, user_data0);
2023 req.SetUserData(&user_data1, user_data1);
2024 req.SetUserData(&user_data2, user_data2);
2025 req.set_method("GET");
2026 req.Start();
2027 MessageLoop::current()->Run();
2028
2029 // Make sure we can retrieve our specific user data
2030 EXPECT_EQ(user_data0, req.GetUserData(NULL));
2031 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
2032 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
2033
2034 // Check the interceptor got called as expected
2035 EXPECT_TRUE(interceptor.did_intercept_main_);
2036
2037 // Check we got one good response
2038 EXPECT_TRUE(req.status().is_success());
2039 EXPECT_EQ(200, req.response_headers()->response_code());
2040 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2041 EXPECT_EQ(1, d.response_started_count());
2042 EXPECT_EQ(0, d.received_redirect_count());
2043}
2044
2045TEST_F(URLRequestTest, InterceptRedirect) {
2046 TestInterceptor interceptor;
2047
2048 // intercept the main request and respond with a redirect
2049 interceptor.intercept_main_request_ = true;
2050 interceptor.main_headers_ = TestInterceptor::redirect_headers();
2051 interceptor.main_data_ = TestInterceptor::redirect_data();
2052
2053 // intercept that redirect and respond a final OK response
2054 interceptor.intercept_redirect_ = true;
2055 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
2056 interceptor.redirect_data_ = TestInterceptor::ok_data();
2057
2058 TestDelegate d;
2059 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2060 req.set_method("GET");
2061 req.Start();
2062 MessageLoop::current()->Run();
2063
2064 // Check the interceptor got called as expected
2065 EXPECT_TRUE(interceptor.did_intercept_main_);
2066 EXPECT_TRUE(interceptor.did_intercept_redirect_);
2067
2068 // Check we got one good response
2069 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:112070 if (req.status().is_success()) {
2071 EXPECT_EQ(200, req.response_headers()->response_code());
2072 }
[email protected]a5c713f2009-04-16 21:05:472073 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2074 EXPECT_EQ(1, d.response_started_count());
2075 EXPECT_EQ(0, d.received_redirect_count());
2076}
2077
2078TEST_F(URLRequestTest, InterceptServerError) {
2079 TestInterceptor interceptor;
2080
2081 // intercept the main request to generate a server error response
2082 interceptor.intercept_main_request_ = true;
2083 interceptor.main_headers_ = TestInterceptor::error_headers();
2084 interceptor.main_data_ = TestInterceptor::error_data();
2085
2086 // intercept that error and respond with an OK response
2087 interceptor.intercept_final_response_ = true;
2088 interceptor.final_headers_ = TestInterceptor::ok_headers();
2089 interceptor.final_data_ = TestInterceptor::ok_data();
2090
2091 TestDelegate d;
2092 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2093 req.set_method("GET");
2094 req.Start();
2095 MessageLoop::current()->Run();
2096
2097 // Check the interceptor got called as expected
2098 EXPECT_TRUE(interceptor.did_intercept_main_);
2099 EXPECT_TRUE(interceptor.did_intercept_final_);
2100
2101 // Check we got one good response
2102 EXPECT_TRUE(req.status().is_success());
2103 EXPECT_EQ(200, req.response_headers()->response_code());
2104 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2105 EXPECT_EQ(1, d.response_started_count());
2106 EXPECT_EQ(0, d.received_redirect_count());
2107}
2108
2109TEST_F(URLRequestTest, InterceptNetworkError) {
2110 TestInterceptor interceptor;
2111
2112 // intercept the main request to simulate a network error
2113 interceptor.simulate_main_network_error_ = true;
2114
2115 // intercept that error and respond with an OK response
2116 interceptor.intercept_final_response_ = true;
2117 interceptor.final_headers_ = TestInterceptor::ok_headers();
2118 interceptor.final_data_ = TestInterceptor::ok_data();
2119
2120 TestDelegate d;
2121 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2122 req.set_method("GET");
2123 req.Start();
2124 MessageLoop::current()->Run();
2125
2126 // Check the interceptor got called as expected
2127 EXPECT_TRUE(interceptor.did_simulate_error_main_);
2128 EXPECT_TRUE(interceptor.did_intercept_final_);
2129
2130 // Check we received one good response
2131 EXPECT_TRUE(req.status().is_success());
2132 EXPECT_EQ(200, req.response_headers()->response_code());
2133 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2134 EXPECT_EQ(1, d.response_started_count());
2135 EXPECT_EQ(0, d.received_redirect_count());
2136}
2137
2138TEST_F(URLRequestTest, InterceptRestartRequired) {
2139 TestInterceptor interceptor;
2140
2141 // restart the main request
2142 interceptor.restart_main_request_ = true;
2143
2144 // then intercept the new main request and respond with an OK response
2145 interceptor.intercept_main_request_ = true;
2146 interceptor.main_headers_ = TestInterceptor::ok_headers();
2147 interceptor.main_data_ = TestInterceptor::ok_data();
2148
2149 TestDelegate d;
2150 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2151 req.set_method("GET");
2152 req.Start();
2153 MessageLoop::current()->Run();
2154
2155 // Check the interceptor got called as expected
2156 EXPECT_TRUE(interceptor.did_restart_main_);
2157 EXPECT_TRUE(interceptor.did_intercept_main_);
2158
2159 // Check we received one good response
2160 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:112161 if (req.status().is_success()) {
2162 EXPECT_EQ(200, req.response_headers()->response_code());
2163 }
[email protected]a5c713f2009-04-16 21:05:472164 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2165 EXPECT_EQ(1, d.response_started_count());
2166 EXPECT_EQ(0, d.received_redirect_count());
2167}
2168
2169TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
2170 TestInterceptor interceptor;
2171
2172 // intercept the main request and cancel from within the restarted job
2173 interceptor.cancel_main_request_ = true;
2174
2175 // setup to intercept final response and override it with an OK response
2176 interceptor.intercept_final_response_ = true;
2177 interceptor.final_headers_ = TestInterceptor::ok_headers();
2178 interceptor.final_data_ = TestInterceptor::ok_data();
2179
2180 TestDelegate d;
2181 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2182 req.set_method("GET");
2183 req.Start();
2184 MessageLoop::current()->Run();
2185
2186 // Check the interceptor got called as expected
2187 EXPECT_TRUE(interceptor.did_cancel_main_);
2188 EXPECT_FALSE(interceptor.did_intercept_final_);
2189
2190 // Check we see a canceled request
2191 EXPECT_FALSE(req.status().is_success());
2192 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
2193}
2194
2195TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
2196 TestInterceptor interceptor;
2197
2198 // intercept the main request and respond with a redirect
2199 interceptor.intercept_main_request_ = true;
2200 interceptor.main_headers_ = TestInterceptor::redirect_headers();
2201 interceptor.main_data_ = TestInterceptor::redirect_data();
2202
2203 // intercept the redirect and cancel from within that job
2204 interceptor.cancel_redirect_request_ = true;
2205
2206 // setup to intercept final response and override it with an OK response
2207 interceptor.intercept_final_response_ = true;
2208 interceptor.final_headers_ = TestInterceptor::ok_headers();
2209 interceptor.final_data_ = TestInterceptor::ok_data();
2210
2211 TestDelegate d;
2212 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2213 req.set_method("GET");
2214 req.Start();
2215 MessageLoop::current()->Run();
2216
2217 // Check the interceptor got called as expected
2218 EXPECT_TRUE(interceptor.did_intercept_main_);
2219 EXPECT_TRUE(interceptor.did_cancel_redirect_);
2220 EXPECT_FALSE(interceptor.did_intercept_final_);
2221
2222 // Check we see a canceled request
2223 EXPECT_FALSE(req.status().is_success());
2224 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
2225}
2226
2227TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
2228 TestInterceptor interceptor;
2229
2230 // intercept the main request to simulate a network error
2231 interceptor.simulate_main_network_error_ = true;
2232
2233 // setup to intercept final response and cancel from within that job
2234 interceptor.cancel_final_request_ = true;
2235
2236 TestDelegate d;
2237 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2238 req.set_method("GET");
2239 req.Start();
2240 MessageLoop::current()->Run();
2241
2242 // Check the interceptor got called as expected
2243 EXPECT_TRUE(interceptor.did_simulate_error_main_);
2244 EXPECT_TRUE(interceptor.did_cancel_final_);
2245
2246 // Check we see a canceled request
2247 EXPECT_FALSE(req.status().is_success());
2248 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
2249}
2250
2251TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
2252 TestInterceptor interceptor;
2253
2254 // intercept the main request and cancel then restart from within that job
2255 interceptor.cancel_then_restart_main_request_ = true;
2256
2257 // setup to intercept final response and override it with an OK response
2258 interceptor.intercept_final_response_ = true;
2259 interceptor.final_headers_ = TestInterceptor::ok_headers();
2260 interceptor.final_data_ = TestInterceptor::ok_data();
2261
2262 TestDelegate d;
2263 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2264 req.set_method("GET");
2265 req.Start();
2266 MessageLoop::current()->Run();
2267
2268 // Check the interceptor got called as expected
2269 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
2270 EXPECT_FALSE(interceptor.did_intercept_final_);
2271
2272 // Check we see a canceled request
2273 EXPECT_FALSE(req.status().is_success());
2274 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
2275}
2276
[email protected]b89ca032009-08-31 21:41:312277class URLRequestTestFTP : public URLRequestTest {
2278 protected:
2279 static void SetUpTestCase() {
2280 server_ = FTPTestServer::CreateServer(L"");
2281 }
2282
2283 static void TearDownTestCase() {
2284 server_ = NULL;
2285 }
2286
2287 static scoped_refptr<FTPTestServer> server_;
2288};
2289
2290// static
2291scoped_refptr<FTPTestServer> URLRequestTestFTP::server_;
2292
[email protected]5accf7332009-11-24 03:41:382293// Flaky, see http://crbug.com/25045.
2294TEST_F(URLRequestTestFTP, FLAKY_FTPDirectoryListing) {
[email protected]a25e90e2009-09-09 17:05:372295 ASSERT_TRUE(NULL != server_.get());
2296 TestDelegate d;
2297 {
2298 TestURLRequest r(server_->TestServerPage("/"), &d);
2299 r.Start();
2300 EXPECT_TRUE(r.is_pending());
2301
2302 MessageLoop::current()->Run();
2303
2304 EXPECT_FALSE(r.is_pending());
2305 EXPECT_EQ(1, d.response_started_count());
2306 EXPECT_FALSE(d.received_data_before_response());
2307 EXPECT_LT(0, d.bytes_received());
2308 }
2309}
2310
[email protected]7df70012010-02-04 00:09:552311// Flaky, see http://crbug.com/25045.
2312TEST_F(URLRequestTestFTP, FLAKY_FTPGetTestAnonymous) {
[email protected]b89ca032009-08-31 21:41:312313 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:022314 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272315 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022316 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272317 TestDelegate d;
2318 {
[email protected]b89ca032009-08-31 21:41:312319 TestURLRequest r(server_->TestServerPage("/LICENSE"), &d);
[email protected]dd265012009-01-08 20:45:272320 r.Start();
2321 EXPECT_TRUE(r.is_pending());
2322
2323 MessageLoop::current()->Run();
2324
2325 int64 file_size = 0;
2326 file_util::GetFileSize(app_path, &file_size);
2327
[email protected]ba2f3342009-07-30 18:08:422328 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272329 EXPECT_EQ(1, d.response_started_count());
2330 EXPECT_FALSE(d.received_data_before_response());
2331 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2332 }
2333}
2334
[email protected]e9ecbd12009-12-20 18:44:402335// Flaky, see http://crbug.com/25045.
2336TEST_F(URLRequestTestFTP, FLAKY_FTPGetTest) {
[email protected]b89ca032009-08-31 21:41:312337 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:022338 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272339 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022340 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272341 TestDelegate d;
2342 {
[email protected]b89ca032009-08-31 21:41:312343 TestURLRequest r(server_->TestServerPage("/LICENSE", "chrome", "chrome"),
2344 &d);
[email protected]dd265012009-01-08 20:45:272345 r.Start();
2346 EXPECT_TRUE(r.is_pending());
2347
2348 MessageLoop::current()->Run();
2349
2350 int64 file_size = 0;
2351 file_util::GetFileSize(app_path, &file_size);
2352
[email protected]ba2f3342009-07-30 18:08:422353 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272354 EXPECT_EQ(1, d.response_started_count());
2355 EXPECT_FALSE(d.received_data_before_response());
2356 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2357 }
2358}
2359
[email protected]49abd652010-08-05 05:04:532360// Flaky, see http://crbug.com/25045.
2361TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPassword) {
[email protected]b89ca032009-08-31 21:41:312362 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:022363 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272364 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022365 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272366 TestDelegate d;
2367 {
[email protected]b89ca032009-08-31 21:41:312368 TestURLRequest r(server_->TestServerPage("/LICENSE",
2369 "chrome", "wrong_password"), &d);
[email protected]dd265012009-01-08 20:45:272370 r.Start();
2371 EXPECT_TRUE(r.is_pending());
2372
2373 MessageLoop::current()->Run();
2374
2375 int64 file_size = 0;
2376 file_util::GetFileSize(app_path, &file_size);
2377
[email protected]ba2f3342009-07-30 18:08:422378 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272379 EXPECT_EQ(1, d.response_started_count());
2380 EXPECT_FALSE(d.received_data_before_response());
2381 EXPECT_EQ(d.bytes_received(), 0);
2382 }
2383}
2384
[email protected]cde4e80d2009-10-16 19:58:152385// Flaky, see http://crbug.com/25045.
2386TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPasswordRestart) {
[email protected]b89ca032009-08-31 21:41:312387 ASSERT_TRUE(NULL != server_.get());
[email protected]8b8a197d2009-08-26 15:57:582388 FilePath app_path;
2389 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2390 app_path = app_path.AppendASCII("LICENSE");
2391 TestDelegate d;
2392 // Set correct login credentials. The delegate will be asked for them when
2393 // the initial login with wrong credentials will fail.
[email protected]13c8a092010-07-29 06:15:442394 d.set_username(kChrome);
2395 d.set_password(kChrome);
[email protected]8b8a197d2009-08-26 15:57:582396 {
[email protected]b89ca032009-08-31 21:41:312397 TestURLRequest r(server_->TestServerPage("/LICENSE",
2398 "chrome", "wrong_password"), &d);
[email protected]8b8a197d2009-08-26 15:57:582399 r.Start();
2400 EXPECT_TRUE(r.is_pending());
2401
2402 MessageLoop::current()->Run();
2403
2404 int64 file_size = 0;
2405 file_util::GetFileSize(app_path, &file_size);
2406
2407 EXPECT_FALSE(r.is_pending());
2408 EXPECT_EQ(1, d.response_started_count());
2409 EXPECT_FALSE(d.received_data_before_response());
2410 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2411 }
2412}
2413
[email protected]49abd652010-08-05 05:04:532414// Flaky, see http://crbug.com/25045.
2415TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUser) {
[email protected]b89ca032009-08-31 21:41:312416 ASSERT_TRUE(NULL != server_.get());
[email protected]399b8702009-05-01 20:34:022417 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272418 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022419 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272420 TestDelegate d;
2421 {
[email protected]b89ca032009-08-31 21:41:312422 TestURLRequest r(server_->TestServerPage("/LICENSE",
2423 "wrong_user", "chrome"), &d);
[email protected]dd265012009-01-08 20:45:272424 r.Start();
2425 EXPECT_TRUE(r.is_pending());
2426
2427 MessageLoop::current()->Run();
2428
2429 int64 file_size = 0;
2430 file_util::GetFileSize(app_path, &file_size);
2431
[email protected]ba2f3342009-07-30 18:08:422432 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272433 EXPECT_EQ(1, d.response_started_count());
2434 EXPECT_FALSE(d.received_data_before_response());
2435 EXPECT_EQ(d.bytes_received(), 0);
2436 }
2437}
[email protected]8b8a197d2009-08-26 15:57:582438
[email protected]cde4e80d2009-10-16 19:58:152439// Flaky, see http://crbug.com/25045.
2440TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUserRestart) {
[email protected]b89ca032009-08-31 21:41:312441 ASSERT_TRUE(NULL != server_.get());
[email protected]8b8a197d2009-08-26 15:57:582442 FilePath app_path;
2443 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2444 app_path = app_path.AppendASCII("LICENSE");
2445 TestDelegate d;
2446 // Set correct login credentials. The delegate will be asked for them when
2447 // the initial login with wrong credentials will fail.
[email protected]13c8a092010-07-29 06:15:442448 d.set_username(kChrome);
2449 d.set_password(kChrome);
[email protected]8b8a197d2009-08-26 15:57:582450 {
[email protected]b89ca032009-08-31 21:41:312451 TestURLRequest r(server_->TestServerPage("/LICENSE",
2452 "wrong_user", "chrome"), &d);
[email protected]8b8a197d2009-08-26 15:57:582453 r.Start();
2454 EXPECT_TRUE(r.is_pending());
2455
2456 MessageLoop::current()->Run();
2457
2458 int64 file_size = 0;
2459 file_util::GetFileSize(app_path, &file_size);
2460
2461 EXPECT_FALSE(r.is_pending());
2462 EXPECT_EQ(1, d.response_started_count());
2463 EXPECT_FALSE(d.received_data_before_response());
2464 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2465 }
2466}
[email protected]60a3df52009-09-22 16:13:242467
[email protected]cde4e80d2009-10-16 19:58:152468// Flaky, see http://crbug.com/25045.
2469TEST_F(URLRequestTestFTP, FLAKY_FTPCacheURLCredentials) {
[email protected]60a3df52009-09-22 16:13:242470 ASSERT_TRUE(NULL != server_.get());
2471 FilePath app_path;
2472 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2473 app_path = app_path.AppendASCII("LICENSE");
2474
2475 scoped_ptr<TestDelegate> d(new TestDelegate);
2476 {
2477 // Pass correct login identity in the URL.
2478 TestURLRequest r(server_->TestServerPage("/LICENSE",
2479 "chrome", "chrome"),
2480 d.get());
2481 r.Start();
2482 EXPECT_TRUE(r.is_pending());
2483
2484 MessageLoop::current()->Run();
2485
2486 int64 file_size = 0;
2487 file_util::GetFileSize(app_path, &file_size);
2488
2489 EXPECT_FALSE(r.is_pending());
2490 EXPECT_EQ(1, d->response_started_count());
2491 EXPECT_FALSE(d->received_data_before_response());
2492 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2493 }
2494
2495 d.reset(new TestDelegate);
2496 {
2497 // This request should use cached identity from previous request.
2498 TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get());
2499 r.Start();
2500 EXPECT_TRUE(r.is_pending());
2501
2502 MessageLoop::current()->Run();
2503
2504 int64 file_size = 0;
2505 file_util::GetFileSize(app_path, &file_size);
2506
2507 EXPECT_FALSE(r.is_pending());
2508 EXPECT_EQ(1, d->response_started_count());
2509 EXPECT_FALSE(d->received_data_before_response());
2510 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2511 }
2512}
2513
[email protected]cde4e80d2009-10-16 19:58:152514// Flaky, see http://crbug.com/25045.
2515TEST_F(URLRequestTestFTP, FLAKY_FTPCacheLoginBoxCredentials) {
[email protected]60a3df52009-09-22 16:13:242516 ASSERT_TRUE(NULL != server_.get());
2517 FilePath app_path;
2518 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2519 app_path = app_path.AppendASCII("LICENSE");
2520
2521 scoped_ptr<TestDelegate> d(new TestDelegate);
2522 // Set correct login credentials. The delegate will be asked for them when
2523 // the initial login with wrong credentials will fail.
[email protected]13c8a092010-07-29 06:15:442524 d->set_username(kChrome);
2525 d->set_password(kChrome);
[email protected]60a3df52009-09-22 16:13:242526 {
2527 TestURLRequest r(server_->TestServerPage("/LICENSE",
2528 "chrome", "wrong_password"),
2529 d.get());
2530 r.Start();
2531 EXPECT_TRUE(r.is_pending());
2532
2533 MessageLoop::current()->Run();
2534
2535 int64 file_size = 0;
2536 file_util::GetFileSize(app_path, &file_size);
2537
2538 EXPECT_FALSE(r.is_pending());
2539 EXPECT_EQ(1, d->response_started_count());
2540 EXPECT_FALSE(d->received_data_before_response());
2541 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2542 }
2543
2544 // Use a new delegate without explicit credentials. The cached ones should be
2545 // used.
2546 d.reset(new TestDelegate);
2547 {
2548 // Don't pass wrong credentials in the URL, they would override valid cached
2549 // ones.
2550 TestURLRequest r(server_->TestServerPage("/LICENSE"), d.get());
2551 r.Start();
2552 EXPECT_TRUE(r.is_pending());
2553
2554 MessageLoop::current()->Run();
2555
2556 int64 file_size = 0;
2557 file_util::GetFileSize(app_path, &file_size);
2558
2559 EXPECT_FALSE(r.is_pending());
2560 EXPECT_EQ(1, d->response_started_count());
2561 EXPECT_FALSE(d->received_data_before_response());
2562 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2563 }
2564}
[email protected]09a362d32009-09-24 18:01:332565
2566// Check that default A-L header is sent.
2567TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
2568 ASSERT_TRUE(NULL != server_.get());
2569 TestDelegate d;
2570 TestURLRequest req(server_->TestServerPage("echoheader?Accept-Language"), &d);
[email protected]f72a1cc2010-04-30 07:17:302571 req.set_context(new TestURLRequestContext());
[email protected]09a362d32009-09-24 18:01:332572 req.Start();
2573 MessageLoop::current()->Run();
2574 EXPECT_EQ(req.context()->accept_language(), d.data_received());
2575}
2576
2577// Check that if request overrides the A-L header, the default is not appended.
2578// See http://crbug.com/20894
2579TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
2580 ASSERT_TRUE(NULL != server_.get());
2581 TestDelegate d;
[email protected]eb834fb2009-10-23 16:09:412582 TestURLRequest
2583 req(server_->TestServerPage("echoheaderoverride?Accept-Language"), &d);
[email protected]f72a1cc2010-04-30 07:17:302584 req.set_context(new TestURLRequestContext());
[email protected]88e6b6f32010-05-07 23:14:252585 net::HttpRequestHeaders headers;
2586 headers.SetHeader(net::HttpRequestHeaders::kAcceptLanguage, "ru");
2587 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:332588 req.Start();
2589 MessageLoop::current()->Run();
2590 EXPECT_EQ(std::string("ru"), d.data_received());
2591}
2592
2593// Check that default A-C header is sent.
2594TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) {
2595 ASSERT_TRUE(NULL != server_.get());
2596 TestDelegate d;
2597 TestURLRequest req(server_->TestServerPage("echoheader?Accept-Charset"), &d);
[email protected]f72a1cc2010-04-30 07:17:302598 req.set_context(new TestURLRequestContext());
[email protected]09a362d32009-09-24 18:01:332599 req.Start();
2600 MessageLoop::current()->Run();
2601 EXPECT_EQ(req.context()->accept_charset(), d.data_received());
2602}
2603
2604// Check that if request overrides the A-C header, the default is not appended.
2605// See http://crbug.com/20894
2606TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) {
2607 ASSERT_TRUE(NULL != server_.get());
2608 TestDelegate d;
[email protected]eb834fb2009-10-23 16:09:412609 TestURLRequest
2610 req(server_->TestServerPage("echoheaderoverride?Accept-Charset"), &d);
[email protected]f72a1cc2010-04-30 07:17:302611 req.set_context(new TestURLRequestContext());
[email protected]88e6b6f32010-05-07 23:14:252612 net::HttpRequestHeaders headers;
2613 headers.SetHeader(net::HttpRequestHeaders::kAcceptCharset, "koi-8r");
2614 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:332615 req.Start();
2616 MessageLoop::current()->Run();
2617 EXPECT_EQ(std::string("koi-8r"), d.data_received());
2618}