blob: 82efe662e52dc9a36e45fd6554b34f2791160465 [file] [log] [blame]
[email protected]cbe04ef2011-01-11 00:13:241// Copyright (c) 2011 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 <shlobj.h>
[email protected]d8eb84242010-09-25 02:25:0611#include <windows.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]528c56d2010-07-30 19:28:4424#include "base/string_number_conversions.h"
[email protected]d8eb84242010-09-25 02:25:0625#include "base/string_piece.h"
26#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2227#include "base/utf_string_conversions.h"
[email protected]0757e7702009-03-27 04:00:2228#include "net/base/cookie_monster.h"
[email protected]34602282010-02-03 22:14:1529#include "net/base/cookie_policy.h"
initial.commit586acc5fe2008-07-26 22:42:5230#include "net/base/load_flags.h"
[email protected]d8eb84242010-09-25 02:25:0631#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5332#include "net/base/net_log.h"
33#include "net/base/net_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/base/net_module.h"
35#include "net/base/net_util.h"
[email protected]96adadb2010-08-28 01:16:1736#include "net/base/ssl_connection_status_flags.h"
[email protected]195e77d2009-07-23 19:10:2337#include "net/base/upload_data.h"
initial.commit586acc5fe2008-07-26 22:42:5238#include "net/disk_cache/disk_cache.h"
[email protected]ba2f3342009-07-30 18:08:4239#include "net/ftp/ftp_network_layer.h"
initial.commit586acc5fe2008-07-26 22:42:5240#include "net/http/http_cache.h"
41#include "net/http/http_network_layer.h"
[email protected]88e6b6f32010-05-07 23:14:2542#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2143#include "net/http/http_response_headers.h"
[email protected]63de95b2008-12-10 04:11:2744#include "net/proxy/proxy_service.h"
[email protected]1b9565c2010-07-21 01:19:3145#include "net/test/test_server.h"
initial.commit586acc5fe2008-07-26 22:42:5246#include "net/url_request/url_request.h"
[email protected]7886a8c2009-08-21 04:11:0947#include "net/url_request/url_request_file_dir_job.h"
[email protected]bcb84f8b2009-08-31 16:20:1448#include "net/url_request/url_request_http_job.h"
[email protected]a5c713f2009-04-16 21:05:4749#include "net/url_request/url_request_test_job.h"
initial.commit586acc5fe2008-07-26 22:42:5250#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1551#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5252
[email protected]e1acf6f2008-10-27 20:43:3353using base::Time;
54
initial.commit586acc5fe2008-07-26 22:42:5255namespace {
56
[email protected]13c8a092010-07-29 06:15:4457const string16 kChrome(ASCIIToUTF16("chrome"));
58const string16 kSecret(ASCIIToUTF16("secret"));
59const string16 kUser(ASCIIToUTF16("user"));
60
[email protected]8a16266e2009-09-10 21:08:3961base::StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:5262 return "header";
63}
64
[email protected]71c64f62008-11-15 04:36:5165// Do a case-insensitive search through |haystack| for |needle|.
66bool ContainsString(const std::string& haystack, const char* needle) {
67 std::string::const_iterator it =
68 std::search(haystack.begin(),
69 haystack.end(),
70 needle,
71 needle + strlen(needle),
[email protected]07f1cee2010-11-03 03:53:3572 base::CaseInsensitiveCompare<char>());
[email protected]71c64f62008-11-15 04:36:5173 return it != haystack.end();
74}
75
[email protected]661376a2009-04-29 02:04:2376void FillBuffer(char* buffer, size_t len) {
77 static bool called = false;
78 if (!called) {
79 called = true;
80 int seed = static_cast<int>(Time::Now().ToInternalValue());
81 srand(seed);
82 }
83
84 for (size_t i = 0; i < len; i++) {
85 buffer[i] = static_cast<char>(rand());
86 if (!buffer[i])
87 buffer[i] = 'g';
88 }
89}
90
[email protected]195e77d2009-07-23 19:10:2391scoped_refptr<net::UploadData> CreateSimpleUploadData(const char* data) {
[email protected]ad8e04a2010-11-01 04:16:2792 scoped_refptr<net::UploadData> upload(new net::UploadData);
[email protected]195e77d2009-07-23 19:10:2393 upload->AppendBytes(data, strlen(data));
94 return upload;
95}
96
[email protected]96adadb2010-08-28 01:16:1797// Verify that the SSLInfo of a successful SSL connection has valid values.
98void CheckSSLInfo(const net::SSLInfo& ssl_info) {
[email protected]89af3d92010-10-15 20:31:3799 // Allow ChromeFrame fake SSLInfo to get through.
[email protected]c679be22010-08-29 15:20:05100 if (ssl_info.cert.get() &&
[email protected]89af3d92010-10-15 20:31:37101 ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") {
102 // -1 means unknown.
103 EXPECT_EQ(ssl_info.security_bits, -1);
[email protected]c679be22010-08-29 15:20:05104 return;
[email protected]89af3d92010-10-15 20:31:37105 }
106
[email protected]96adadb2010-08-28 01:16:17107 // -1 means unknown. 0 means no encryption.
108 EXPECT_GT(ssl_info.security_bits, 0);
109
110 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
111 int cipher_suite = net::SSLConnectionStatusToCipherSuite(
112 ssl_info.connection_status);
113 EXPECT_NE(0, cipher_suite);
114}
115
[email protected]9396b252008-09-29 17:29:38116} // namespace
initial.commit586acc5fe2008-07-26 22:42:52117
[email protected]7a0bb4bf2008-11-19 21:41:48118// Inherit PlatformTest since we require the autorelease pool on Mac OS X.f
119class URLRequestTest : public PlatformTest {
[email protected]abb26092010-11-11 22:19:00120 public:
121 static void SetUpTestCase() {
[email protected]6981d9632010-11-30 21:34:02122 net::URLRequest::AllowFileAccess();
[email protected]abb26092010-11-11 22:19:00123 }
[email protected]7a0bb4bf2008-11-19 21:41:48124};
125
[email protected]b89290212009-08-14 22:37:35126class URLRequestTestHTTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:11127 public:
128 URLRequestTestHTTP()
129 : test_server_(net::TestServer::TYPE_HTTP,
130 FilePath(FILE_PATH_LITERAL(
131 "net/data/url_request_unittest"))) {
132 }
133
[email protected]b89290212009-08-14 22:37:35134 protected:
[email protected]762d2db2010-01-11 19:03:01135 void HTTPUploadDataOperationTest(const std::string& method) {
[email protected]762d2db2010-01-11 19:03:01136 const int kMsgSize = 20000; // multiple of 10
137 const int kIterations = 50;
138 char *uploadBytes = new char[kMsgSize+1];
139 char *ptr = uploadBytes;
140 char marker = 'a';
141 for (int idx = 0; idx < kMsgSize/10; idx++) {
142 memcpy(ptr, "----------", 10);
143 ptr += 10;
144 if (idx % 100 == 0) {
145 ptr--;
146 *ptr++ = marker;
147 if (++marker > 'z')
148 marker = 'a';
149 }
150 }
151 uploadBytes[kMsgSize] = '\0';
152
[email protected]aeb53f02011-01-15 00:21:34153 scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext());
[email protected]762d2db2010-01-11 19:03:01154
155 for (int i = 0; i < kIterations; ++i) {
156 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:02157 net::URLRequest r(test_server_.GetURL("echo"), &d);
[email protected]762d2db2010-01-11 19:03:01158 r.set_context(context);
159 r.set_method(method.c_str());
160
161 r.AppendBytesToUpload(uploadBytes, kMsgSize);
162
163 r.Start();
164 EXPECT_TRUE(r.is_pending());
165
166 MessageLoop::current()->Run();
167
168 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
169 (int) r.status().status() << ", os error: " << r.status().os_error();
170
171 EXPECT_FALSE(d.received_data_before_response());
172 EXPECT_EQ(uploadBytes, d.data_received());
173 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0);
174 EXPECT_EQ(d.data_received().compare(uploadBytes), 0);
175 }
176 delete[] uploadBytes;
177 }
178
[email protected]95409e12010-08-17 20:07:11179 net::TestServer test_server_;
[email protected]b89290212009-08-14 22:37:35180};
181
[email protected]95409e12010-08-17 20:07:11182// In this unit test, we're using the HTTPTestServer as a proxy server and
183// issuing a CONNECT request with the magic host name "www.redirect.com".
184// The HTTPTestServer will return a 302 response, which we should not
185// follow.
[email protected]b89290212009-08-14 22:37:35186TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]95409e12010-08-17 20:07:11187 ASSERT_TRUE(test_server_.Start());
188
[email protected]d1ec59082009-02-11 02:48:15189 TestDelegate d;
190 {
[email protected]6981d9632010-11-30 21:34:02191 net::URLRequest r(GURL("https://www.redirect.com/"), &d);
[email protected]95409e12010-08-17 20:07:11192 r.set_context(
193 new TestURLRequestContext(test_server_.host_port_pair().ToString()));
[email protected]d1ec59082009-02-11 02:48:15194
195 r.Start();
196 EXPECT_TRUE(r.is_pending());
197
198 MessageLoop::current()->Run();
199
[email protected]f90bf0d92011-01-13 02:12:44200 EXPECT_EQ(net::URLRequestStatus::FAILED, r.status().status());
[email protected]c744cf22009-02-27 07:28:08201 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08202 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:15203 // We should not have followed the redirect.
204 EXPECT_EQ(0, d.received_redirect_count());
205 }
206}
207
[email protected]95409e12010-08-17 20:07:11208// In this unit test, we're using the HTTPTestServer as a proxy server and
209// issuing a CONNECT request with the magic host name "www.server-auth.com".
210// The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:35211TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]95409e12010-08-17 20:07:11212 ASSERT_TRUE(test_server_.Start());
213
[email protected]dc651782009-02-14 01:45:08214 TestDelegate d;
215 {
[email protected]6981d9632010-11-30 21:34:02216 net::URLRequest r(GURL("https://www.server-auth.com/"), &d);
[email protected]95409e12010-08-17 20:07:11217 r.set_context(
218 new TestURLRequestContext(test_server_.host_port_pair().ToString()));
[email protected]dc651782009-02-14 01:45:08219
220 r.Start();
221 EXPECT_TRUE(r.is_pending());
222
223 MessageLoop::current()->Run();
224
[email protected]f90bf0d92011-01-13 02:12:44225 EXPECT_EQ(net::URLRequestStatus::FAILED, r.status().status());
[email protected]c744cf22009-02-27 07:28:08226 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08227 }
228}
229
[email protected]b89290212009-08-14 22:37:35230TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
[email protected]95409e12010-08-17 20:07:11231 ASSERT_TRUE(test_server_.Start());
232
initial.commit586acc5fe2008-07-26 22:42:52233 TestDelegate d;
234 {
[email protected]95409e12010-08-17 20:07:11235 TestURLRequest r(test_server_.GetURL(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52236
237 r.Start();
238 EXPECT_TRUE(r.is_pending());
239
240 MessageLoop::current()->Run();
241
242 EXPECT_EQ(1, d.response_started_count());
243 EXPECT_FALSE(d.received_data_before_response());
244 EXPECT_NE(0, d.bytes_received());
[email protected]c31a54592009-09-04 02:36:16245
[email protected]9e743cd2010-03-16 07:03:53246 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:52247 }
initial.commit586acc5fe2008-07-26 22:42:52248}
249
[email protected]b89290212009-08-14 22:37:35250TEST_F(URLRequestTestHTTP, GetTest) {
[email protected]95409e12010-08-17 20:07:11251 ASSERT_TRUE(test_server_.Start());
252
initial.commit586acc5fe2008-07-26 22:42:52253 TestDelegate d;
254 {
[email protected]95409e12010-08-17 20:07:11255 TestURLRequest r(test_server_.GetURL(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52256
257 r.Start();
258 EXPECT_TRUE(r.is_pending());
259
260 MessageLoop::current()->Run();
261
262 EXPECT_EQ(1, d.response_started_count());
263 EXPECT_FALSE(d.received_data_before_response());
264 EXPECT_NE(0, d.bytes_received());
265 }
[email protected]5d7b373e2009-09-02 07:19:03266}
267
[email protected]7844480a2009-12-16 21:18:58268TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
[email protected]95409e12010-08-17 20:07:11269 ASSERT_TRUE(test_server_.Start());
270
271 net::TestServer https_test_server(
272 net::TestServer::TYPE_HTTPS, FilePath(FILE_PATH_LITERAL("net/data/ssl")));
273 ASSERT_TRUE(https_test_server.Start());
[email protected]7844480a2009-12-16 21:18:58274
275 // An https server is sent a request with an https referer,
276 // and responds with a redirect to an http url. The http
277 // server should not be sent the referer.
[email protected]95409e12010-08-17 20:07:11278 GURL http_destination = test_server_.GetURL("");
[email protected]7844480a2009-12-16 21:18:58279 TestDelegate d;
[email protected]95409e12010-08-17 20:07:11280 TestURLRequest req(https_test_server.GetURL(
[email protected]7844480a2009-12-16 21:18:58281 "server-redirect?" + http_destination.spec()), &d);
282 req.set_referrer("https://www.referrer.com/");
283 req.Start();
284 MessageLoop::current()->Run();
285
286 EXPECT_EQ(1, d.response_started_count());
287 EXPECT_EQ(1, d.received_redirect_count());
288 EXPECT_EQ(http_destination, req.url());
289 EXPECT_EQ(std::string(), req.referrer());
290}
291
[email protected]73e0bba2009-02-19 22:57:09292class HTTPSRequestTest : public testing::Test {
[email protected]ea224582008-12-07 20:25:46293};
294
[email protected]5774ada2010-07-15 06:30:54295TEST_F(HTTPSRequestTest, HTTPSGetTest) {
[email protected]95409e12010-08-17 20:07:11296 net::TestServer test_server(net::TestServer::TYPE_HTTPS,
297 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
298 ASSERT_TRUE(test_server.Start());
[email protected]ea224582008-12-07 20:25:46299
[email protected]ea224582008-12-07 20:25:46300 TestDelegate d;
301 {
[email protected]95409e12010-08-17 20:07:11302 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]ea224582008-12-07 20:25:46303
304 r.Start();
305 EXPECT_TRUE(r.is_pending());
306
307 MessageLoop::current()->Run();
308
309 EXPECT_EQ(1, d.response_started_count());
310 EXPECT_FALSE(d.received_data_before_response());
311 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17312 CheckSSLInfo(r.ssl_info());
[email protected]ea224582008-12-07 20:25:46313 }
[email protected]ea224582008-12-07 20:25:46314}
315
[email protected]5774ada2010-07-15 06:30:54316TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]347599952010-10-28 11:57:36317 net::TestServer::HTTPSOptions https_options(
318 net::TestServer::HTTPSOptions::CERT_MISMATCHED_NAME);
319 net::TestServer test_server(https_options,
[email protected]95409e12010-08-17 20:07:11320 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
321 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:33322
323 bool err_allowed = true;
324 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
325 TestDelegate d;
326 {
327 d.set_allow_certificate_errors(err_allowed);
[email protected]95409e12010-08-17 20:07:11328 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]bacff652009-03-31 17:50:33329
330 r.Start();
331 EXPECT_TRUE(r.is_pending());
332
333 MessageLoop::current()->Run();
334
335 EXPECT_EQ(1, d.response_started_count());
336 EXPECT_FALSE(d.received_data_before_response());
337 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:17338 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:33339 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17340 CheckSSLInfo(r.ssl_info());
341 } else {
[email protected]bacff652009-03-31 17:50:33342 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17343 }
[email protected]bacff652009-03-31 17:50:33344 }
345 }
346}
347
[email protected]5774ada2010-07-15 06:30:54348TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]347599952010-10-28 11:57:36349 net::TestServer::HTTPSOptions https_options(
350 net::TestServer::HTTPSOptions::CERT_EXPIRED);
351 net::TestServer test_server(https_options,
[email protected]95409e12010-08-17 20:07:11352 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
353 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:33354
355 // Iterate from false to true, just so that we do the opposite of the
356 // previous test in order to increase test coverage.
357 bool err_allowed = false;
358 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
359 TestDelegate d;
360 {
361 d.set_allow_certificate_errors(err_allowed);
[email protected]95409e12010-08-17 20:07:11362 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]bacff652009-03-31 17:50:33363
364 r.Start();
365 EXPECT_TRUE(r.is_pending());
366
367 MessageLoop::current()->Run();
368
369 EXPECT_EQ(1, d.response_started_count());
370 EXPECT_FALSE(d.received_data_before_response());
371 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:17372 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:33373 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17374 CheckSSLInfo(r.ssl_info());
375 } else {
[email protected]bacff652009-03-31 17:50:33376 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17377 }
[email protected]bacff652009-03-31 17:50:33378 }
379 }
380}
[email protected]73e0bba2009-02-19 22:57:09381
[email protected]8df162a2010-08-07 01:10:02382namespace {
383
384class SSLClientAuthTestDelegate : public TestDelegate {
385 public:
386 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
387 }
388 virtual void OnCertificateRequested(
[email protected]6981d9632010-11-30 21:34:02389 net::URLRequest* request,
[email protected]8df162a2010-08-07 01:10:02390 net::SSLCertRequestInfo* cert_request_info) {
391 on_certificate_requested_count_++;
392 MessageLoop::current()->Quit();
393 }
394 int on_certificate_requested_count() {
395 return on_certificate_requested_count_;
396 }
397 private:
398 int on_certificate_requested_count_;
399};
400
401} // namespace
402
403// TODO(davidben): Test the rest of the code. Specifically,
404// - Filtering which certificates to select.
405// - Sending a certificate back.
406// - Getting a certificate request in an SSL renegotiation sending the
407// HTTP request.
408TEST_F(HTTPSRequestTest, ClientAuthTest) {
[email protected]347599952010-10-28 11:57:36409 net::TestServer::HTTPSOptions https_options;
410 https_options.request_client_certificate = true;
411 net::TestServer test_server(https_options,
[email protected]95409e12010-08-17 20:07:11412 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
413 ASSERT_TRUE(test_server.Start());
[email protected]8df162a2010-08-07 01:10:02414
415 SSLClientAuthTestDelegate d;
416 {
[email protected]95409e12010-08-17 20:07:11417 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]8df162a2010-08-07 01:10:02418
419 r.Start();
420 EXPECT_TRUE(r.is_pending());
421
422 MessageLoop::current()->Run();
423
424 EXPECT_EQ(1, d.on_certificate_requested_count());
425 EXPECT_FALSE(d.received_data_before_response());
426 EXPECT_EQ(0, d.bytes_received());
[email protected]65a3b912010-08-21 05:46:58427
428 // Send no certificate.
429 // TODO(davidben): Get temporary client cert import (with keys) working on
430 // all platforms so we can test sending a cert as well.
431 r.ContinueWithCertificate(NULL);
432
433 MessageLoop::current()->Run();
434
435 EXPECT_EQ(1, d.response_started_count());
436 EXPECT_FALSE(d.received_data_before_response());
437 EXPECT_NE(0, d.bytes_received());
[email protected]8df162a2010-08-07 01:10:02438 }
439}
440
[email protected]37314622009-08-17 20:29:39441TEST_F(URLRequestTestHTTP, CancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52442 TestDelegate d;
443 {
444 TestURLRequest r(GURL("http://www.google.com/"), &d);
445
446 r.Start();
447 EXPECT_TRUE(r.is_pending());
448
449 r.Cancel();
450
451 MessageLoop::current()->Run();
452
453 // We expect to receive OnResponseStarted even though the request has been
454 // cancelled.
455 EXPECT_EQ(1, d.response_started_count());
456 EXPECT_EQ(0, d.bytes_received());
457 EXPECT_FALSE(d.received_data_before_response());
458 }
initial.commit586acc5fe2008-07-26 22:42:52459}
460
[email protected]37314622009-08-17 20:29:39461TEST_F(URLRequestTestHTTP, CancelTest2) {
[email protected]95409e12010-08-17 20:07:11462 ASSERT_TRUE(test_server_.Start());
[email protected]dd265012009-01-08 20:45:27463
initial.commit586acc5fe2008-07-26 22:42:52464 TestDelegate d;
465 {
[email protected]95409e12010-08-17 20:07:11466 TestURLRequest r(test_server_.GetURL(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52467
468 d.set_cancel_in_response_started(true);
469
470 r.Start();
471 EXPECT_TRUE(r.is_pending());
472
473 MessageLoop::current()->Run();
474
475 EXPECT_EQ(1, d.response_started_count());
476 EXPECT_EQ(0, d.bytes_received());
477 EXPECT_FALSE(d.received_data_before_response());
[email protected]f90bf0d92011-01-13 02:12:44478 EXPECT_EQ(net::URLRequestStatus::CANCELED, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:52479 }
initial.commit586acc5fe2008-07-26 22:42:52480}
481
[email protected]37314622009-08-17 20:29:39482TEST_F(URLRequestTestHTTP, CancelTest3) {
[email protected]95409e12010-08-17 20:07:11483 ASSERT_TRUE(test_server_.Start());
484
initial.commit586acc5fe2008-07-26 22:42:52485 TestDelegate d;
486 {
[email protected]95409e12010-08-17 20:07:11487 TestURLRequest r(test_server_.GetURL(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52488
489 d.set_cancel_in_received_data(true);
490
491 r.Start();
492 EXPECT_TRUE(r.is_pending());
493
494 MessageLoop::current()->Run();
495
496 EXPECT_EQ(1, d.response_started_count());
497 // There is no guarantee about how much data was received
498 // before the cancel was issued. It could have been 0 bytes,
499 // or it could have been all the bytes.
500 // EXPECT_EQ(0, d.bytes_received());
501 EXPECT_FALSE(d.received_data_before_response());
[email protected]f90bf0d92011-01-13 02:12:44502 EXPECT_EQ(net::URLRequestStatus::CANCELED, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:52503 }
initial.commit586acc5fe2008-07-26 22:42:52504}
505
[email protected]37314622009-08-17 20:29:39506TEST_F(URLRequestTestHTTP, CancelTest4) {
[email protected]95409e12010-08-17 20:07:11507 ASSERT_TRUE(test_server_.Start());
508
initial.commit586acc5fe2008-07-26 22:42:52509 TestDelegate d;
510 {
[email protected]95409e12010-08-17 20:07:11511 TestURLRequest r(test_server_.GetURL(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52512
513 r.Start();
514 EXPECT_TRUE(r.is_pending());
515
516 // The request will be implicitly canceled when it is destroyed. The
517 // test delegate must not post a quit message when this happens because
518 // this test doesn't actually have a message loop. The quit message would
519 // get put on this thread's message queue and the next test would exit
520 // early, causing problems.
521 d.set_quit_on_complete(false);
522 }
523 // expect things to just cleanup properly.
524
525 // we won't actually get a received reponse here because we've never run the
526 // message loop
527 EXPECT_FALSE(d.received_data_before_response());
528 EXPECT_EQ(0, d.bytes_received());
529}
530
[email protected]37314622009-08-17 20:29:39531TEST_F(URLRequestTestHTTP, CancelTest5) {
[email protected]95409e12010-08-17 20:07:11532 ASSERT_TRUE(test_server_.Start());
533
[email protected]aeb53f02011-01-15 00:21:34534 scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext());
initial.commit586acc5fe2008-07-26 22:42:52535
536 // populate cache
537 {
538 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:02539 net::URLRequest r(test_server_.GetURL("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52540 r.set_context(context);
541 r.Start();
542 MessageLoop::current()->Run();
[email protected]f90bf0d92011-01-13 02:12:44543 EXPECT_EQ(net::URLRequestStatus::SUCCESS, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:52544 }
545
546 // cancel read from cache (see bug 990242)
547 {
548 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:02549 net::URLRequest r(test_server_.GetURL("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52550 r.set_context(context);
551 r.Start();
552 r.Cancel();
553 MessageLoop::current()->Run();
554
[email protected]f90bf0d92011-01-13 02:12:44555 EXPECT_EQ(net::URLRequestStatus::CANCELED, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:52556 EXPECT_EQ(1, d.response_started_count());
557 EXPECT_EQ(0, d.bytes_received());
558 EXPECT_FALSE(d.received_data_before_response());
559 }
initial.commit586acc5fe2008-07-26 22:42:52560}
561
[email protected]37314622009-08-17 20:29:39562TEST_F(URLRequestTestHTTP, PostTest) {
[email protected]95409e12010-08-17 20:07:11563 ASSERT_TRUE(test_server_.Start());
[email protected]762d2db2010-01-11 19:03:01564 HTTPUploadDataOperationTest("POST");
565}
initial.commit586acc5fe2008-07-26 22:42:52566
[email protected]762d2db2010-01-11 19:03:01567TEST_F(URLRequestTestHTTP, PutTest) {
[email protected]95409e12010-08-17 20:07:11568 ASSERT_TRUE(test_server_.Start());
[email protected]762d2db2010-01-11 19:03:01569 HTTPUploadDataOperationTest("PUT");
initial.commit586acc5fe2008-07-26 22:42:52570}
571
[email protected]37314622009-08-17 20:29:39572TEST_F(URLRequestTestHTTP, PostEmptyTest) {
[email protected]95409e12010-08-17 20:07:11573 ASSERT_TRUE(test_server_.Start());
574
initial.commit586acc5fe2008-07-26 22:42:52575 TestDelegate d;
576 {
[email protected]95409e12010-08-17 20:07:11577 TestURLRequest r(test_server_.GetURL("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52578 r.set_method("POST");
579
580 r.Start();
581 EXPECT_TRUE(r.is_pending());
582
583 MessageLoop::current()->Run();
584
585 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
586 (int) r.status().status() << ", os error: " << r.status().os_error();
587
588 EXPECT_FALSE(d.received_data_before_response());
589 EXPECT_TRUE(d.data_received().empty());
590 }
initial.commit586acc5fe2008-07-26 22:42:52591}
592
[email protected]37314622009-08-17 20:29:39593TEST_F(URLRequestTestHTTP, PostFileTest) {
[email protected]95409e12010-08-17 20:07:11594 ASSERT_TRUE(test_server_.Start());
595
initial.commit586acc5fe2008-07-26 22:42:52596 TestDelegate d;
597 {
[email protected]95409e12010-08-17 20:07:11598 TestURLRequest r(test_server_.GetURL("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52599 r.set_method("POST");
600
[email protected]399b8702009-05-01 20:34:02601 FilePath dir;
initial.commit586acc5fe2008-07-26 22:42:52602 PathService::Get(base::DIR_EXE, &dir);
[email protected]9396b252008-09-29 17:29:38603 file_util::SetCurrentDirectory(dir);
initial.commit586acc5fe2008-07-26 22:42:52604
[email protected]72cbd322009-04-07 10:17:12605 FilePath path;
initial.commit586acc5fe2008-07-26 22:42:52606 PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]72cbd322009-04-07 10:17:12607 path = path.Append(FILE_PATH_LITERAL("net"));
608 path = path.Append(FILE_PATH_LITERAL("data"));
609 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
610 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
initial.commit586acc5fe2008-07-26 22:42:52611 r.AppendFileToUpload(path);
612
613 // This file should just be ignored in the upload stream.
[email protected]72cbd322009-04-07 10:17:12614 r.AppendFileToUpload(FilePath(FILE_PATH_LITERAL(
615 "c:\\path\\to\\non\\existant\\file.randomness.12345")));
initial.commit586acc5fe2008-07-26 22:42:52616
617 r.Start();
618 EXPECT_TRUE(r.is_pending());
619
620 MessageLoop::current()->Run();
621
[email protected]10a1fe92008-11-04 21:47:02622 int64 longsize;
623 ASSERT_EQ(true, file_util::GetFileSize(path, &longsize));
624 int size = static_cast<int>(longsize);
initial.commit586acc5fe2008-07-26 22:42:52625 scoped_array<char> buf(new char[size]);
626
[email protected]dd265012009-01-08 20:45:27627 int size_read = static_cast<int>(file_util::ReadFile(path,
628 buf.get(), size));
[email protected]eac0709a2008-11-04 21:00:46629 ASSERT_EQ(size, size_read);
initial.commit586acc5fe2008-07-26 22:42:52630
631 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
632 (int) r.status().status() << ", os error: " << r.status().os_error();
633
634 EXPECT_FALSE(d.received_data_before_response());
635
636 ASSERT_EQ(size, d.bytes_received());
637 EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size));
638 }
initial.commit586acc5fe2008-07-26 22:42:52639}
640
[email protected]7a0bb4bf2008-11-19 21:41:48641TEST_F(URLRequestTest, AboutBlankTest) {
initial.commit586acc5fe2008-07-26 22:42:52642 TestDelegate d;
643 {
644 TestURLRequest r(GURL("about:blank"), &d);
645
646 r.Start();
647 EXPECT_TRUE(r.is_pending());
648
649 MessageLoop::current()->Run();
650
651 EXPECT_TRUE(!r.is_pending());
652 EXPECT_FALSE(d.received_data_before_response());
653 EXPECT_EQ(d.bytes_received(), 0);
654 }
initial.commit586acc5fe2008-07-26 22:42:52655}
656
[email protected]fcb7fd92009-10-22 04:18:58657TEST_F(URLRequestTest, DataURLImageTest) {
658 TestDelegate d;
659 {
660 // Use our nice little Chrome logo.
661 TestURLRequest r(GURL(
662 "data:image/png;base64,"
663 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
664 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
665 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
666 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
667 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
668 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
669 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
670 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
671 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
672 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
673 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
674 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
675 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
676 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
677 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
678 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
679 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
680 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
681 &d);
682
683 r.Start();
684 EXPECT_TRUE(r.is_pending());
685
686 MessageLoop::current()->Run();
687
688 EXPECT_TRUE(!r.is_pending());
689 EXPECT_FALSE(d.received_data_before_response());
690 EXPECT_EQ(d.bytes_received(), 911);
691 }
692}
693
[email protected]7a0bb4bf2008-11-19 21:41:48694TEST_F(URLRequestTest, FileTest) {
[email protected]b9e04f02008-11-27 04:03:57695 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52696 PathService::Get(base::FILE_EXE, &app_path);
[email protected]72cbd322009-04-07 10:17:12697 GURL app_url = net::FilePathToFileURL(app_path);
initial.commit586acc5fe2008-07-26 22:42:52698
699 TestDelegate d;
700 {
[email protected]b9e04f02008-11-27 04:03:57701 TestURLRequest r(app_url, &d);
initial.commit586acc5fe2008-07-26 22:42:52702
703 r.Start();
704 EXPECT_TRUE(r.is_pending());
705
706 MessageLoop::current()->Run();
707
[email protected]b77280c2009-03-20 17:27:47708 int64 file_size = -1;
709 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
initial.commit586acc5fe2008-07-26 22:42:52710
711 EXPECT_TRUE(!r.is_pending());
712 EXPECT_EQ(1, d.response_started_count());
713 EXPECT_FALSE(d.received_data_before_response());
[email protected]9396b252008-09-29 17:29:38714 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
initial.commit586acc5fe2008-07-26 22:42:52715 }
initial.commit586acc5fe2008-07-26 22:42:52716}
717
[email protected]661376a2009-04-29 02:04:23718TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
719 const size_t buffer_size = 4000;
720 scoped_array<char> buffer(new char[buffer_size]);
721 FillBuffer(buffer.get(), buffer_size);
722
723 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55724 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23725 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53726 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23727
728 int64 file_size;
729 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
730
731 const size_t first_byte_position = 500;
732 const size_t last_byte_position = buffer_size - first_byte_position;
733 const size_t content_length = last_byte_position - first_byte_position + 1;
734 std::string partial_buffer_string(buffer.get() + first_byte_position,
735 buffer.get() + last_byte_position + 1);
736
737 TestDelegate d;
738 {
739 TestURLRequest r(temp_url, &d);
740
[email protected]88e6b6f32010-05-07 23:14:25741 net::HttpRequestHeaders headers;
742 headers.SetHeader(net::HttpRequestHeaders::kRange,
[email protected]d8eb84242010-09-25 02:25:06743 base::StringPrintf(
744 "bytes=%" PRIuS "-%" PRIuS,
745 first_byte_position, last_byte_position));
[email protected]88e6b6f32010-05-07 23:14:25746 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:23747 r.Start();
748 EXPECT_TRUE(r.is_pending());
749
750 MessageLoop::current()->Run();
751 EXPECT_TRUE(!r.is_pending());
752 EXPECT_EQ(1, d.response_started_count());
753 EXPECT_FALSE(d.received_data_before_response());
754 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
755 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
756 EXPECT_TRUE(partial_buffer_string == d.data_received());
757 }
758
759 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23760}
761
762TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
763 const size_t buffer_size = 4000;
764 scoped_array<char> buffer(new char[buffer_size]);
765 FillBuffer(buffer.get(), buffer_size);
766
767 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55768 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23769 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53770 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23771
772 int64 file_size;
773 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
774
775 const size_t first_byte_position = 500;
776 const size_t last_byte_position = buffer_size - 1;
777 const size_t content_length = last_byte_position - first_byte_position + 1;
778 std::string partial_buffer_string(buffer.get() + first_byte_position,
779 buffer.get() + last_byte_position + 1);
780
781 TestDelegate d;
782 {
783 TestURLRequest r(temp_url, &d);
784
[email protected]88e6b6f32010-05-07 23:14:25785 net::HttpRequestHeaders headers;
786 headers.SetHeader(net::HttpRequestHeaders::kRange,
[email protected]d8eb84242010-09-25 02:25:06787 base::StringPrintf("bytes=%" PRIuS "-",
788 first_byte_position));
[email protected]88e6b6f32010-05-07 23:14:25789 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:23790 r.Start();
791 EXPECT_TRUE(r.is_pending());
792
793 MessageLoop::current()->Run();
794 EXPECT_TRUE(!r.is_pending());
795 EXPECT_EQ(1, d.response_started_count());
796 EXPECT_FALSE(d.received_data_before_response());
797 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
798 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
799 EXPECT_TRUE(partial_buffer_string == d.data_received());
800 }
801
802 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23803}
804
805TEST_F(URLRequestTest, FileTestMultipleRanges) {
806 const size_t buffer_size = 400000;
807 scoped_array<char> buffer(new char[buffer_size]);
808 FillBuffer(buffer.get(), buffer_size);
809
810 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:55811 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]661376a2009-04-29 02:04:23812 GURL temp_url = net::FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:53813 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:23814
815 int64 file_size;
816 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
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 "bytes=0-0,10-200,200-300");
825 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:23826 r.Start();
827 EXPECT_TRUE(r.is_pending());
828
829 MessageLoop::current()->Run();
830 EXPECT_TRUE(d.request_failed());
831 }
832
833 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:23834}
835
[email protected]7a0bb4bf2008-11-19 21:41:48836TEST_F(URLRequestTest, InvalidUrlTest) {
initial.commit586acc5fe2008-07-26 22:42:52837 TestDelegate d;
838 {
839 TestURLRequest r(GURL("invalid url"), &d);
840
841 r.Start();
842 EXPECT_TRUE(r.is_pending());
843
844 MessageLoop::current()->Run();
845 EXPECT_TRUE(d.request_failed());
846 }
initial.commit586acc5fe2008-07-26 22:42:52847}
848
[email protected]37314622009-08-17 20:29:39849TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
[email protected]95409e12010-08-17 20:07:11850 ASSERT_TRUE(test_server_.Start());
851
initial.commit586acc5fe2008-07-26 22:42:52852 TestDelegate d;
[email protected]95409e12010-08-17 20:07:11853 TestURLRequest req(test_server_.GetURL("files/with-headers.html"), &d);
initial.commit586acc5fe2008-07-26 22:42:52854 req.Start();
855 MessageLoop::current()->Run();
856
857 const net::HttpResponseHeaders* headers = req.response_headers();
[email protected]589deddb2009-10-05 23:41:40858
859 // Simple sanity check that response_info() accesses the same data.
860 EXPECT_EQ(headers, req.response_info().headers.get());
861
initial.commit586acc5fe2008-07-26 22:42:52862 std::string header;
863 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
864 EXPECT_EQ("private", header);
865
866 header.clear();
867 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
868 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
869
870 // The response has two "X-Multiple-Entries" headers.
871 // This verfies our output has them concatenated together.
872 header.clear();
873 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
874 EXPECT_EQ("a, b", header);
875}
876
[email protected]9396b252008-09-29 17:29:38877#if defined(OS_WIN)
[email protected]7a0bb4bf2008-11-19 21:41:48878TEST_F(URLRequestTest, ResolveShortcutTest) {
[email protected]399b8702009-05-01 20:34:02879 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52880 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:02881 app_path = app_path.AppendASCII("net");
882 app_path = app_path.AppendASCII("data");
883 app_path = app_path.AppendASCII("url_request_unittest");
884 app_path = app_path.AppendASCII("with-headers.html");
initial.commit586acc5fe2008-07-26 22:42:52885
[email protected]399b8702009-05-01 20:34:02886 std::wstring lnk_path = app_path.value() + L".lnk";
initial.commit586acc5fe2008-07-26 22:42:52887
888 HRESULT result;
889 IShellLink *shell = NULL;
890 IPersistFile *persist = NULL;
891
892 CoInitialize(NULL);
893 // Temporarily create a shortcut for test
894 result = CoCreateInstance(CLSID_ShellLink, NULL,
[email protected]5d7b373e2009-09-02 07:19:03895 CLSCTX_INPROC_SERVER, IID_IShellLink,
896 reinterpret_cast<LPVOID*>(&shell));
[email protected]d149ce82009-07-01 23:57:02897 ASSERT_TRUE(SUCCEEDED(result));
initial.commit586acc5fe2008-07-26 22:42:52898 result = shell->QueryInterface(IID_IPersistFile,
[email protected]5d7b373e2009-09-02 07:19:03899 reinterpret_cast<LPVOID*>(&persist));
[email protected]d149ce82009-07-01 23:57:02900 ASSERT_TRUE(SUCCEEDED(result));
[email protected]399b8702009-05-01 20:34:02901 result = shell->SetPath(app_path.value().c_str());
initial.commit586acc5fe2008-07-26 22:42:52902 EXPECT_TRUE(SUCCEEDED(result));
903 result = shell->SetDescription(L"ResolveShortcutTest");
904 EXPECT_TRUE(SUCCEEDED(result));
905 result = persist->Save(lnk_path.c_str(), TRUE);
906 EXPECT_TRUE(SUCCEEDED(result));
907 if (persist)
908 persist->Release();
909 if (shell)
910 shell->Release();
911
912 TestDelegate d;
913 {
[email protected]72cbd322009-04-07 10:17:12914 TestURLRequest r(net::FilePathToFileURL(FilePath(lnk_path)), &d);
initial.commit586acc5fe2008-07-26 22:42:52915
916 r.Start();
917 EXPECT_TRUE(r.is_pending());
918
919 MessageLoop::current()->Run();
920
921 WIN32_FILE_ATTRIBUTE_DATA data;
[email protected]399b8702009-05-01 20:34:02922 GetFileAttributesEx(app_path.value().c_str(),
923 GetFileExInfoStandard, &data);
924 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
initial.commit586acc5fe2008-07-26 22:42:52925 FILE_SHARE_READ, NULL, OPEN_EXISTING,
926 FILE_ATTRIBUTE_NORMAL, NULL);
927 EXPECT_NE(INVALID_HANDLE_VALUE, file);
928 scoped_array<char> buffer(new char[data.nFileSizeLow]);
929 DWORD read_size;
930 BOOL result;
931 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
932 &read_size, NULL);
933 std::string content(buffer.get(), read_size);
934 CloseHandle(file);
935
936 EXPECT_TRUE(!r.is_pending());
937 EXPECT_EQ(1, d.received_redirect_count());
938 EXPECT_EQ(content, d.data_received());
939 }
940
941 // Clean the shortcut
942 DeleteFile(lnk_path.c_str());
943 CoUninitialize();
initial.commit586acc5fe2008-07-26 22:42:52944}
[email protected]9396b252008-09-29 17:29:38945#endif // defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:52946
[email protected]37314622009-08-17 20:29:39947TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
[email protected]95409e12010-08-17 20:07:11948 ASSERT_TRUE(test_server_.Start());
[email protected]dd265012009-01-08 20:45:27949
initial.commit586acc5fe2008-07-26 22:42:52950 TestDelegate d;
[email protected]95409e12010-08-17 20:07:11951 TestURLRequest req(test_server_.GetURL(
initial.commit586acc5fe2008-07-26 22:42:52952 "files/content-type-normalization.html"), &d);
953 req.Start();
954 MessageLoop::current()->Run();
955
956 std::string mime_type;
957 req.GetMimeType(&mime_type);
958 EXPECT_EQ("text/html", mime_type);
959
960 std::string charset;
961 req.GetCharset(&charset);
962 EXPECT_EQ("utf-8", charset);
963 req.Cancel();
964}
965
[email protected]7a0bb4bf2008-11-19 21:41:48966TEST_F(URLRequestTest, FileDirCancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52967 // Put in mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37968 net::NetModule::SetResourceProvider(TestNetResourceProvider);
initial.commit586acc5fe2008-07-26 22:42:52969
970 TestDelegate d;
971 {
[email protected]72cbd322009-04-07 10:17:12972 FilePath file_path;
initial.commit586acc5fe2008-07-26 22:42:52973 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
[email protected]72cbd322009-04-07 10:17:12974 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
975 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
initial.commit586acc5fe2008-07-26 22:42:52976
[email protected]8ac1a752008-07-31 19:40:37977 TestURLRequest req(net::FilePathToFileURL(file_path), &d);
initial.commit586acc5fe2008-07-26 22:42:52978 req.Start();
979 EXPECT_TRUE(req.is_pending());
980
981 d.set_cancel_in_received_data_pending(true);
982
983 MessageLoop::current()->Run();
984 }
initial.commit586acc5fe2008-07-26 22:42:52985
986 // Take out mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37987 net::NetModule::SetResourceProvider(NULL);
initial.commit586acc5fe2008-07-26 22:42:52988}
989
[email protected]7886a8c2009-08-21 04:11:09990TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
991 // There is an implicit redirect when loading a file path that matches a
992 // directory and does not end with a slash. Ensure that following such
993 // redirects does not crash. See http://crbug.com/18686.
994
995 FilePath path;
996 PathService::Get(base::DIR_SOURCE_ROOT, &path);
997 path = path.Append(FILE_PATH_LITERAL("net"));
998 path = path.Append(FILE_PATH_LITERAL("data"));
999 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1000
1001 TestDelegate d;
[email protected]7886a8c2009-08-21 04:11:091002 TestURLRequest req(net::FilePathToFileURL(path), &d);
1003 req.Start();
1004 MessageLoop::current()->Run();
1005
[email protected]0db55772010-06-14 22:27:441006 ASSERT_EQ(1, d.received_redirect_count());
1007 ASSERT_LT(0, d.bytes_received());
1008 ASSERT_FALSE(d.request_failed());
1009 ASSERT_TRUE(req.status().is_success());
[email protected]7886a8c2009-08-21 04:11:091010}
1011
[email protected]0db55772010-06-14 22:27:441012#if defined(OS_WIN)
1013// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
1014TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
1015 TestDelegate d;
1016 TestURLRequest req(GURL("file:///"), &d);
1017 req.Start();
1018 MessageLoop::current()->Run();
1019
1020 ASSERT_EQ(1, d.received_redirect_count());
1021 ASSERT_FALSE(req.status().is_success());
1022}
1023#endif
1024
[email protected]37314622009-08-17 20:29:391025TEST_F(URLRequestTestHTTP, RestrictRedirects) {
[email protected]95409e12010-08-17 20:07:111026 ASSERT_TRUE(test_server_.Start());
[email protected]dd265012009-01-08 20:45:271027
initial.commit586acc5fe2008-07-26 22:42:521028 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111029 TestURLRequest req(test_server_.GetURL(
initial.commit586acc5fe2008-07-26 22:42:521030 "files/redirect-to-file.html"), &d);
1031 req.Start();
1032 MessageLoop::current()->Run();
1033
[email protected]f90bf0d92011-01-13 02:12:441034 EXPECT_EQ(net::URLRequestStatus::FAILED, req.status().status());
initial.commit586acc5fe2008-07-26 22:42:521035 EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error());
1036}
1037
[email protected]37314622009-08-17 20:29:391038TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
[email protected]95409e12010-08-17 20:07:111039 ASSERT_TRUE(test_server_.Start());
[email protected]4e66ed12009-07-21 23:38:421040
1041 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111042 TestURLRequest req(test_server_.GetURL(
[email protected]4e66ed12009-07-21 23:38:421043 "files/redirect-to-invalid-url.html"), &d);
1044 req.Start();
1045 MessageLoop::current()->Run();
1046
[email protected]f90bf0d92011-01-13 02:12:441047 EXPECT_EQ(net::URLRequestStatus::FAILED, req.status().status());
[email protected]4e66ed12009-07-21 23:38:421048 EXPECT_EQ(net::ERR_INVALID_URL, req.status().os_error());
1049}
1050
[email protected]37314622009-08-17 20:29:391051TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
[email protected]95409e12010-08-17 20:07:111052 ASSERT_TRUE(test_server_.Start());
1053
initial.commit586acc5fe2008-07-26 22:42:521054 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111055 TestURLRequest req(test_server_.GetURL(
initial.commit586acc5fe2008-07-26 22:42:521056 "echoheader?Referer"), &d);
1057 req.set_referrer("http://user:[email protected]/");
1058 req.Start();
1059 MessageLoop::current()->Run();
1060
1061 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
1062}
1063
[email protected]37314622009-08-17 20:29:391064TEST_F(URLRequestTestHTTP, CancelRedirect) {
[email protected]95409e12010-08-17 20:07:111065 ASSERT_TRUE(test_server_.Start());
1066
initial.commit586acc5fe2008-07-26 22:42:521067 TestDelegate d;
1068 {
1069 d.set_cancel_in_received_redirect(true);
[email protected]95409e12010-08-17 20:07:111070 TestURLRequest req(test_server_.GetURL(
initial.commit586acc5fe2008-07-26 22:42:521071 "files/redirect-test.html"), &d);
1072 req.Start();
1073 MessageLoop::current()->Run();
1074
1075 EXPECT_EQ(1, d.response_started_count());
1076 EXPECT_EQ(0, d.bytes_received());
1077 EXPECT_FALSE(d.received_data_before_response());
[email protected]f90bf0d92011-01-13 02:12:441078 EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status());
initial.commit586acc5fe2008-07-26 22:42:521079 }
1080}
1081
[email protected]37314622009-08-17 20:29:391082TEST_F(URLRequestTestHTTP, DeferredRedirect) {
[email protected]95409e12010-08-17 20:07:111083 ASSERT_TRUE(test_server_.Start());
1084
[email protected]195e77d2009-07-23 19:10:231085 TestDelegate d;
1086 {
1087 d.set_quit_on_redirect(true);
[email protected]95409e12010-08-17 20:07:111088 TestURLRequest req(test_server_.GetURL(
[email protected]195e77d2009-07-23 19:10:231089 "files/redirect-test.html"), &d);
1090 req.Start();
1091 MessageLoop::current()->Run();
1092
1093 EXPECT_EQ(1, d.received_redirect_count());
1094
1095 req.FollowDeferredRedirect();
1096 MessageLoop::current()->Run();
1097
1098 EXPECT_EQ(1, d.response_started_count());
1099 EXPECT_FALSE(d.received_data_before_response());
[email protected]f90bf0d92011-01-13 02:12:441100 EXPECT_EQ(net::URLRequestStatus::SUCCESS, req.status().status());
[email protected]195e77d2009-07-23 19:10:231101
1102 FilePath path;
1103 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1104 path = path.Append(FILE_PATH_LITERAL("net"));
1105 path = path.Append(FILE_PATH_LITERAL("data"));
1106 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1107 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
1108
1109 std::string contents;
1110 EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
1111 EXPECT_EQ(contents, d.data_received());
1112 }
1113}
1114
[email protected]37314622009-08-17 20:29:391115TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
[email protected]95409e12010-08-17 20:07:111116 ASSERT_TRUE(test_server_.Start());
1117
[email protected]195e77d2009-07-23 19:10:231118 TestDelegate d;
1119 {
1120 d.set_quit_on_redirect(true);
[email protected]95409e12010-08-17 20:07:111121 TestURLRequest req(test_server_.GetURL(
[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.Cancel();
1129 MessageLoop::current()->Run();
1130
1131 EXPECT_EQ(1, d.response_started_count());
1132 EXPECT_EQ(0, d.bytes_received());
1133 EXPECT_FALSE(d.received_data_before_response());
[email protected]f90bf0d92011-01-13 02:12:441134 EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status());
[email protected]195e77d2009-07-23 19:10:231135 }
1136}
1137
[email protected]37314622009-08-17 20:29:391138TEST_F(URLRequestTestHTTP, VaryHeader) {
[email protected]95409e12010-08-17 20:07:111139 ASSERT_TRUE(test_server_.Start());
initial.commit586acc5fe2008-07-26 22:42:521140
[email protected]aeb53f02011-01-15 00:21:341141 scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext());
initial.commit586acc5fe2008-07-26 22:42:521142
initial.commit586acc5fe2008-07-26 22:42:521143 // populate the cache
1144 {
1145 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021146 net::URLRequest req(test_server_.GetURL("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521147 req.set_context(context);
[email protected]88e6b6f32010-05-07 23:14:251148 net::HttpRequestHeaders headers;
1149 headers.SetHeader("foo", "1");
1150 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:521151 req.Start();
1152 MessageLoop::current()->Run();
initial.commit586acc5fe2008-07-26 22:42:521153 }
1154
initial.commit586acc5fe2008-07-26 22:42:521155 // expect a cache hit
1156 {
1157 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021158 net::URLRequest req(test_server_.GetURL("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521159 req.set_context(context);
[email protected]88e6b6f32010-05-07 23:14:251160 net::HttpRequestHeaders headers;
1161 headers.SetHeader("foo", "1");
1162 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:521163 req.Start();
1164 MessageLoop::current()->Run();
1165
[email protected]cb4ff9d2009-09-04 22:51:531166 EXPECT_TRUE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521167 }
1168
1169 // expect a cache miss
1170 {
1171 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021172 net::URLRequest req(test_server_.GetURL("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:521173 req.set_context(context);
[email protected]88e6b6f32010-05-07 23:14:251174 net::HttpRequestHeaders headers;
1175 headers.SetHeader("foo", "2");
1176 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:521177 req.Start();
1178 MessageLoop::current()->Run();
1179
[email protected]cb4ff9d2009-09-04 22:51:531180 EXPECT_FALSE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521181 }
1182}
1183
[email protected]cb4ff9d2009-09-04 22:51:531184TEST_F(URLRequestTestHTTP, BasicAuth) {
[email protected]95409e12010-08-17 20:07:111185 ASSERT_TRUE(test_server_.Start());
1186
[email protected]aeb53f02011-01-15 00:21:341187 scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext());
initial.commit586acc5fe2008-07-26 22:42:521188
initial.commit586acc5fe2008-07-26 22:42:521189 // populate the cache
1190 {
1191 TestDelegate d;
[email protected]13c8a092010-07-29 06:15:441192 d.set_username(kUser);
1193 d.set_password(kSecret);
initial.commit586acc5fe2008-07-26 22:42:521194
[email protected]6981d9632010-11-30 21:34:021195 net::URLRequest r(test_server_.GetURL("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:521196 r.set_context(context);
1197 r.Start();
1198
1199 MessageLoop::current()->Run();
1200
1201 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
initial.commit586acc5fe2008-07-26 22:42:521202 }
1203
initial.commit586acc5fe2008-07-26 22:42:521204 // repeat request with end-to-end validation. since auth-basic results in a
1205 // cachable page, we expect this test to result in a 304. in which case, the
1206 // response should be fetched from the cache.
1207 {
1208 TestDelegate d;
[email protected]13c8a092010-07-29 06:15:441209 d.set_username(kUser);
1210 d.set_password(kSecret);
initial.commit586acc5fe2008-07-26 22:42:521211
[email protected]6981d9632010-11-30 21:34:021212 net::URLRequest r(test_server_.GetURL("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:521213 r.set_context(context);
1214 r.set_load_flags(net::LOAD_VALIDATE_CACHE);
1215 r.Start();
1216
1217 MessageLoop::current()->Run();
1218
1219 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1220
[email protected]cb4ff9d2009-09-04 22:51:531221 // Should be the same cached document.
1222 EXPECT_TRUE(r.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521223 }
1224}
license.botbf09a502008-08-24 00:55:551225
[email protected]0757e7702009-03-27 04:00:221226// Check that Set-Cookie headers in 401 responses are respected.
1227// http://crbug.com/6450
[email protected]be6fca6c2010-01-30 21:48:571228TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
[email protected]95409e12010-08-17 20:07:111229 ASSERT_TRUE(test_server_.Start());
[email protected]0757e7702009-03-27 04:00:221230
1231 GURL url_requiring_auth =
[email protected]95409e12010-08-17 20:07:111232 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
[email protected]0757e7702009-03-27 04:00:221233
1234 // Request a page that will give a 401 containing a Set-Cookie header.
1235 // Verify that when the transaction is restarted, it includes the new cookie.
1236 {
[email protected]aeb53f02011-01-15 00:21:341237 scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext());
[email protected]0757e7702009-03-27 04:00:221238 TestDelegate d;
[email protected]13c8a092010-07-29 06:15:441239 d.set_username(kUser);
1240 d.set_password(kSecret);
[email protected]0757e7702009-03-27 04:00:221241
[email protected]6981d9632010-11-30 21:34:021242 net::URLRequest r(url_requiring_auth, &d);
[email protected]0757e7702009-03-27 04:00:221243 r.set_context(context);
1244 r.Start();
1245
1246 MessageLoop::current()->Run();
1247
1248 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1249
1250 // Make sure we sent the cookie in the restarted transaction.
1251 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1252 != std::string::npos);
1253 }
1254
1255 // Same test as above, except this time the restart is initiated earlier
1256 // (without user intervention since identity is embedded in the URL).
1257 {
[email protected]aeb53f02011-01-15 00:21:341258 scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext());
[email protected]0757e7702009-03-27 04:00:221259 TestDelegate d;
1260
1261 GURL::Replacements replacements;
1262 std::string username("user2");
1263 std::string password("secret");
1264 replacements.SetUsernameStr(username);
1265 replacements.SetPasswordStr(password);
1266 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
1267
[email protected]6981d9632010-11-30 21:34:021268 net::URLRequest r(url_with_identity, &d);
[email protected]0757e7702009-03-27 04:00:221269 r.set_context(context);
1270 r.Start();
1271
1272 MessageLoop::current()->Run();
1273
1274 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
1275
1276 // Make sure we sent the cookie in the restarted transaction.
1277 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1278 != std::string::npos);
1279 }
1280}
1281
[email protected]be6fca6c2010-01-30 21:48:571282TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]95409e12010-08-17 20:07:111283 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1284 ASSERT_TRUE(test_server.Start());
1285
[email protected]aeb53f02011-01-15 00:21:341286 scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext());
[email protected]861fcd52009-08-26 02:33:461287
1288 // Set up a cookie.
1289 {
1290 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021291 net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]861fcd52009-08-26 02:33:461292 req.set_context(context);
1293 req.Start();
1294 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201295 EXPECT_EQ(0, d.blocked_get_cookies_count());
1296 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461297 }
1298
1299 // Verify that the cookie is set.
1300 {
1301 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111302 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]861fcd52009-08-26 02:33:461303 req.set_context(context);
1304 req.Start();
1305 MessageLoop::current()->Run();
1306
1307 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1308 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201309 EXPECT_EQ(0, d.blocked_get_cookies_count());
1310 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461311 }
1312
1313 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
1314 {
1315 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111316 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]861fcd52009-08-26 02:33:461317 req.set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES);
1318 req.set_context(context);
1319 req.Start();
1320 MessageLoop::current()->Run();
1321
1322 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1323 == std::string::npos);
[email protected]abf03a02010-03-12 05:02:131324
[email protected]9fb83e82010-07-02 18:24:551325 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
[email protected]abf03a02010-03-12 05:02:131326 EXPECT_EQ(0, d.blocked_get_cookies_count());
[email protected]3dbb80b2010-02-09 22:41:201327 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461328 }
1329}
1330
[email protected]be6fca6c2010-01-30 21:48:571331TEST_F(URLRequestTest, DoNotSaveCookies) {
[email protected]95409e12010-08-17 20:07:111332 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1333 ASSERT_TRUE(test_server.Start());
1334
[email protected]aeb53f02011-01-15 00:21:341335 scoped_refptr<net::URLRequestContext> context(new TestURLRequestContext());
[email protected]861fcd52009-08-26 02:33:461336
1337 // Set up a cookie.
1338 {
1339 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021340 net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
1341 &d);
[email protected]861fcd52009-08-26 02:33:461342 req.set_context(context);
1343 req.Start();
1344 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201345
1346 EXPECT_EQ(0, d.blocked_get_cookies_count());
1347 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:551348 EXPECT_EQ(1, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461349 }
1350
1351 // Try to set-up another cookie and update the previous cookie.
1352 {
[email protected]861fcd52009-08-26 02:33:461353 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021354 net::URLRequest req(test_server.GetURL(
[email protected]861fcd52009-08-26 02:33:461355 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
1356 req.set_load_flags(net::LOAD_DO_NOT_SAVE_COOKIES);
1357 req.set_context(context);
1358 req.Start();
1359
1360 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201361
[email protected]9fb83e82010-07-02 18:24:551362 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
[email protected]3dbb80b2010-02-09 22:41:201363 EXPECT_EQ(0, d.blocked_get_cookies_count());
[email protected]abf03a02010-03-12 05:02:131364 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:551365 EXPECT_EQ(0, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461366 }
1367
1368 // Verify the cookies weren't saved or updated.
1369 {
1370 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111371 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]861fcd52009-08-26 02:33:461372 req.set_context(context);
1373 req.Start();
1374 MessageLoop::current()->Run();
1375
1376 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1377 == std::string::npos);
1378 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1379 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201380
1381 EXPECT_EQ(0, d.blocked_get_cookies_count());
1382 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:551383 EXPECT_EQ(0, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461384 }
1385}
1386
[email protected]34602282010-02-03 22:14:151387TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
[email protected]95409e12010-08-17 20:07:111388 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1389 ASSERT_TRUE(test_server.Start());
1390
[email protected]ad8e04a2010-11-01 04:16:271391 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
[email protected]34602282010-02-03 22:14:151392
1393 // Set up a cookie.
1394 {
1395 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021396 net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]34602282010-02-03 22:14:151397 req.set_context(context);
1398 req.Start();
1399 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201400
1401 EXPECT_EQ(0, d.blocked_get_cookies_count());
1402 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151403 }
1404
1405 // Verify that the cookie is set.
1406 {
1407 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111408 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]34602282010-02-03 22:14:151409 req.set_context(context);
1410 req.Start();
1411 MessageLoop::current()->Run();
1412
1413 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1414 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201415
1416 EXPECT_EQ(0, d.blocked_get_cookies_count());
1417 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151418 }
1419
1420 // Verify that the cookie isn't sent.
1421 {
1422 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES);
1423 context->set_cookie_policy(&cookie_policy);
1424
1425 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111426 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]34602282010-02-03 22:14:151427 req.set_context(context);
1428 req.Start();
1429 MessageLoop::current()->Run();
1430
1431 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1432 == std::string::npos);
1433
1434 context->set_cookie_policy(NULL);
[email protected]3dbb80b2010-02-09 22:41:201435
1436 EXPECT_EQ(1, d.blocked_get_cookies_count());
1437 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151438 }
1439}
1440
1441TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
[email protected]95409e12010-08-17 20:07:111442 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1443 ASSERT_TRUE(test_server.Start());
1444
[email protected]ad8e04a2010-11-01 04:16:271445 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
[email protected]34602282010-02-03 22:14:151446
1447 // Set up a cookie.
1448 {
1449 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021450 net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
1451 &d);
[email protected]34602282010-02-03 22:14:151452 req.set_context(context);
1453 req.Start();
1454 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201455
1456 EXPECT_EQ(0, d.blocked_get_cookies_count());
1457 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151458 }
1459
1460 // Try to set-up another cookie and update the previous cookie.
1461 {
1462 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE);
1463 context->set_cookie_policy(&cookie_policy);
1464
1465 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021466 net::URLRequest req(test_server.GetURL(
[email protected]34602282010-02-03 22:14:151467 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
1468 req.set_context(context);
1469 req.Start();
1470
1471 MessageLoop::current()->Run();
1472
1473 context->set_cookie_policy(NULL);
[email protected]3dbb80b2010-02-09 22:41:201474
1475 EXPECT_EQ(0, d.blocked_get_cookies_count());
1476 EXPECT_EQ(2, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151477 }
1478
1479
1480 // Verify the cookies weren't saved or updated.
1481 {
1482 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111483 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]34602282010-02-03 22:14:151484 req.set_context(context);
1485 req.Start();
1486 MessageLoop::current()->Run();
1487
1488 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1489 == std::string::npos);
1490 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1491 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201492
1493 EXPECT_EQ(0, d.blocked_get_cookies_count());
1494 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151495 }
1496}
1497
[email protected]2adf2882010-09-27 08:30:371498TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
1499 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1500 ASSERT_TRUE(test_server.Start());
1501
[email protected]ad8e04a2010-11-01 04:16:271502 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
[email protected]2adf2882010-09-27 08:30:371503
1504 // Set up an empty cookie.
1505 {
1506 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021507 net::URLRequest req(test_server.GetURL("set-cookie"), &d);
[email protected]2adf2882010-09-27 08:30:371508 req.set_context(context);
1509 req.Start();
1510 MessageLoop::current()->Run();
1511
1512 EXPECT_EQ(0, d.blocked_get_cookies_count());
1513 EXPECT_EQ(0, d.blocked_set_cookie_count());
1514 EXPECT_EQ(0, d.set_cookie_count());
1515 }
1516}
1517
[email protected]34602282010-02-03 22:14:151518TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
[email protected]95409e12010-08-17 20:07:111519 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1520 ASSERT_TRUE(test_server.Start());
1521
[email protected]ad8e04a2010-11-01 04:16:271522 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
[email protected]34602282010-02-03 22:14:151523
1524 // Set up a cookie.
1525 {
1526 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021527 net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]34602282010-02-03 22:14:151528 req.set_context(context);
1529 req.Start();
1530 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201531
1532 EXPECT_EQ(0, d.blocked_get_cookies_count());
1533 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151534 }
1535
1536 // Verify that the cookie is set.
1537 {
1538 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111539 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]34602282010-02-03 22:14:151540 req.set_context(context);
1541 req.Start();
1542 MessageLoop::current()->Run();
1543
1544 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1545 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201546
1547 EXPECT_EQ(0, d.blocked_get_cookies_count());
1548 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151549 }
1550
1551 // Verify that the cookie isn't sent.
1552 {
1553 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES |
1554 TestCookiePolicy::ASYNC);
1555 context->set_cookie_policy(&cookie_policy);
1556
1557 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111558 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]34602282010-02-03 22:14:151559 req.set_context(context);
1560 req.Start();
1561 MessageLoop::current()->Run();
1562
1563 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1564 == std::string::npos);
1565
1566 context->set_cookie_policy(NULL);
[email protected]3dbb80b2010-02-09 22:41:201567
1568 EXPECT_EQ(1, d.blocked_get_cookies_count());
1569 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151570 }
1571}
1572
1573TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
[email protected]95409e12010-08-17 20:07:111574 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1575 ASSERT_TRUE(test_server.Start());
1576
[email protected]ad8e04a2010-11-01 04:16:271577 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
[email protected]34602282010-02-03 22:14:151578
1579 // Set up a cookie.
1580 {
1581 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021582 net::URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
1583 &d);
[email protected]34602282010-02-03 22:14:151584 req.set_context(context);
1585 req.Start();
1586 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201587
1588 EXPECT_EQ(0, d.blocked_get_cookies_count());
1589 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151590 }
1591
1592 // Try to set-up another cookie and update the previous cookie.
1593 {
1594 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE |
1595 TestCookiePolicy::ASYNC);
1596 context->set_cookie_policy(&cookie_policy);
1597
1598 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021599 net::URLRequest req(test_server.GetURL(
[email protected]34602282010-02-03 22:14:151600 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
1601 req.set_context(context);
1602 req.Start();
1603
1604 MessageLoop::current()->Run();
1605
1606 context->set_cookie_policy(NULL);
[email protected]3dbb80b2010-02-09 22:41:201607
1608 EXPECT_EQ(0, d.blocked_get_cookies_count());
1609 EXPECT_EQ(2, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151610 }
1611
1612 // Verify the cookies weren't saved or updated.
1613 {
1614 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111615 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]34602282010-02-03 22:14:151616 req.set_context(context);
1617 req.Start();
1618 MessageLoop::current()->Run();
1619
1620 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1621 == std::string::npos);
1622 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1623 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201624
1625 EXPECT_EQ(0, d.blocked_get_cookies_count());
1626 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151627 }
1628}
1629
[email protected]3dbb80b2010-02-09 22:41:201630TEST_F(URLRequestTest, CancelTest_During_CookiePolicy) {
[email protected]95409e12010-08-17 20:07:111631 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1632 ASSERT_TRUE(test_server.Start());
1633
[email protected]ad8e04a2010-11-01 04:16:271634 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
[email protected]34602282010-02-03 22:14:151635
1636 TestCookiePolicy cookie_policy(TestCookiePolicy::ASYNC);
1637 context->set_cookie_policy(&cookie_policy);
1638
1639 // Set up a cookie.
1640 {
1641 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021642 net::URLRequest req(test_server.GetURL("set-cookie?A=1&B=2&C=3"),
1643 &d);
[email protected]34602282010-02-03 22:14:151644 req.set_context(context);
1645 req.Start(); // Triggers an asynchronous cookie policy check.
1646
[email protected]3dbb80b2010-02-09 22:41:201647 // But, now we cancel the request by letting it go out of scope. This
1648 // should not cause a crash.
1649
1650 EXPECT_EQ(0, d.blocked_get_cookies_count());
1651 EXPECT_EQ(0, d.blocked_set_cookie_count());
1652 }
1653
1654 context->set_cookie_policy(NULL);
1655
1656 // Let the cookie policy complete. Make sure it handles the destruction of
[email protected]6981d9632010-11-30 21:34:021657 // the net::URLRequest properly.
[email protected]3dbb80b2010-02-09 22:41:201658 MessageLoop::current()->RunAllPending();
1659}
1660
[email protected]9fb83e82010-07-02 18:24:551661TEST_F(URLRequestTest, CancelTest_During_OnGetCookies) {
[email protected]95409e12010-08-17 20:07:111662 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1663 ASSERT_TRUE(test_server.Start());
1664
[email protected]ad8e04a2010-11-01 04:16:271665 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
[email protected]3dbb80b2010-02-09 22:41:201666
1667 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES);
1668 context->set_cookie_policy(&cookie_policy);
1669
1670 // Set up a cookie.
1671 {
1672 TestDelegate d;
1673 d.set_cancel_in_get_cookies_blocked(true);
[email protected]6981d9632010-11-30 21:34:021674 net::URLRequest req(test_server.GetURL("set-cookie?A=1&B=2&C=3"),
1675 &d);
[email protected]3dbb80b2010-02-09 22:41:201676 req.set_context(context);
1677 req.Start(); // Triggers an asynchronous cookie policy check.
1678
1679 MessageLoop::current()->Run();
1680
[email protected]f90bf0d92011-01-13 02:12:441681 EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status());
[email protected]3dbb80b2010-02-09 22:41:201682
1683 EXPECT_EQ(1, d.blocked_get_cookies_count());
1684 EXPECT_EQ(0, d.blocked_set_cookie_count());
1685 }
1686
1687 context->set_cookie_policy(NULL);
1688}
1689
[email protected]9fb83e82010-07-02 18:24:551690TEST_F(URLRequestTest, CancelTest_During_OnSetCookie) {
[email protected]95409e12010-08-17 20:07:111691 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1692 ASSERT_TRUE(test_server.Start());
1693
[email protected]ad8e04a2010-11-01 04:16:271694 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
[email protected]3dbb80b2010-02-09 22:41:201695
1696 TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE);
1697 context->set_cookie_policy(&cookie_policy);
1698
1699 // Set up a cookie.
1700 {
1701 TestDelegate d;
1702 d.set_cancel_in_set_cookie_blocked(true);
[email protected]6981d9632010-11-30 21:34:021703 net::URLRequest req(test_server.GetURL("set-cookie?A=1&B=2&C=3"),
1704 &d);
[email protected]3dbb80b2010-02-09 22:41:201705 req.set_context(context);
1706 req.Start(); // Triggers an asynchronous cookie policy check.
1707
1708 MessageLoop::current()->Run();
1709
[email protected]f90bf0d92011-01-13 02:12:441710 EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status());
[email protected]3dbb80b2010-02-09 22:41:201711
1712 // Even though the response will contain 3 set-cookie headers, we expect
[email protected]9fb83e82010-07-02 18:24:551713 // only one to be blocked as that first one will cause OnSetCookie to be
1714 // called, which will cancel the request. Once canceled, it should not
1715 // attempt to set further cookies.
[email protected]3dbb80b2010-02-09 22:41:201716
1717 EXPECT_EQ(0, d.blocked_get_cookies_count());
1718 EXPECT_EQ(1, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151719 }
1720
1721 context->set_cookie_policy(NULL);
1722}
1723
[email protected]4f79b3f2010-02-05 04:27:471724TEST_F(URLRequestTest, CookiePolicy_ForceSession) {
[email protected]95409e12010-08-17 20:07:111725 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath());
1726 ASSERT_TRUE(test_server.Start());
1727
[email protected]ad8e04a2010-11-01 04:16:271728 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
[email protected]4f79b3f2010-02-05 04:27:471729
1730 TestCookiePolicy cookie_policy(TestCookiePolicy::FORCE_SESSION);
1731 context->set_cookie_policy(&cookie_policy);
1732
1733 // Set up a cookie.
1734 {
1735 TestDelegate d;
[email protected]6981d9632010-11-30 21:34:021736 net::URLRequest req(test_server.GetURL(
[email protected]4f79b3f2010-02-05 04:27:471737 "set-cookie?A=1;expires=\"Fri, 05 Feb 2010 23:42:01 GMT\""), &d);
1738 req.set_context(context);
1739 req.Start(); // Triggers an asynchronous cookie policy check.
1740
1741 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201742
1743 EXPECT_EQ(0, d.blocked_get_cookies_count());
1744 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]4f79b3f2010-02-05 04:27:471745 }
1746
1747 // Now, check the cookie store.
[email protected]7d80c542010-12-03 15:30:361748 net::CookieList cookies =
[email protected]4f79b3f2010-02-05 04:27:471749 context->cookie_store()->GetCookieMonster()->GetAllCookies();
1750 EXPECT_EQ(1U, cookies.size());
[email protected]65781e92010-07-21 15:29:571751 EXPECT_FALSE(cookies[0].IsPersistent());
[email protected]4f79b3f2010-02-05 04:27:471752
1753 context->set_cookie_policy(NULL);
1754}
1755
[email protected]71c64f62008-11-15 04:36:511756// In this test, we do a POST which the server will 302 redirect.
1757// The subsequent transaction should use GET, and should not send the
1758// Content-Type header.
1759// http://code.google.com/p/chromium/issues/detail?id=843
[email protected]37314622009-08-17 20:29:391760TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
[email protected]95409e12010-08-17 20:07:111761 ASSERT_TRUE(test_server_.Start());
1762
[email protected]195e77d2009-07-23 19:10:231763 const char kData[] = "hello world";
[email protected]95409e12010-08-17 20:07:111764
[email protected]71c64f62008-11-15 04:36:511765 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111766 TestURLRequest req(test_server_.GetURL("files/redirect-to-echoall"), &d);
[email protected]71c64f62008-11-15 04:36:511767 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:231768 req.set_upload(CreateSimpleUploadData(kData));
[email protected]71c64f62008-11-15 04:36:511769
1770 // Set headers (some of which are specific to the POST).
[email protected]88e6b6f32010-05-07 23:14:251771 net::HttpRequestHeaders headers;
1772 headers.AddHeadersFromString(
[email protected]dd265012009-01-08 20:45:271773 "Content-Type: multipart/form-data; "
1774 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
1775 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
1776 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
[email protected]71c64f62008-11-15 04:36:511777 "Accept-Language: en-US,en\r\n"
1778 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
[email protected]195e77d2009-07-23 19:10:231779 "Content-Length: 11\r\n"
[email protected]a86c97cc2009-06-24 21:26:271780 "Origin: http://localhost:1337/");
[email protected]88e6b6f32010-05-07 23:14:251781 req.SetExtraRequestHeaders(headers);
[email protected]71c64f62008-11-15 04:36:511782 req.Start();
1783 MessageLoop::current()->Run();
1784
1785 std::string mime_type;
1786 req.GetMimeType(&mime_type);
1787 EXPECT_EQ("text/html", mime_type);
1788
1789 const std::string& data = d.data_received();
1790
1791 // Check that the post-specific headers were stripped:
1792 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
1793 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
1794 EXPECT_FALSE(ContainsString(data, "Origin:"));
1795
1796 // These extra request headers should not have been stripped.
1797 EXPECT_TRUE(ContainsString(data, "Accept:"));
1798 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
1799 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
1800}
1801
[email protected]37314622009-08-17 20:29:391802TEST_F(URLRequestTestHTTP, Post307RedirectPost) {
[email protected]95409e12010-08-17 20:07:111803 ASSERT_TRUE(test_server_.Start());
1804
[email protected]195e77d2009-07-23 19:10:231805 const char kData[] = "hello world";
[email protected]95409e12010-08-17 20:07:111806
[email protected]140932f2008-12-12 03:58:061807 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111808 TestURLRequest req(test_server_.GetURL("files/redirect307-to-echo"),
[email protected]dd265012009-01-08 20:45:271809 &d);
[email protected]140932f2008-12-12 03:58:061810 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:231811 req.set_upload(CreateSimpleUploadData(kData).get());
[email protected]88e6b6f32010-05-07 23:14:251812 net::HttpRequestHeaders headers;
1813 headers.SetHeader(net::HttpRequestHeaders::kContentLength,
[email protected]528c56d2010-07-30 19:28:441814 base::UintToString(arraysize(kData) - 1));
[email protected]88e6b6f32010-05-07 23:14:251815 req.SetExtraRequestHeaders(headers);
[email protected]140932f2008-12-12 03:58:061816 req.Start();
1817 MessageLoop::current()->Run();
[email protected]195e77d2009-07-23 19:10:231818 EXPECT_EQ("POST", req.method());
1819 EXPECT_EQ(kData, d.data_received());
[email protected]140932f2008-12-12 03:58:061820}
[email protected]dd265012009-01-08 20:45:271821
[email protected]a5c713f2009-04-16 21:05:471822// Custom URLRequestJobs for use with interceptor tests
[email protected]cbe04ef2011-01-11 00:13:241823class RestartTestJob : public net::URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:471824 public:
[email protected]6981d9632010-11-30 21:34:021825 explicit RestartTestJob(net::URLRequest* request)
[email protected]cbe04ef2011-01-11 00:13:241826 : net::URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:471827 protected:
1828 virtual void StartAsync() {
1829 this->NotifyRestartRequired();
1830 }
[email protected]5389bc72009-11-05 23:34:241831 private:
[email protected]13c8a092010-07-29 06:15:441832 ~RestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471833};
1834
[email protected]cbe04ef2011-01-11 00:13:241835class CancelTestJob : public net::URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:471836 public:
[email protected]6981d9632010-11-30 21:34:021837 explicit CancelTestJob(net::URLRequest* request)
[email protected]cbe04ef2011-01-11 00:13:241838 : net::URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:471839 protected:
1840 virtual void StartAsync() {
1841 request_->Cancel();
1842 }
[email protected]5389bc72009-11-05 23:34:241843 private:
1844 ~CancelTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471845};
1846
[email protected]cbe04ef2011-01-11 00:13:241847class CancelThenRestartTestJob : public net::URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:471848 public:
[email protected]6981d9632010-11-30 21:34:021849 explicit CancelThenRestartTestJob(net::URLRequest* request)
[email protected]cbe04ef2011-01-11 00:13:241850 : net::URLRequestTestJob(request, true) {
[email protected]a5c713f2009-04-16 21:05:471851 }
1852 protected:
1853 virtual void StartAsync() {
1854 request_->Cancel();
1855 this->NotifyRestartRequired();
1856 }
[email protected]5389bc72009-11-05 23:34:241857 private:
1858 ~CancelThenRestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:471859};
1860
1861// An Interceptor for use with interceptor tests
[email protected]6981d9632010-11-30 21:34:021862class TestInterceptor : net::URLRequest::Interceptor {
[email protected]a5c713f2009-04-16 21:05:471863 public:
1864 TestInterceptor()
1865 : intercept_main_request_(false), restart_main_request_(false),
1866 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1867 simulate_main_network_error_(false),
1868 intercept_redirect_(false), cancel_redirect_request_(false),
1869 intercept_final_response_(false), cancel_final_request_(false),
1870 did_intercept_main_(false), did_restart_main_(false),
1871 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1872 did_simulate_error_main_(false),
1873 did_intercept_redirect_(false), did_cancel_redirect_(false),
1874 did_intercept_final_(false), did_cancel_final_(false) {
[email protected]6981d9632010-11-30 21:34:021875 net::URLRequest::RegisterRequestInterceptor(this);
[email protected]a5c713f2009-04-16 21:05:471876 }
1877
1878 ~TestInterceptor() {
[email protected]6981d9632010-11-30 21:34:021879 net::URLRequest::UnregisterRequestInterceptor(this);
[email protected]a5c713f2009-04-16 21:05:471880 }
1881
[email protected]9acd869e2010-12-11 10:18:591882 virtual net::URLRequestJob* MaybeIntercept(net::URLRequest* request) {
[email protected]a5c713f2009-04-16 21:05:471883 if (restart_main_request_) {
1884 restart_main_request_ = false;
1885 did_restart_main_ = true;
1886 return new RestartTestJob(request);
1887 }
1888 if (cancel_main_request_) {
1889 cancel_main_request_ = false;
1890 did_cancel_main_ = true;
1891 return new CancelTestJob(request);
1892 }
1893 if (cancel_then_restart_main_request_) {
1894 cancel_then_restart_main_request_ = false;
1895 did_cancel_then_restart_main_ = true;
1896 return new CancelThenRestartTestJob(request);
1897 }
1898 if (simulate_main_network_error_) {
1899 simulate_main_network_error_ = false;
1900 did_simulate_error_main_ = true;
1901 // will error since the requeted url is not one of its canned urls
[email protected]cbe04ef2011-01-11 00:13:241902 return new net::URLRequestTestJob(request, true);
[email protected]a5c713f2009-04-16 21:05:471903 }
1904 if (!intercept_main_request_)
1905 return NULL;
1906 intercept_main_request_ = false;
1907 did_intercept_main_ = true;
[email protected]cbe04ef2011-01-11 00:13:241908 return new net::URLRequestTestJob(request,
1909 main_headers_,
1910 main_data_,
1911 true);
[email protected]a5c713f2009-04-16 21:05:471912 }
1913
[email protected]9acd869e2010-12-11 10:18:591914 virtual net::URLRequestJob* MaybeInterceptRedirect(net::URLRequest* request,
1915 const GURL& location) {
[email protected]a5c713f2009-04-16 21:05:471916 if (cancel_redirect_request_) {
1917 cancel_redirect_request_ = false;
1918 did_cancel_redirect_ = true;
1919 return new CancelTestJob(request);
1920 }
1921 if (!intercept_redirect_)
1922 return NULL;
1923 intercept_redirect_ = false;
1924 did_intercept_redirect_ = true;
[email protected]cbe04ef2011-01-11 00:13:241925 return new net::URLRequestTestJob(request,
1926 redirect_headers_,
1927 redirect_data_,
1928 true);
[email protected]a5c713f2009-04-16 21:05:471929 }
1930
[email protected]9acd869e2010-12-11 10:18:591931 virtual net::URLRequestJob* MaybeInterceptResponse(net::URLRequest* request) {
[email protected]a5c713f2009-04-16 21:05:471932 if (cancel_final_request_) {
1933 cancel_final_request_ = false;
1934 did_cancel_final_ = true;
1935 return new CancelTestJob(request);
1936 }
1937 if (!intercept_final_response_)
1938 return NULL;
1939 intercept_final_response_ = false;
1940 did_intercept_final_ = true;
[email protected]cbe04ef2011-01-11 00:13:241941 return new net::URLRequestTestJob(request,
1942 final_headers_,
1943 final_data_,
1944 true);
[email protected]a5c713f2009-04-16 21:05:471945 }
1946
1947 // Whether to intercept the main request, and if so the response to return.
1948 bool intercept_main_request_;
1949 std::string main_headers_;
1950 std::string main_data_;
1951
1952 // Other actions we take at MaybeIntercept time
1953 bool restart_main_request_;
1954 bool cancel_main_request_;
1955 bool cancel_then_restart_main_request_;
1956 bool simulate_main_network_error_;
1957
1958 // Whether to intercept redirects, and if so the response to return.
1959 bool intercept_redirect_;
1960 std::string redirect_headers_;
1961 std::string redirect_data_;
1962
1963 // Other actions we can take at MaybeInterceptRedirect time
1964 bool cancel_redirect_request_;
1965
1966 // Whether to intercept final response, and if so the response to return.
1967 bool intercept_final_response_;
1968 std::string final_headers_;
1969 std::string final_data_;
1970
1971 // Other actions we can take at MaybeInterceptResponse time
1972 bool cancel_final_request_;
1973
1974 // If we did something or not
1975 bool did_intercept_main_;
1976 bool did_restart_main_;
1977 bool did_cancel_main_;
1978 bool did_cancel_then_restart_main_;
1979 bool did_simulate_error_main_;
1980 bool did_intercept_redirect_;
1981 bool did_cancel_redirect_;
1982 bool did_intercept_final_;
1983 bool did_cancel_final_;
1984
1985 // Static getters for canned response header and data strings
1986
1987 static std::string ok_data() {
[email protected]cbe04ef2011-01-11 00:13:241988 return net::URLRequestTestJob::test_data_1();
[email protected]a5c713f2009-04-16 21:05:471989 }
1990
1991 static std::string ok_headers() {
[email protected]cbe04ef2011-01-11 00:13:241992 return net::URLRequestTestJob::test_headers();
[email protected]a5c713f2009-04-16 21:05:471993 }
1994
1995 static std::string redirect_data() {
1996 return std::string();
1997 }
1998
1999 static std::string redirect_headers() {
[email protected]cbe04ef2011-01-11 00:13:242000 return net::URLRequestTestJob::test_redirect_headers();
[email protected]a5c713f2009-04-16 21:05:472001 }
2002
2003 static std::string error_data() {
2004 return std::string("ohhh nooooo mr. bill!");
2005 }
2006
2007 static std::string error_headers() {
[email protected]cbe04ef2011-01-11 00:13:242008 return net::URLRequestTestJob::test_error_headers();
[email protected]a5c713f2009-04-16 21:05:472009 }
2010};
2011
2012TEST_F(URLRequestTest, Intercept) {
2013 TestInterceptor interceptor;
2014
2015 // intercept the main request and respond with a simple response
2016 interceptor.intercept_main_request_ = true;
2017 interceptor.main_headers_ = TestInterceptor::ok_headers();
2018 interceptor.main_data_ = TestInterceptor::ok_data();
2019
2020 TestDelegate d;
2021 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]6981d9632010-11-30 21:34:022022 net::URLRequest::UserData* user_data0 = new net::URLRequest::UserData();
2023 net::URLRequest::UserData* user_data1 = new net::URLRequest::UserData();
2024 net::URLRequest::UserData* user_data2 = new net::URLRequest::UserData();
[email protected]a5c713f2009-04-16 21:05:472025 req.SetUserData(NULL, user_data0);
2026 req.SetUserData(&user_data1, user_data1);
2027 req.SetUserData(&user_data2, user_data2);
2028 req.set_method("GET");
2029 req.Start();
2030 MessageLoop::current()->Run();
2031
2032 // Make sure we can retrieve our specific user data
2033 EXPECT_EQ(user_data0, req.GetUserData(NULL));
2034 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
2035 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
2036
2037 // Check the interceptor got called as expected
2038 EXPECT_TRUE(interceptor.did_intercept_main_);
2039
2040 // Check we got one good response
2041 EXPECT_TRUE(req.status().is_success());
2042 EXPECT_EQ(200, req.response_headers()->response_code());
2043 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2044 EXPECT_EQ(1, d.response_started_count());
2045 EXPECT_EQ(0, d.received_redirect_count());
2046}
2047
2048TEST_F(URLRequestTest, InterceptRedirect) {
2049 TestInterceptor interceptor;
2050
2051 // intercept the main request and respond with a redirect
2052 interceptor.intercept_main_request_ = true;
2053 interceptor.main_headers_ = TestInterceptor::redirect_headers();
2054 interceptor.main_data_ = TestInterceptor::redirect_data();
2055
2056 // intercept that redirect and respond a final OK response
2057 interceptor.intercept_redirect_ = true;
2058 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
2059 interceptor.redirect_data_ = TestInterceptor::ok_data();
2060
2061 TestDelegate d;
2062 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2063 req.set_method("GET");
2064 req.Start();
2065 MessageLoop::current()->Run();
2066
2067 // Check the interceptor got called as expected
2068 EXPECT_TRUE(interceptor.did_intercept_main_);
2069 EXPECT_TRUE(interceptor.did_intercept_redirect_);
2070
2071 // Check we got one good response
2072 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:112073 if (req.status().is_success()) {
2074 EXPECT_EQ(200, req.response_headers()->response_code());
2075 }
[email protected]a5c713f2009-04-16 21:05:472076 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2077 EXPECT_EQ(1, d.response_started_count());
2078 EXPECT_EQ(0, d.received_redirect_count());
2079}
2080
2081TEST_F(URLRequestTest, InterceptServerError) {
2082 TestInterceptor interceptor;
2083
2084 // intercept the main request to generate a server error response
2085 interceptor.intercept_main_request_ = true;
2086 interceptor.main_headers_ = TestInterceptor::error_headers();
2087 interceptor.main_data_ = TestInterceptor::error_data();
2088
2089 // intercept that error and respond with an OK response
2090 interceptor.intercept_final_response_ = true;
2091 interceptor.final_headers_ = TestInterceptor::ok_headers();
2092 interceptor.final_data_ = TestInterceptor::ok_data();
2093
2094 TestDelegate d;
2095 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2096 req.set_method("GET");
2097 req.Start();
2098 MessageLoop::current()->Run();
2099
2100 // Check the interceptor got called as expected
2101 EXPECT_TRUE(interceptor.did_intercept_main_);
2102 EXPECT_TRUE(interceptor.did_intercept_final_);
2103
2104 // Check we got one good response
2105 EXPECT_TRUE(req.status().is_success());
2106 EXPECT_EQ(200, req.response_headers()->response_code());
2107 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2108 EXPECT_EQ(1, d.response_started_count());
2109 EXPECT_EQ(0, d.received_redirect_count());
2110}
2111
2112TEST_F(URLRequestTest, InterceptNetworkError) {
2113 TestInterceptor interceptor;
2114
2115 // intercept the main request to simulate a network error
2116 interceptor.simulate_main_network_error_ = true;
2117
2118 // intercept that error and respond with an OK response
2119 interceptor.intercept_final_response_ = true;
2120 interceptor.final_headers_ = TestInterceptor::ok_headers();
2121 interceptor.final_data_ = TestInterceptor::ok_data();
2122
2123 TestDelegate d;
2124 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2125 req.set_method("GET");
2126 req.Start();
2127 MessageLoop::current()->Run();
2128
2129 // Check the interceptor got called as expected
2130 EXPECT_TRUE(interceptor.did_simulate_error_main_);
2131 EXPECT_TRUE(interceptor.did_intercept_final_);
2132
2133 // Check we received one good response
2134 EXPECT_TRUE(req.status().is_success());
2135 EXPECT_EQ(200, req.response_headers()->response_code());
2136 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2137 EXPECT_EQ(1, d.response_started_count());
2138 EXPECT_EQ(0, d.received_redirect_count());
2139}
2140
2141TEST_F(URLRequestTest, InterceptRestartRequired) {
2142 TestInterceptor interceptor;
2143
2144 // restart the main request
2145 interceptor.restart_main_request_ = true;
2146
2147 // then intercept the new main request and respond with an OK response
2148 interceptor.intercept_main_request_ = true;
2149 interceptor.main_headers_ = TestInterceptor::ok_headers();
2150 interceptor.main_data_ = TestInterceptor::ok_data();
2151
2152 TestDelegate d;
2153 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2154 req.set_method("GET");
2155 req.Start();
2156 MessageLoop::current()->Run();
2157
2158 // Check the interceptor got called as expected
2159 EXPECT_TRUE(interceptor.did_restart_main_);
2160 EXPECT_TRUE(interceptor.did_intercept_main_);
2161
2162 // Check we received one good response
2163 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:112164 if (req.status().is_success()) {
2165 EXPECT_EQ(200, req.response_headers()->response_code());
2166 }
[email protected]a5c713f2009-04-16 21:05:472167 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2168 EXPECT_EQ(1, d.response_started_count());
2169 EXPECT_EQ(0, d.received_redirect_count());
2170}
2171
2172TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
2173 TestInterceptor interceptor;
2174
2175 // intercept the main request and cancel from within the restarted job
2176 interceptor.cancel_main_request_ = true;
2177
2178 // setup to intercept final response and override it with an OK response
2179 interceptor.intercept_final_response_ = true;
2180 interceptor.final_headers_ = TestInterceptor::ok_headers();
2181 interceptor.final_data_ = TestInterceptor::ok_data();
2182
2183 TestDelegate d;
2184 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2185 req.set_method("GET");
2186 req.Start();
2187 MessageLoop::current()->Run();
2188
2189 // Check the interceptor got called as expected
2190 EXPECT_TRUE(interceptor.did_cancel_main_);
2191 EXPECT_FALSE(interceptor.did_intercept_final_);
2192
2193 // Check we see a canceled request
2194 EXPECT_FALSE(req.status().is_success());
[email protected]f90bf0d92011-01-13 02:12:442195 EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:472196}
2197
2198TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
2199 TestInterceptor interceptor;
2200
2201 // intercept the main request and respond with a redirect
2202 interceptor.intercept_main_request_ = true;
2203 interceptor.main_headers_ = TestInterceptor::redirect_headers();
2204 interceptor.main_data_ = TestInterceptor::redirect_data();
2205
2206 // intercept the redirect and cancel from within that job
2207 interceptor.cancel_redirect_request_ = true;
2208
2209 // setup to intercept final response and override it with an OK response
2210 interceptor.intercept_final_response_ = true;
2211 interceptor.final_headers_ = TestInterceptor::ok_headers();
2212 interceptor.final_data_ = TestInterceptor::ok_data();
2213
2214 TestDelegate d;
2215 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2216 req.set_method("GET");
2217 req.Start();
2218 MessageLoop::current()->Run();
2219
2220 // Check the interceptor got called as expected
2221 EXPECT_TRUE(interceptor.did_intercept_main_);
2222 EXPECT_TRUE(interceptor.did_cancel_redirect_);
2223 EXPECT_FALSE(interceptor.did_intercept_final_);
2224
2225 // Check we see a canceled request
2226 EXPECT_FALSE(req.status().is_success());
[email protected]f90bf0d92011-01-13 02:12:442227 EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:472228}
2229
2230TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
2231 TestInterceptor interceptor;
2232
2233 // intercept the main request to simulate a network error
2234 interceptor.simulate_main_network_error_ = true;
2235
2236 // setup to intercept final response and cancel from within that job
2237 interceptor.cancel_final_request_ = true;
2238
2239 TestDelegate d;
2240 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2241 req.set_method("GET");
2242 req.Start();
2243 MessageLoop::current()->Run();
2244
2245 // Check the interceptor got called as expected
2246 EXPECT_TRUE(interceptor.did_simulate_error_main_);
2247 EXPECT_TRUE(interceptor.did_cancel_final_);
2248
2249 // Check we see a canceled request
2250 EXPECT_FALSE(req.status().is_success());
[email protected]f90bf0d92011-01-13 02:12:442251 EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:472252}
2253
2254TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
2255 TestInterceptor interceptor;
2256
2257 // intercept the main request and cancel then restart from within that job
2258 interceptor.cancel_then_restart_main_request_ = true;
2259
2260 // setup to intercept final response and override it with an OK response
2261 interceptor.intercept_final_response_ = true;
2262 interceptor.final_headers_ = TestInterceptor::ok_headers();
2263 interceptor.final_data_ = TestInterceptor::ok_data();
2264
2265 TestDelegate d;
2266 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
2267 req.set_method("GET");
2268 req.Start();
2269 MessageLoop::current()->Run();
2270
2271 // Check the interceptor got called as expected
2272 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
2273 EXPECT_FALSE(interceptor.did_intercept_final_);
2274
2275 // Check we see a canceled request
2276 EXPECT_FALSE(req.status().is_success());
[email protected]f90bf0d92011-01-13 02:12:442277 EXPECT_EQ(net::URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:472278}
2279
[email protected]b89ca032009-08-31 21:41:312280class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:112281 public:
2282 URLRequestTestFTP() : test_server_(net::TestServer::TYPE_FTP, FilePath()) {
2283 }
2284
[email protected]b89ca032009-08-31 21:41:312285 protected:
[email protected]95409e12010-08-17 20:07:112286 net::TestServer test_server_;
[email protected]b89ca032009-08-31 21:41:312287};
2288
[email protected]5accf7332009-11-24 03:41:382289// Flaky, see http://crbug.com/25045.
2290TEST_F(URLRequestTestFTP, FLAKY_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:112291 ASSERT_TRUE(test_server_.Start());
2292
[email protected]a25e90e2009-09-09 17:05:372293 TestDelegate d;
2294 {
[email protected]95409e12010-08-17 20:07:112295 TestURLRequest r(test_server_.GetURL("/"), &d);
[email protected]a25e90e2009-09-09 17:05:372296 r.Start();
2297 EXPECT_TRUE(r.is_pending());
2298
2299 MessageLoop::current()->Run();
2300
2301 EXPECT_FALSE(r.is_pending());
2302 EXPECT_EQ(1, d.response_started_count());
2303 EXPECT_FALSE(d.received_data_before_response());
2304 EXPECT_LT(0, d.bytes_received());
2305 }
2306}
2307
[email protected]7df70012010-02-04 00:09:552308// Flaky, see http://crbug.com/25045.
2309TEST_F(URLRequestTestFTP, FLAKY_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:112310 ASSERT_TRUE(test_server_.Start());
2311
[email protected]399b8702009-05-01 20:34:022312 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272313 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022314 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272315 TestDelegate d;
2316 {
[email protected]95409e12010-08-17 20:07:112317 TestURLRequest r(test_server_.GetURL("/LICENSE"), &d);
[email protected]dd265012009-01-08 20:45:272318 r.Start();
2319 EXPECT_TRUE(r.is_pending());
2320
2321 MessageLoop::current()->Run();
2322
2323 int64 file_size = 0;
2324 file_util::GetFileSize(app_path, &file_size);
2325
[email protected]ba2f3342009-07-30 18:08:422326 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272327 EXPECT_EQ(1, d.response_started_count());
2328 EXPECT_FALSE(d.received_data_before_response());
2329 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2330 }
2331}
2332
[email protected]e9ecbd12009-12-20 18:44:402333// Flaky, see http://crbug.com/25045.
2334TEST_F(URLRequestTestFTP, FLAKY_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:112335 ASSERT_TRUE(test_server_.Start());
2336
[email protected]399b8702009-05-01 20:34:022337 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272338 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022339 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272340 TestDelegate d;
2341 {
[email protected]95409e12010-08-17 20:07:112342 TestURLRequest r(
2343 test_server_.GetURLWithUserAndPassword("/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]95409e12010-08-17 20:07:112362 ASSERT_TRUE(test_server_.Start());
2363
[email protected]399b8702009-05-01 20:34:022364 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272365 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022366 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272367 TestDelegate d;
2368 {
[email protected]95409e12010-08-17 20:07:112369 TestURLRequest r(
2370 test_server_.GetURLWithUserAndPassword("/LICENSE",
2371 "chrome",
2372 "wrong_password"),
2373 &d);
[email protected]dd265012009-01-08 20:45:272374 r.Start();
2375 EXPECT_TRUE(r.is_pending());
2376
2377 MessageLoop::current()->Run();
2378
2379 int64 file_size = 0;
2380 file_util::GetFileSize(app_path, &file_size);
2381
[email protected]ba2f3342009-07-30 18:08:422382 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272383 EXPECT_EQ(1, d.response_started_count());
2384 EXPECT_FALSE(d.received_data_before_response());
2385 EXPECT_EQ(d.bytes_received(), 0);
2386 }
2387}
2388
[email protected]cde4e80d2009-10-16 19:58:152389// Flaky, see http://crbug.com/25045.
2390TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:112391 ASSERT_TRUE(test_server_.Start());
2392
[email protected]8b8a197d2009-08-26 15:57:582393 FilePath app_path;
2394 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2395 app_path = app_path.AppendASCII("LICENSE");
2396 TestDelegate d;
2397 // Set correct login credentials. The delegate will be asked for them when
2398 // the initial login with wrong credentials will fail.
[email protected]13c8a092010-07-29 06:15:442399 d.set_username(kChrome);
2400 d.set_password(kChrome);
[email protected]8b8a197d2009-08-26 15:57:582401 {
[email protected]95409e12010-08-17 20:07:112402 TestURLRequest r(
2403 test_server_.GetURLWithUserAndPassword("/LICENSE",
2404 "chrome",
2405 "wrong_password"),
2406 &d);
[email protected]8b8a197d2009-08-26 15:57:582407 r.Start();
2408 EXPECT_TRUE(r.is_pending());
2409
2410 MessageLoop::current()->Run();
2411
2412 int64 file_size = 0;
2413 file_util::GetFileSize(app_path, &file_size);
2414
2415 EXPECT_FALSE(r.is_pending());
2416 EXPECT_EQ(1, d.response_started_count());
2417 EXPECT_FALSE(d.received_data_before_response());
2418 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2419 }
2420}
2421
[email protected]49abd652010-08-05 05:04:532422// Flaky, see http://crbug.com/25045.
2423TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:112424 ASSERT_TRUE(test_server_.Start());
2425
[email protected]399b8702009-05-01 20:34:022426 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272427 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022428 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272429 TestDelegate d;
2430 {
[email protected]95409e12010-08-17 20:07:112431 TestURLRequest r(
2432 test_server_.GetURLWithUserAndPassword("/LICENSE",
2433 "wrong_user",
2434 "chrome"),
2435 &d);
[email protected]dd265012009-01-08 20:45:272436 r.Start();
2437 EXPECT_TRUE(r.is_pending());
2438
2439 MessageLoop::current()->Run();
2440
2441 int64 file_size = 0;
2442 file_util::GetFileSize(app_path, &file_size);
2443
[email protected]ba2f3342009-07-30 18:08:422444 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272445 EXPECT_EQ(1, d.response_started_count());
2446 EXPECT_FALSE(d.received_data_before_response());
2447 EXPECT_EQ(d.bytes_received(), 0);
2448 }
2449}
[email protected]8b8a197d2009-08-26 15:57:582450
[email protected]cde4e80d2009-10-16 19:58:152451// Flaky, see http://crbug.com/25045.
2452TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:112453 ASSERT_TRUE(test_server_.Start());
2454
[email protected]8b8a197d2009-08-26 15:57:582455 FilePath app_path;
2456 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2457 app_path = app_path.AppendASCII("LICENSE");
2458 TestDelegate d;
2459 // Set correct login credentials. The delegate will be asked for them when
2460 // the initial login with wrong credentials will fail.
[email protected]13c8a092010-07-29 06:15:442461 d.set_username(kChrome);
2462 d.set_password(kChrome);
[email protected]8b8a197d2009-08-26 15:57:582463 {
[email protected]95409e12010-08-17 20:07:112464 TestURLRequest r(
2465 test_server_.GetURLWithUserAndPassword("/LICENSE",
2466 "wrong_user",
2467 "chrome"),
2468 &d);
[email protected]8b8a197d2009-08-26 15:57:582469 r.Start();
2470 EXPECT_TRUE(r.is_pending());
2471
2472 MessageLoop::current()->Run();
2473
2474 int64 file_size = 0;
2475 file_util::GetFileSize(app_path, &file_size);
2476
2477 EXPECT_FALSE(r.is_pending());
2478 EXPECT_EQ(1, d.response_started_count());
2479 EXPECT_FALSE(d.received_data_before_response());
2480 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2481 }
2482}
[email protected]60a3df52009-09-22 16:13:242483
[email protected]cde4e80d2009-10-16 19:58:152484// Flaky, see http://crbug.com/25045.
2485TEST_F(URLRequestTestFTP, FLAKY_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:112486 ASSERT_TRUE(test_server_.Start());
2487
[email protected]60a3df52009-09-22 16:13:242488 FilePath app_path;
2489 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2490 app_path = app_path.AppendASCII("LICENSE");
2491
2492 scoped_ptr<TestDelegate> d(new TestDelegate);
2493 {
2494 // Pass correct login identity in the URL.
[email protected]95409e12010-08-17 20:07:112495 TestURLRequest r(
2496 test_server_.GetURLWithUserAndPassword("/LICENSE",
2497 "chrome",
2498 "chrome"),
2499 d.get());
[email protected]60a3df52009-09-22 16:13:242500 r.Start();
2501 EXPECT_TRUE(r.is_pending());
2502
2503 MessageLoop::current()->Run();
2504
2505 int64 file_size = 0;
2506 file_util::GetFileSize(app_path, &file_size);
2507
2508 EXPECT_FALSE(r.is_pending());
2509 EXPECT_EQ(1, d->response_started_count());
2510 EXPECT_FALSE(d->received_data_before_response());
2511 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2512 }
2513
2514 d.reset(new TestDelegate);
2515 {
2516 // This request should use cached identity from previous request.
[email protected]95409e12010-08-17 20:07:112517 TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get());
[email protected]60a3df52009-09-22 16:13:242518 r.Start();
2519 EXPECT_TRUE(r.is_pending());
2520
2521 MessageLoop::current()->Run();
2522
2523 int64 file_size = 0;
2524 file_util::GetFileSize(app_path, &file_size);
2525
2526 EXPECT_FALSE(r.is_pending());
2527 EXPECT_EQ(1, d->response_started_count());
2528 EXPECT_FALSE(d->received_data_before_response());
2529 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2530 }
2531}
2532
[email protected]cde4e80d2009-10-16 19:58:152533// Flaky, see http://crbug.com/25045.
2534TEST_F(URLRequestTestFTP, FLAKY_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:112535 ASSERT_TRUE(test_server_.Start());
2536
[email protected]60a3df52009-09-22 16:13:242537 FilePath app_path;
2538 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2539 app_path = app_path.AppendASCII("LICENSE");
2540
2541 scoped_ptr<TestDelegate> d(new TestDelegate);
2542 // Set correct login credentials. The delegate will be asked for them when
2543 // the initial login with wrong credentials will fail.
[email protected]13c8a092010-07-29 06:15:442544 d->set_username(kChrome);
2545 d->set_password(kChrome);
[email protected]60a3df52009-09-22 16:13:242546 {
[email protected]95409e12010-08-17 20:07:112547 TestURLRequest r(
2548 test_server_.GetURLWithUserAndPassword("/LICENSE",
2549 "chrome",
2550 "wrong_password"),
2551 d.get());
[email protected]60a3df52009-09-22 16:13:242552 r.Start();
2553 EXPECT_TRUE(r.is_pending());
2554
2555 MessageLoop::current()->Run();
2556
2557 int64 file_size = 0;
2558 file_util::GetFileSize(app_path, &file_size);
2559
2560 EXPECT_FALSE(r.is_pending());
2561 EXPECT_EQ(1, d->response_started_count());
2562 EXPECT_FALSE(d->received_data_before_response());
2563 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2564 }
2565
2566 // Use a new delegate without explicit credentials. The cached ones should be
2567 // used.
2568 d.reset(new TestDelegate);
2569 {
2570 // Don't pass wrong credentials in the URL, they would override valid cached
2571 // ones.
[email protected]95409e12010-08-17 20:07:112572 TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get());
[email protected]60a3df52009-09-22 16:13:242573 r.Start();
2574 EXPECT_TRUE(r.is_pending());
2575
2576 MessageLoop::current()->Run();
2577
2578 int64 file_size = 0;
2579 file_util::GetFileSize(app_path, &file_size);
2580
2581 EXPECT_FALSE(r.is_pending());
2582 EXPECT_EQ(1, d->response_started_count());
2583 EXPECT_FALSE(d->received_data_before_response());
2584 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2585 }
2586}
[email protected]09a362d32009-09-24 18:01:332587
2588// Check that default A-L header is sent.
2589TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
[email protected]95409e12010-08-17 20:07:112590 ASSERT_TRUE(test_server_.Start());
2591
[email protected]09a362d32009-09-24 18:01:332592 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112593 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
[email protected]f72a1cc2010-04-30 07:17:302594 req.set_context(new TestURLRequestContext());
[email protected]09a362d32009-09-24 18:01:332595 req.Start();
2596 MessageLoop::current()->Run();
2597 EXPECT_EQ(req.context()->accept_language(), d.data_received());
2598}
2599
2600// Check that if request overrides the A-L header, the default is not appended.
2601// See http://crbug.com/20894
2602TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
[email protected]95409e12010-08-17 20:07:112603 ASSERT_TRUE(test_server_.Start());
2604
[email protected]09a362d32009-09-24 18:01:332605 TestDelegate d;
[email protected]eb834fb2009-10-23 16:09:412606 TestURLRequest
[email protected]95409e12010-08-17 20:07:112607 req(test_server_.GetURL("echoheaderoverride?Accept-Language"), &d);
[email protected]f72a1cc2010-04-30 07:17:302608 req.set_context(new TestURLRequestContext());
[email protected]88e6b6f32010-05-07 23:14:252609 net::HttpRequestHeaders headers;
2610 headers.SetHeader(net::HttpRequestHeaders::kAcceptLanguage, "ru");
2611 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:332612 req.Start();
2613 MessageLoop::current()->Run();
2614 EXPECT_EQ(std::string("ru"), d.data_received());
2615}
2616
2617// Check that default A-C header is sent.
2618TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) {
[email protected]95409e12010-08-17 20:07:112619 ASSERT_TRUE(test_server_.Start());
2620
[email protected]09a362d32009-09-24 18:01:332621 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112622 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
[email protected]f72a1cc2010-04-30 07:17:302623 req.set_context(new TestURLRequestContext());
[email protected]09a362d32009-09-24 18:01:332624 req.Start();
2625 MessageLoop::current()->Run();
2626 EXPECT_EQ(req.context()->accept_charset(), d.data_received());
2627}
2628
2629// Check that if request overrides the A-C header, the default is not appended.
2630// See http://crbug.com/20894
2631TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) {
[email protected]95409e12010-08-17 20:07:112632 ASSERT_TRUE(test_server_.Start());
2633
[email protected]09a362d32009-09-24 18:01:332634 TestDelegate d;
[email protected]eb834fb2009-10-23 16:09:412635 TestURLRequest
[email protected]95409e12010-08-17 20:07:112636 req(test_server_.GetURL("echoheaderoverride?Accept-Charset"), &d);
[email protected]f72a1cc2010-04-30 07:17:302637 req.set_context(new TestURLRequestContext());
[email protected]88e6b6f32010-05-07 23:14:252638 net::HttpRequestHeaders headers;
2639 headers.SetHeader(net::HttpRequestHeaders::kAcceptCharset, "koi-8r");
2640 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:332641 req.Start();
2642 MessageLoop::current()->Run();
2643 EXPECT_EQ(std::string("koi-8r"), d.data_received());
2644}
[email protected]50ba404b2011-01-21 13:38:452645
2646// Check that default User-Agent header is sent.
2647TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
2648 ASSERT_TRUE(test_server_.Start());
2649
2650 TestDelegate d;
2651 TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d);
2652 req.set_context(new TestURLRequestContext());
2653 req.Start();
2654 MessageLoop::current()->Run();
2655 EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received());
2656}
2657
2658// Check that if request overrides the User-Agent header,
2659// the default is not appended.
2660TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
2661 ASSERT_TRUE(test_server_.Start());
2662
2663 TestDelegate d;
2664 TestURLRequest
2665 req(test_server_.GetURL("echoheaderoverride?User-Agent"), &d);
2666 req.set_context(new TestURLRequestContext());
2667 net::HttpRequestHeaders headers;
2668 headers.SetHeader(net::HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
2669 req.SetExtraRequestHeaders(headers);
2670 req.Start();
2671 MessageLoop::current()->Run();
2672 EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received());
2673}