blob: f6721ba86c266d58707a73ad636ba7e2314ca505 [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]ea224582008-12-07 20:25:465#include "build/build_config.h"
6
[email protected]9396b252008-09-29 17:29:387#if defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:528#include <shlobj.h>
[email protected]d8eb84242010-09-25 02:25:069#include <windows.h>
[email protected]9396b252008-09-29 17:29:3810#endif
11
initial.commit586acc5fe2008-07-26 22:42:5212#include <algorithm>
13#include <string>
14
[email protected]aad63572011-05-24 20:14:3915#include "base/basictypes.h"
[email protected]8523ba52011-05-22 19:00:5816#include "base/compiler_specific.h"
[email protected]399b8702009-05-01 20:34:0217#include "base/file_util.h"
[email protected]34b2b002009-11-20 06:53:2818#include "base/format_macros.h"
initial.commit586acc5fe2008-07-26 22:42:5219#include "base/message_loop.h"
20#include "base/path_service.h"
21#include "base/process_util.h"
[email protected]528c56d2010-07-30 19:28:4422#include "base/string_number_conversions.h"
[email protected]d8eb84242010-09-25 02:25:0623#include "base/string_piece.h"
[email protected]ebbad602011-01-21 23:30:4724#include "base/string_util.h"
[email protected]d8eb84242010-09-25 02:25:0625#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2226#include "base/utf_string_conversions.h"
[email protected]0757e7702009-03-27 04:00:2227#include "net/base/cookie_monster.h"
[email protected]54f4c9362011-07-25 21:54:4628#include "net/base/cookie_store_test_helpers.h"
initial.commit586acc5fe2008-07-26 22:42:5229#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1430#include "net/base/mock_host_resolver.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"
[email protected]d2db0292011-01-26 20:23:4450#include "net/url_request/url_request_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5251#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1552#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5253
[email protected]e1acf6f2008-10-27 20:43:3354using base::Time;
55
[email protected]7461a402011-03-24 23:19:5156namespace net {
57
initial.commit586acc5fe2008-07-26 22:42:5258namespace {
59
[email protected]13c8a092010-07-29 06:15:4460const string16 kChrome(ASCIIToUTF16("chrome"));
61const string16 kSecret(ASCIIToUTF16("secret"));
62const string16 kUser(ASCIIToUTF16("user"));
63
[email protected]8a16266e2009-09-10 21:08:3964base::StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:5265 return "header";
66}
67
[email protected]71c64f62008-11-15 04:36:5168// Do a case-insensitive search through |haystack| for |needle|.
69bool ContainsString(const std::string& haystack, const char* needle) {
70 std::string::const_iterator it =
71 std::search(haystack.begin(),
72 haystack.end(),
73 needle,
74 needle + strlen(needle),
[email protected]07f1cee2010-11-03 03:53:3575 base::CaseInsensitiveCompare<char>());
[email protected]71c64f62008-11-15 04:36:5176 return it != haystack.end();
77}
78
[email protected]661376a2009-04-29 02:04:2379void FillBuffer(char* buffer, size_t len) {
80 static bool called = false;
81 if (!called) {
82 called = true;
83 int seed = static_cast<int>(Time::Now().ToInternalValue());
84 srand(seed);
85 }
86
87 for (size_t i = 0; i < len; i++) {
88 buffer[i] = static_cast<char>(rand());
89 if (!buffer[i])
90 buffer[i] = 'g';
91 }
92}
93
[email protected]7461a402011-03-24 23:19:5194scoped_refptr<UploadData> CreateSimpleUploadData(const char* data) {
95 scoped_refptr<UploadData> upload(new UploadData);
[email protected]195e77d2009-07-23 19:10:2396 upload->AppendBytes(data, strlen(data));
97 return upload;
98}
99
[email protected]96adadb2010-08-28 01:16:17100// Verify that the SSLInfo of a successful SSL connection has valid values.
[email protected]7461a402011-03-24 23:19:51101void CheckSSLInfo(const SSLInfo& ssl_info) {
[email protected]89af3d92010-10-15 20:31:37102 // Allow ChromeFrame fake SSLInfo to get through.
[email protected]c679be22010-08-29 15:20:05103 if (ssl_info.cert.get() &&
[email protected]89af3d92010-10-15 20:31:37104 ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") {
105 // -1 means unknown.
106 EXPECT_EQ(ssl_info.security_bits, -1);
[email protected]c679be22010-08-29 15:20:05107 return;
[email protected]89af3d92010-10-15 20:31:37108 }
109
[email protected]96adadb2010-08-28 01:16:17110 // -1 means unknown. 0 means no encryption.
111 EXPECT_GT(ssl_info.security_bits, 0);
112
113 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
[email protected]7461a402011-03-24 23:19:51114 int cipher_suite = SSLConnectionStatusToCipherSuite(
[email protected]96adadb2010-08-28 01:16:17115 ssl_info.connection_status);
116 EXPECT_NE(0, cipher_suite);
117}
118
[email protected]9396b252008-09-29 17:29:38119} // namespace
initial.commit586acc5fe2008-07-26 22:42:52120
[email protected]4c76d7c2011-04-15 19:14:12121// A network delegate that blocks requests, optionally cancelling or redirecting
122// them.
123class BlockingNetworkDelegate : public TestNetworkDelegate {
124 public:
[email protected]8523ba52011-05-22 19:00:58125 BlockingNetworkDelegate()
126 : callback_retval_(net::OK),
127 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {}
[email protected]4c76d7c2011-04-15 19:14:12128
129 void set_callback_retval(int retval) { callback_retval_ = retval; }
130 void set_redirect_url(const GURL& url) { redirect_url_ = url; }
131
132 private:
133 // TestNetworkDelegate:
134 virtual int OnBeforeURLRequest(net::URLRequest* request,
135 net::CompletionCallback* callback,
136 GURL* new_url) {
[email protected]bb445ce2011-05-03 18:23:57137 if (redirect_url_ == request->url()) {
138 // We've already seen this request and redirected elsewhere.
139 return net::OK;
140 }
141
[email protected]4c76d7c2011-04-15 19:14:12142 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
143
144 if (!redirect_url_.is_empty())
145 *new_url = redirect_url_;
[email protected]8523ba52011-05-22 19:00:58146 MessageLoop::current()->PostTask(
147 FROM_HERE,
148 method_factory_.NewRunnableMethod(&BlockingNetworkDelegate::DoCallback,
149 callback));
[email protected]4c76d7c2011-04-15 19:14:12150 return net::ERR_IO_PENDING;
151 }
152
153 void DoCallback(net::CompletionCallback* callback) {
154 callback->Run(callback_retval_);
155 }
156
157 int callback_retval_;
158 GURL redirect_url_;
[email protected]8523ba52011-05-22 19:00:58159 ScopedRunnableMethodFactory<BlockingNetworkDelegate> method_factory_;
[email protected]4c76d7c2011-04-15 19:14:12160};
161
[email protected]7a0bb4bf2008-11-19 21:41:48162// Inherit PlatformTest since we require the autorelease pool on Mac OS X.f
163class URLRequestTest : public PlatformTest {
[email protected]abb26092010-11-11 22:19:00164 public:
[email protected]87a09a92011-07-14 15:50:50165 URLRequestTest() : default_context_(new TestURLRequestContext(true)) {
166 default_context_->set_network_delegate(&default_network_delegate_);
167 default_context_->Init();
168 }
169
[email protected]abb26092010-11-11 22:19:00170 static void SetUpTestCase() {
[email protected]7461a402011-03-24 23:19:51171 URLRequest::AllowFileAccess();
[email protected]abb26092010-11-11 22:19:00172 }
[email protected]87a09a92011-07-14 15:50:50173
174 protected:
175 TestNetworkDelegate default_network_delegate_; // must outlive URLRequest
176 scoped_refptr<TestURLRequestContext> default_context_;
[email protected]7a0bb4bf2008-11-19 21:41:48177};
178
[email protected]b89290212009-08-14 22:37:35179class URLRequestTestHTTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:11180 public:
181 URLRequestTestHTTP()
[email protected]7461a402011-03-24 23:19:51182 : test_server_(TestServer::TYPE_HTTP,
[email protected]95409e12010-08-17 20:07:11183 FilePath(FILE_PATH_LITERAL(
184 "net/data/url_request_unittest"))) {
185 }
186
[email protected]b89290212009-08-14 22:37:35187 protected:
[email protected]762d2db2010-01-11 19:03:01188 void HTTPUploadDataOperationTest(const std::string& method) {
[email protected]762d2db2010-01-11 19:03:01189 const int kMsgSize = 20000; // multiple of 10
190 const int kIterations = 50;
191 char *uploadBytes = new char[kMsgSize+1];
192 char *ptr = uploadBytes;
193 char marker = 'a';
194 for (int idx = 0; idx < kMsgSize/10; idx++) {
195 memcpy(ptr, "----------", 10);
196 ptr += 10;
197 if (idx % 100 == 0) {
198 ptr--;
199 *ptr++ = marker;
200 if (++marker > 'z')
201 marker = 'a';
202 }
203 }
204 uploadBytes[kMsgSize] = '\0';
205
[email protected]762d2db2010-01-11 19:03:01206 for (int i = 0; i < kIterations; ++i) {
207 TestDelegate d;
[email protected]7461a402011-03-24 23:19:51208 URLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:50209 r.set_context(default_context_);
[email protected]762d2db2010-01-11 19:03:01210 r.set_method(method.c_str());
211
212 r.AppendBytesToUpload(uploadBytes, kMsgSize);
213
214 r.Start();
215 EXPECT_TRUE(r.is_pending());
216
217 MessageLoop::current()->Run();
218
219 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
220 (int) r.status().status() << ", os error: " << r.status().os_error();
221
222 EXPECT_FALSE(d.received_data_before_response());
223 EXPECT_EQ(uploadBytes, d.data_received());
224 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0);
225 EXPECT_EQ(d.data_received().compare(uploadBytes), 0);
226 }
227 delete[] uploadBytes;
228 }
229
[email protected]699efe602011-01-25 07:17:11230 void AddChunksToUpload(TestURLRequest* r) {
[email protected]0c9bf872011-03-04 17:53:22231 r->AppendChunkToUpload("a", 1, false);
232 r->AppendChunkToUpload("bcd", 3, false);
233 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
234 r->AppendChunkToUpload("\r\n\r\n", 4, false);
235 r->AppendChunkToUpload("0", 1, false);
236 r->AppendChunkToUpload("2323", 4, true);
[email protected]699efe602011-01-25 07:17:11237 }
238
239 void VerifyReceivedDataMatchesChunks(TestURLRequest* r, TestDelegate* d) {
240 // This should match the chunks sent by AddChunksToUpload().
241 const char* expected_data =
242 "abcdthis is a longer chunk than before.\r\n\r\n02323";
243
244 ASSERT_EQ(1, d->response_started_count()) << "request failed: " <<
245 (int) r->status().status() << ", os error: " << r->status().os_error();
246
247 EXPECT_FALSE(d->received_data_before_response());
248
249 ASSERT_EQ(strlen(expected_data), static_cast<size_t>(d->bytes_received()));
250 EXPECT_EQ(0, memcmp(d->data_received().c_str(), expected_data,
251 strlen(expected_data)));
252 }
253
[email protected]7461a402011-03-24 23:19:51254 TestServer test_server_;
[email protected]b89290212009-08-14 22:37:35255};
256
[email protected]95409e12010-08-17 20:07:11257// In this unit test, we're using the HTTPTestServer as a proxy server and
258// issuing a CONNECT request with the magic host name "www.redirect.com".
259// The HTTPTestServer will return a 302 response, which we should not
260// follow.
[email protected]b89290212009-08-14 22:37:35261TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]95409e12010-08-17 20:07:11262 ASSERT_TRUE(test_server_.Start());
263
[email protected]87a09a92011-07-14 15:50:50264 TestNetworkDelegate network_delegate; // must outlive URLRequest
265 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
266 context->SetProxyFromString(test_server_.host_port_pair().ToString());
267 context->set_network_delegate(&network_delegate);
268 context->Init();
269
[email protected]d1ec59082009-02-11 02:48:15270 TestDelegate d;
271 {
[email protected]7461a402011-03-24 23:19:51272 URLRequest r(GURL("https://www.redirect.com/"), &d);
[email protected]87a09a92011-07-14 15:50:50273 r.set_context(context);
[email protected]d1ec59082009-02-11 02:48:15274 r.Start();
275 EXPECT_TRUE(r.is_pending());
276
277 MessageLoop::current()->Run();
278
[email protected]7461a402011-03-24 23:19:51279 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
280 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08281 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:15282 // We should not have followed the redirect.
283 EXPECT_EQ(0, d.received_redirect_count());
284 }
285}
286
[email protected]8202d0c2011-02-23 08:31:14287// This is the same as the previous test, but checks that the network delegate
288// registers the error.
289TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
290 ASSERT_TRUE(test_server_.Start());
291
[email protected]87a09a92011-07-14 15:50:50292 TestNetworkDelegate network_delegate; // must outlive URLRequest
293 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
294 context->SetProxyFromString(test_server_.host_port_pair().ToString());
295 context->set_network_delegate(&network_delegate);
296 context->Init();
297
[email protected]8202d0c2011-02-23 08:31:14298 TestDelegate d;
299 {
[email protected]7461a402011-03-24 23:19:51300 URLRequest r(GURL("https://www.redirect.com/"), &d);
[email protected]8202d0c2011-02-23 08:31:14301 r.set_context(context);
[email protected]8202d0c2011-02-23 08:31:14302 r.Start();
303 EXPECT_TRUE(r.is_pending());
304
305 MessageLoop::current()->Run();
306
[email protected]7461a402011-03-24 23:19:51307 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
308 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]8202d0c2011-02-23 08:31:14309 EXPECT_EQ(1, d.response_started_count());
310 // We should not have followed the redirect.
311 EXPECT_EQ(0, d.received_redirect_count());
312
313 EXPECT_EQ(1, network_delegate.error_count());
[email protected]a83dd332011-07-13 10:41:01314 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_os_error());
[email protected]8202d0c2011-02-23 08:31:14315 }
316}
317
[email protected]4c76d7c2011-04-15 19:14:12318// Tests that the network delegate can block and cancel a request.
319TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
320 ASSERT_TRUE(test_server_.Start());
321
322 TestDelegate d;
323 BlockingNetworkDelegate network_delegate;
324 network_delegate.set_callback_retval(ERR_EMPTY_RESPONSE);
325
[email protected]87a09a92011-07-14 15:50:50326 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
327 context->SetProxyFromString(test_server_.host_port_pair().ToString());
328 context->set_network_delegate(&network_delegate);
329 context->Init();
330
[email protected]4c76d7c2011-04-15 19:14:12331 {
332 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]4c76d7c2011-04-15 19:14:12333 r.set_context(context);
334
335 r.Start();
336 MessageLoop::current()->Run();
337
338 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
339 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().os_error());
340 EXPECT_EQ(1, network_delegate.created_requests());
341 EXPECT_EQ(0, network_delegate.destroyed_requests());
342 }
343 EXPECT_EQ(1, network_delegate.destroyed_requests());
344}
345
346// Tests that the network delegate can block and redirect a request to a new
347// URL.
348TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
349 ASSERT_TRUE(test_server_.Start());
350
351 TestDelegate d;
352 BlockingNetworkDelegate network_delegate;
353 GURL redirect_url(test_server_.GetURL("simple.html"));
354 network_delegate.set_redirect_url(redirect_url);
355
[email protected]87a09a92011-07-14 15:50:50356 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
357 context->SetProxyFromString(test_server_.host_port_pair().ToString());
358 context->set_network_delegate(&network_delegate);
359 context->Init();
360
[email protected]4c76d7c2011-04-15 19:14:12361 {
[email protected]8f1ac082011-04-19 21:14:13362 GURL original_url(test_server_.GetURL("empty.html"));
363 TestURLRequest r(original_url, &d);
[email protected]4c76d7c2011-04-15 19:14:12364 r.set_context(context);
365
366 r.Start();
367 MessageLoop::current()->Run();
368
369 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
370 EXPECT_EQ(0, r.status().os_error());
371 EXPECT_EQ(redirect_url, r.url());
[email protected]8f1ac082011-04-19 21:14:13372 EXPECT_EQ(original_url, r.original_url());
373 EXPECT_EQ(2U, r.url_chain().size());
[email protected]4c76d7c2011-04-15 19:14:12374 EXPECT_EQ(1, network_delegate.created_requests());
375 EXPECT_EQ(0, network_delegate.destroyed_requests());
376 }
377 EXPECT_EQ(1, network_delegate.destroyed_requests());
378}
379
[email protected]95409e12010-08-17 20:07:11380// In this unit test, we're using the HTTPTestServer as a proxy server and
381// issuing a CONNECT request with the magic host name "www.server-auth.com".
382// The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:35383TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]95409e12010-08-17 20:07:11384 ASSERT_TRUE(test_server_.Start());
385
[email protected]87a09a92011-07-14 15:50:50386 TestNetworkDelegate network_delegate; // must outlive URLRequest
387 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
388 context->SetProxyFromString(test_server_.host_port_pair().ToString());
389 context->set_network_delegate(&network_delegate);
390 context->Init();
391
[email protected]dc651782009-02-14 01:45:08392 TestDelegate d;
393 {
[email protected]7461a402011-03-24 23:19:51394 URLRequest r(GURL("https://www.server-auth.com/"), &d);
[email protected]87a09a92011-07-14 15:50:50395 r.set_context(context);
[email protected]dc651782009-02-14 01:45:08396
397 r.Start();
398 EXPECT_TRUE(r.is_pending());
399
400 MessageLoop::current()->Run();
401
[email protected]7461a402011-03-24 23:19:51402 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
403 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08404 }
405}
406
[email protected]b89290212009-08-14 22:37:35407TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
[email protected]95409e12010-08-17 20:07:11408 ASSERT_TRUE(test_server_.Start());
409
initial.commit586acc5fe2008-07-26 22:42:52410 TestDelegate d;
411 {
[email protected]95409e12010-08-17 20:07:11412 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:50413 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52414
415 r.Start();
416 EXPECT_TRUE(r.is_pending());
417
418 MessageLoop::current()->Run();
419
420 EXPECT_EQ(1, d.response_started_count());
421 EXPECT_FALSE(d.received_data_before_response());
422 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:19423 EXPECT_EQ(test_server_.host_port_pair().host(),
424 r.GetSocketAddress().host());
425 EXPECT_EQ(test_server_.host_port_pair().port(),
426 r.GetSocketAddress().port());
[email protected]c31a54592009-09-04 02:36:16427
[email protected]9e743cd2010-03-16 07:03:53428 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:52429 }
initial.commit586acc5fe2008-07-26 22:42:52430}
431
[email protected]b89290212009-08-14 22:37:35432TEST_F(URLRequestTestHTTP, GetTest) {
[email protected]95409e12010-08-17 20:07:11433 ASSERT_TRUE(test_server_.Start());
434
initial.commit586acc5fe2008-07-26 22:42:52435 TestDelegate d;
436 {
[email protected]95409e12010-08-17 20:07:11437 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:50438 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52439
440 r.Start();
441 EXPECT_TRUE(r.is_pending());
442
443 MessageLoop::current()->Run();
444
445 EXPECT_EQ(1, d.response_started_count());
446 EXPECT_FALSE(d.received_data_before_response());
447 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:19448 EXPECT_EQ(test_server_.host_port_pair().host(),
449 r.GetSocketAddress().host());
450 EXPECT_EQ(test_server_.host_port_pair().port(),
451 r.GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:52452 }
[email protected]5d7b373e2009-09-02 07:19:03453}
454
[email protected]aad63572011-05-24 20:14:39455TEST_F(URLRequestTestHTTP, GetZippedTest) {
456 ASSERT_TRUE(test_server_.Start());
457
458 // Parameter that specifies the Content-Length field in the response:
459 // C - Compressed length.
460 // U - Uncompressed length.
461 // L - Large length (larger than both C & U).
462 // M - Medium length (between C & U).
463 // S - Small length (smaller than both C & U).
464 const char test_parameters[] = "CULMS";
465 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
466 // C & U should be OK.
[email protected]f0e2bf42011-07-22 21:21:44467 // L & M are larger than the data sent, and show an error.
[email protected]aad63572011-05-24 20:14:39468 // S has too little data, but we seem to accept it.
469 const bool test_expect_success[num_tests] =
[email protected]f0e2bf42011-07-22 21:21:44470 { true, true, false, false, true };
[email protected]aad63572011-05-24 20:14:39471
472 for (int i = 0; i < num_tests ; i++) {
473 TestDelegate d;
474 {
475 std::string test_file =
476 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
477 test_parameters[i]);
[email protected]aad63572011-05-24 20:14:39478
[email protected]87a09a92011-07-14 15:50:50479 TestNetworkDelegate network_delegate; // must outlive URLRequest
480 scoped_refptr<TestURLRequestContext> context(
481 new TestURLRequestContext(true));
482 context->set_network_delegate(&network_delegate);
483 context->Init();
484
485 TestURLRequest r(test_server_.GetURL(test_file), &d);
486 r.set_context(context);
[email protected]aad63572011-05-24 20:14:39487 r.Start();
488 EXPECT_TRUE(r.is_pending());
489
490 MessageLoop::current()->Run();
491
492 EXPECT_EQ(1, d.response_started_count());
493 EXPECT_FALSE(d.received_data_before_response());
494 VLOG(1) << " Received " << d.bytes_received() << " bytes"
495 << " status = " << r.status().status()
496 << " os_error = " << r.status().os_error();
497 if (test_expect_success[i]) {
498 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status())
499 << " Parameter = \"" << test_file << "\"";
500 } else {
501 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
502 EXPECT_EQ(-100, r.status().os_error())
503 << " Parameter = \"" << test_file << "\"";
504 }
505 }
506 }
507}
508
[email protected]7844480a2009-12-16 21:18:58509TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
[email protected]95409e12010-08-17 20:07:11510 ASSERT_TRUE(test_server_.Start());
511
[email protected]7461a402011-03-24 23:19:51512 TestServer https_test_server(
513 TestServer::TYPE_HTTPS, FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:11514 ASSERT_TRUE(https_test_server.Start());
[email protected]7844480a2009-12-16 21:18:58515
516 // An https server is sent a request with an https referer,
517 // and responds with a redirect to an http url. The http
518 // server should not be sent the referer.
[email protected]95409e12010-08-17 20:07:11519 GURL http_destination = test_server_.GetURL("");
[email protected]7844480a2009-12-16 21:18:58520 TestDelegate d;
[email protected]95409e12010-08-17 20:07:11521 TestURLRequest req(https_test_server.GetURL(
[email protected]7844480a2009-12-16 21:18:58522 "server-redirect?" + http_destination.spec()), &d);
[email protected]87a09a92011-07-14 15:50:50523 req.set_context(default_context_);
[email protected]7844480a2009-12-16 21:18:58524 req.set_referrer("https://www.referrer.com/");
525 req.Start();
526 MessageLoop::current()->Run();
527
528 EXPECT_EQ(1, d.response_started_count());
529 EXPECT_EQ(1, d.received_redirect_count());
530 EXPECT_EQ(http_destination, req.url());
531 EXPECT_EQ(std::string(), req.referrer());
532}
533
[email protected]8f1ac082011-04-19 21:14:13534TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
535 ASSERT_TRUE(test_server_.Start());
536
537 GURL destination_url = test_server_.GetURL("");
538 GURL middle_redirect_url = test_server_.GetURL(
539 "server-redirect?" + destination_url.spec());
540 GURL original_url = test_server_.GetURL(
541 "server-redirect?" + middle_redirect_url.spec());
542 TestDelegate d;
543 TestURLRequest req(original_url, &d);
[email protected]87a09a92011-07-14 15:50:50544 req.set_context(default_context_);
[email protected]8f1ac082011-04-19 21:14:13545 req.Start();
546 MessageLoop::current()->Run();
547
548 EXPECT_EQ(1, d.response_started_count());
549 EXPECT_EQ(2, d.received_redirect_count());
550 EXPECT_EQ(destination_url, req.url());
551 EXPECT_EQ(original_url, req.original_url());
552 ASSERT_EQ(3U, req.url_chain().size());
553 EXPECT_EQ(original_url, req.url_chain()[0]);
554 EXPECT_EQ(middle_redirect_url, req.url_chain()[1]);
555 EXPECT_EQ(destination_url, req.url_chain()[2]);
556}
557
[email protected]73e0bba2009-02-19 22:57:09558class HTTPSRequestTest : public testing::Test {
[email protected]87a09a92011-07-14 15:50:50559 public:
560 HTTPSRequestTest() : default_context_(new TestURLRequestContext(true)) {
561 default_context_->set_network_delegate(&default_network_delegate_);
562 default_context_->Init();
563 }
564 virtual ~HTTPSRequestTest() {}
565
566 protected:
567 TestNetworkDelegate default_network_delegate_; // must outlive URLRequest
568 scoped_refptr<TestURLRequestContext> default_context_;
[email protected]ea224582008-12-07 20:25:46569};
570
[email protected]5774ada2010-07-15 06:30:54571TEST_F(HTTPSRequestTest, HTTPSGetTest) {
[email protected]7461a402011-03-24 23:19:51572 TestServer test_server(TestServer::TYPE_HTTPS,
[email protected]87a09a92011-07-14 15:50:50573 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:11574 ASSERT_TRUE(test_server.Start());
[email protected]ea224582008-12-07 20:25:46575
[email protected]ea224582008-12-07 20:25:46576 TestDelegate d;
577 {
[email protected]95409e12010-08-17 20:07:11578 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:50579 r.set_context(default_context_);
[email protected]ea224582008-12-07 20:25:46580 r.Start();
581 EXPECT_TRUE(r.is_pending());
582
583 MessageLoop::current()->Run();
584
585 EXPECT_EQ(1, d.response_started_count());
586 EXPECT_FALSE(d.received_data_before_response());
587 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17588 CheckSSLInfo(r.ssl_info());
[email protected]6d81b482011-02-22 19:47:19589 EXPECT_EQ(test_server.host_port_pair().host(),
590 r.GetSocketAddress().host());
591 EXPECT_EQ(test_server.host_port_pair().port(),
592 r.GetSocketAddress().port());
[email protected]ea224582008-12-07 20:25:46593 }
[email protected]ea224582008-12-07 20:25:46594}
595
[email protected]5774ada2010-07-15 06:30:54596TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]7461a402011-03-24 23:19:51597 TestServer::HTTPSOptions https_options(
598 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME);
599 TestServer test_server(https_options,
[email protected]87a09a92011-07-14 15:50:50600 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:11601 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:33602
603 bool err_allowed = true;
604 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
605 TestDelegate d;
606 {
607 d.set_allow_certificate_errors(err_allowed);
[email protected]95409e12010-08-17 20:07:11608 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:50609 r.set_context(default_context_);
[email protected]bacff652009-03-31 17:50:33610
611 r.Start();
612 EXPECT_TRUE(r.is_pending());
613
614 MessageLoop::current()->Run();
615
616 EXPECT_EQ(1, d.response_started_count());
617 EXPECT_FALSE(d.received_data_before_response());
618 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:17619 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:33620 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17621 CheckSSLInfo(r.ssl_info());
622 } else {
[email protected]bacff652009-03-31 17:50:33623 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17624 }
[email protected]bacff652009-03-31 17:50:33625 }
626 }
627}
628
[email protected]5774ada2010-07-15 06:30:54629TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]7461a402011-03-24 23:19:51630 TestServer::HTTPSOptions https_options(
631 TestServer::HTTPSOptions::CERT_EXPIRED);
632 TestServer test_server(https_options,
[email protected]87a09a92011-07-14 15:50:50633 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:11634 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:33635
636 // Iterate from false to true, just so that we do the opposite of the
637 // previous test in order to increase test coverage.
638 bool err_allowed = false;
639 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
640 TestDelegate d;
641 {
642 d.set_allow_certificate_errors(err_allowed);
[email protected]95409e12010-08-17 20:07:11643 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:50644 r.set_context(default_context_);
[email protected]bacff652009-03-31 17:50:33645
646 r.Start();
647 EXPECT_TRUE(r.is_pending());
648
649 MessageLoop::current()->Run();
650
651 EXPECT_EQ(1, d.response_started_count());
652 EXPECT_FALSE(d.received_data_before_response());
653 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:17654 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:33655 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17656 CheckSSLInfo(r.ssl_info());
657 } else {
[email protected]bacff652009-03-31 17:50:33658 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:17659 }
[email protected]bacff652009-03-31 17:50:33660 }
661 }
662}
[email protected]73e0bba2009-02-19 22:57:09663
[email protected]8df162a2010-08-07 01:10:02664namespace {
665
666class SSLClientAuthTestDelegate : public TestDelegate {
667 public:
668 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
669 }
670 virtual void OnCertificateRequested(
[email protected]7461a402011-03-24 23:19:51671 URLRequest* request,
672 SSLCertRequestInfo* cert_request_info) {
[email protected]8df162a2010-08-07 01:10:02673 on_certificate_requested_count_++;
674 MessageLoop::current()->Quit();
675 }
676 int on_certificate_requested_count() {
677 return on_certificate_requested_count_;
678 }
679 private:
680 int on_certificate_requested_count_;
681};
682
683} // namespace
684
685// TODO(davidben): Test the rest of the code. Specifically,
686// - Filtering which certificates to select.
687// - Sending a certificate back.
688// - Getting a certificate request in an SSL renegotiation sending the
689// HTTP request.
690TEST_F(HTTPSRequestTest, ClientAuthTest) {
[email protected]7461a402011-03-24 23:19:51691 TestServer::HTTPSOptions https_options;
[email protected]347599952010-10-28 11:57:36692 https_options.request_client_certificate = true;
[email protected]7461a402011-03-24 23:19:51693 TestServer test_server(https_options,
[email protected]87a09a92011-07-14 15:50:50694 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:11695 ASSERT_TRUE(test_server.Start());
[email protected]8df162a2010-08-07 01:10:02696
697 SSLClientAuthTestDelegate d;
698 {
[email protected]95409e12010-08-17 20:07:11699 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:50700 r.set_context(default_context_);
[email protected]8df162a2010-08-07 01:10:02701
702 r.Start();
703 EXPECT_TRUE(r.is_pending());
704
705 MessageLoop::current()->Run();
706
707 EXPECT_EQ(1, d.on_certificate_requested_count());
708 EXPECT_FALSE(d.received_data_before_response());
709 EXPECT_EQ(0, d.bytes_received());
[email protected]65a3b912010-08-21 05:46:58710
711 // Send no certificate.
712 // TODO(davidben): Get temporary client cert import (with keys) working on
713 // all platforms so we can test sending a cert as well.
714 r.ContinueWithCertificate(NULL);
715
716 MessageLoop::current()->Run();
717
718 EXPECT_EQ(1, d.response_started_count());
719 EXPECT_FALSE(d.received_data_before_response());
720 EXPECT_NE(0, d.bytes_received());
[email protected]8df162a2010-08-07 01:10:02721 }
722}
723
[email protected]37314622009-08-17 20:29:39724TEST_F(URLRequestTestHTTP, CancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52725 TestDelegate d;
726 {
727 TestURLRequest r(GURL("http://www.google.com/"), &d);
[email protected]87a09a92011-07-14 15:50:50728 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52729
730 r.Start();
731 EXPECT_TRUE(r.is_pending());
732
733 r.Cancel();
734
735 MessageLoop::current()->Run();
736
737 // We expect to receive OnResponseStarted even though the request has been
738 // cancelled.
739 EXPECT_EQ(1, d.response_started_count());
740 EXPECT_EQ(0, d.bytes_received());
741 EXPECT_FALSE(d.received_data_before_response());
742 }
initial.commit586acc5fe2008-07-26 22:42:52743}
744
[email protected]37314622009-08-17 20:29:39745TEST_F(URLRequestTestHTTP, CancelTest2) {
[email protected]95409e12010-08-17 20:07:11746 ASSERT_TRUE(test_server_.Start());
[email protected]dd265012009-01-08 20:45:27747
initial.commit586acc5fe2008-07-26 22:42:52748 TestDelegate d;
749 {
[email protected]95409e12010-08-17 20:07:11750 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:50751 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52752
753 d.set_cancel_in_response_started(true);
754
755 r.Start();
756 EXPECT_TRUE(r.is_pending());
757
758 MessageLoop::current()->Run();
759
760 EXPECT_EQ(1, d.response_started_count());
761 EXPECT_EQ(0, d.bytes_received());
762 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:51763 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:52764 }
initial.commit586acc5fe2008-07-26 22:42:52765}
766
[email protected]37314622009-08-17 20:29:39767TEST_F(URLRequestTestHTTP, CancelTest3) {
[email protected]95409e12010-08-17 20:07:11768 ASSERT_TRUE(test_server_.Start());
769
initial.commit586acc5fe2008-07-26 22:42:52770 TestDelegate d;
771 {
[email protected]95409e12010-08-17 20:07:11772 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:50773 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52774
775 d.set_cancel_in_received_data(true);
776
777 r.Start();
778 EXPECT_TRUE(r.is_pending());
779
780 MessageLoop::current()->Run();
781
782 EXPECT_EQ(1, d.response_started_count());
783 // There is no guarantee about how much data was received
784 // before the cancel was issued. It could have been 0 bytes,
785 // or it could have been all the bytes.
786 // EXPECT_EQ(0, d.bytes_received());
787 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:51788 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:52789 }
initial.commit586acc5fe2008-07-26 22:42:52790}
791
[email protected]37314622009-08-17 20:29:39792TEST_F(URLRequestTestHTTP, CancelTest4) {
[email protected]95409e12010-08-17 20:07:11793 ASSERT_TRUE(test_server_.Start());
794
initial.commit586acc5fe2008-07-26 22:42:52795 TestDelegate d;
796 {
[email protected]95409e12010-08-17 20:07:11797 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:50798 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52799
800 r.Start();
801 EXPECT_TRUE(r.is_pending());
802
803 // The request will be implicitly canceled when it is destroyed. The
804 // test delegate must not post a quit message when this happens because
805 // this test doesn't actually have a message loop. The quit message would
806 // get put on this thread's message queue and the next test would exit
807 // early, causing problems.
808 d.set_quit_on_complete(false);
809 }
810 // expect things to just cleanup properly.
811
812 // we won't actually get a received reponse here because we've never run the
813 // message loop
814 EXPECT_FALSE(d.received_data_before_response());
815 EXPECT_EQ(0, d.bytes_received());
816}
817
[email protected]37314622009-08-17 20:29:39818TEST_F(URLRequestTestHTTP, CancelTest5) {
[email protected]95409e12010-08-17 20:07:11819 ASSERT_TRUE(test_server_.Start());
820
initial.commit586acc5fe2008-07-26 22:42:52821 // populate cache
822 {
823 TestDelegate d;
[email protected]7461a402011-03-24 23:19:51824 URLRequest r(test_server_.GetURL("cachetime"), &d);
[email protected]87a09a92011-07-14 15:50:50825 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52826 r.Start();
827 MessageLoop::current()->Run();
[email protected]7461a402011-03-24 23:19:51828 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:52829 }
830
831 // cancel read from cache (see bug 990242)
832 {
833 TestDelegate d;
[email protected]7461a402011-03-24 23:19:51834 URLRequest r(test_server_.GetURL("cachetime"), &d);
[email protected]87a09a92011-07-14 15:50:50835 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52836 r.Start();
837 r.Cancel();
838 MessageLoop::current()->Run();
839
[email protected]7461a402011-03-24 23:19:51840 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:52841 EXPECT_EQ(1, d.response_started_count());
842 EXPECT_EQ(0, d.bytes_received());
843 EXPECT_FALSE(d.received_data_before_response());
844 }
initial.commit586acc5fe2008-07-26 22:42:52845}
846
[email protected]37314622009-08-17 20:29:39847TEST_F(URLRequestTestHTTP, PostTest) {
[email protected]95409e12010-08-17 20:07:11848 ASSERT_TRUE(test_server_.Start());
[email protected]762d2db2010-01-11 19:03:01849 HTTPUploadDataOperationTest("POST");
850}
initial.commit586acc5fe2008-07-26 22:42:52851
[email protected]762d2db2010-01-11 19:03:01852TEST_F(URLRequestTestHTTP, PutTest) {
[email protected]95409e12010-08-17 20:07:11853 ASSERT_TRUE(test_server_.Start());
[email protected]762d2db2010-01-11 19:03:01854 HTTPUploadDataOperationTest("PUT");
initial.commit586acc5fe2008-07-26 22:42:52855}
856
[email protected]37314622009-08-17 20:29:39857TEST_F(URLRequestTestHTTP, PostEmptyTest) {
[email protected]95409e12010-08-17 20:07:11858 ASSERT_TRUE(test_server_.Start());
859
initial.commit586acc5fe2008-07-26 22:42:52860 TestDelegate d;
861 {
[email protected]95409e12010-08-17 20:07:11862 TestURLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:50863 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52864 r.set_method("POST");
865
866 r.Start();
867 EXPECT_TRUE(r.is_pending());
868
869 MessageLoop::current()->Run();
870
871 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
872 (int) r.status().status() << ", os error: " << r.status().os_error();
873
874 EXPECT_FALSE(d.received_data_before_response());
875 EXPECT_TRUE(d.data_received().empty());
876 }
initial.commit586acc5fe2008-07-26 22:42:52877}
878
[email protected]37314622009-08-17 20:29:39879TEST_F(URLRequestTestHTTP, PostFileTest) {
[email protected]95409e12010-08-17 20:07:11880 ASSERT_TRUE(test_server_.Start());
881
initial.commit586acc5fe2008-07-26 22:42:52882 TestDelegate d;
883 {
[email protected]95409e12010-08-17 20:07:11884 TestURLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:50885 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52886 r.set_method("POST");
887
[email protected]399b8702009-05-01 20:34:02888 FilePath dir;
initial.commit586acc5fe2008-07-26 22:42:52889 PathService::Get(base::DIR_EXE, &dir);
[email protected]9396b252008-09-29 17:29:38890 file_util::SetCurrentDirectory(dir);
initial.commit586acc5fe2008-07-26 22:42:52891
[email protected]72cbd322009-04-07 10:17:12892 FilePath path;
initial.commit586acc5fe2008-07-26 22:42:52893 PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]72cbd322009-04-07 10:17:12894 path = path.Append(FILE_PATH_LITERAL("net"));
895 path = path.Append(FILE_PATH_LITERAL("data"));
896 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
897 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
initial.commit586acc5fe2008-07-26 22:42:52898 r.AppendFileToUpload(path);
899
900 // This file should just be ignored in the upload stream.
[email protected]72cbd322009-04-07 10:17:12901 r.AppendFileToUpload(FilePath(FILE_PATH_LITERAL(
902 "c:\\path\\to\\non\\existant\\file.randomness.12345")));
initial.commit586acc5fe2008-07-26 22:42:52903
904 r.Start();
905 EXPECT_TRUE(r.is_pending());
906
907 MessageLoop::current()->Run();
908
[email protected]10a1fe92008-11-04 21:47:02909 int64 longsize;
910 ASSERT_EQ(true, file_util::GetFileSize(path, &longsize));
911 int size = static_cast<int>(longsize);
initial.commit586acc5fe2008-07-26 22:42:52912 scoped_array<char> buf(new char[size]);
913
[email protected]dd265012009-01-08 20:45:27914 int size_read = static_cast<int>(file_util::ReadFile(path,
915 buf.get(), size));
[email protected]eac0709a2008-11-04 21:00:46916 ASSERT_EQ(size, size_read);
initial.commit586acc5fe2008-07-26 22:42:52917
918 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
919 (int) r.status().status() << ", os error: " << r.status().os_error();
920
921 EXPECT_FALSE(d.received_data_before_response());
922
923 ASSERT_EQ(size, d.bytes_received());
924 EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size));
925 }
initial.commit586acc5fe2008-07-26 22:42:52926}
927
[email protected]699efe602011-01-25 07:17:11928TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
929 ASSERT_TRUE(test_server_.Start());
930
931 TestDelegate d;
932 {
933 TestURLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:50934 r.set_context(default_context_);
[email protected]699efe602011-01-25 07:17:11935 r.EnableChunkedUpload();
936 r.set_method("POST");
937 AddChunksToUpload(&r);
938 r.Start();
939 EXPECT_TRUE(r.is_pending());
940
941 MessageLoop::current()->Run();
942
943 VerifyReceivedDataMatchesChunks(&r, &d);
944 }
945}
946
947TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
948 ASSERT_TRUE(test_server_.Start());
949
950 TestDelegate d;
951 {
952 TestURLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:50953 r.set_context(default_context_);
[email protected]699efe602011-01-25 07:17:11954 r.EnableChunkedUpload();
955 r.set_method("POST");
956 r.Start();
957 EXPECT_TRUE(r.is_pending());
958
959 MessageLoop::current()->RunAllPending();
960 AddChunksToUpload(&r);
961 MessageLoop::current()->Run();
962
963 VerifyReceivedDataMatchesChunks(&r, &d);
964 }
965}
966
[email protected]7a0bb4bf2008-11-19 21:41:48967TEST_F(URLRequestTest, AboutBlankTest) {
initial.commit586acc5fe2008-07-26 22:42:52968 TestDelegate d;
969 {
970 TestURLRequest r(GURL("about:blank"), &d);
[email protected]87a09a92011-07-14 15:50:50971 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:52972
973 r.Start();
974 EXPECT_TRUE(r.is_pending());
975
976 MessageLoop::current()->Run();
977
978 EXPECT_TRUE(!r.is_pending());
979 EXPECT_FALSE(d.received_data_before_response());
980 EXPECT_EQ(d.bytes_received(), 0);
[email protected]6d81b482011-02-22 19:47:19981 EXPECT_EQ("", r.GetSocketAddress().host());
982 EXPECT_EQ(0, r.GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:52983 }
initial.commit586acc5fe2008-07-26 22:42:52984}
985
[email protected]fcb7fd92009-10-22 04:18:58986TEST_F(URLRequestTest, DataURLImageTest) {
987 TestDelegate d;
988 {
989 // Use our nice little Chrome logo.
990 TestURLRequest r(GURL(
991 "data:image/png;base64,"
992 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
993 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
994 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
995 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
996 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
997 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
998 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
999 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
1000 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
1001 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
1002 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
1003 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
1004 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
1005 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
1006 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
1007 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
1008 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
1009 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
1010 &d);
[email protected]87a09a92011-07-14 15:50:501011 r.set_context(default_context_);
[email protected]fcb7fd92009-10-22 04:18:581012
1013 r.Start();
1014 EXPECT_TRUE(r.is_pending());
1015
1016 MessageLoop::current()->Run();
1017
1018 EXPECT_TRUE(!r.is_pending());
1019 EXPECT_FALSE(d.received_data_before_response());
1020 EXPECT_EQ(d.bytes_received(), 911);
[email protected]6d81b482011-02-22 19:47:191021 EXPECT_EQ("", r.GetSocketAddress().host());
1022 EXPECT_EQ(0, r.GetSocketAddress().port());
[email protected]fcb7fd92009-10-22 04:18:581023 }
1024}
1025
[email protected]7a0bb4bf2008-11-19 21:41:481026TEST_F(URLRequestTest, FileTest) {
[email protected]b9e04f02008-11-27 04:03:571027 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:521028 PathService::Get(base::FILE_EXE, &app_path);
[email protected]7461a402011-03-24 23:19:511029 GURL app_url = FilePathToFileURL(app_path);
initial.commit586acc5fe2008-07-26 22:42:521030
1031 TestDelegate d;
1032 {
[email protected]b9e04f02008-11-27 04:03:571033 TestURLRequest r(app_url, &d);
[email protected]87a09a92011-07-14 15:50:501034 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521035
1036 r.Start();
1037 EXPECT_TRUE(r.is_pending());
1038
1039 MessageLoop::current()->Run();
1040
[email protected]b77280c2009-03-20 17:27:471041 int64 file_size = -1;
1042 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
initial.commit586acc5fe2008-07-26 22:42:521043
1044 EXPECT_TRUE(!r.is_pending());
1045 EXPECT_EQ(1, d.response_started_count());
1046 EXPECT_FALSE(d.received_data_before_response());
[email protected]9396b252008-09-29 17:29:381047 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:191048 EXPECT_EQ("", r.GetSocketAddress().host());
1049 EXPECT_EQ(0, r.GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:521050 }
initial.commit586acc5fe2008-07-26 22:42:521051}
1052
[email protected]661376a2009-04-29 02:04:231053TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
1054 const size_t buffer_size = 4000;
1055 scoped_array<char> buffer(new char[buffer_size]);
1056 FillBuffer(buffer.get(), buffer_size);
1057
1058 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:551059 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]7461a402011-03-24 23:19:511060 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:531061 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:231062
1063 int64 file_size;
1064 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
1065
1066 const size_t first_byte_position = 500;
1067 const size_t last_byte_position = buffer_size - first_byte_position;
1068 const size_t content_length = last_byte_position - first_byte_position + 1;
1069 std::string partial_buffer_string(buffer.get() + first_byte_position,
1070 buffer.get() + last_byte_position + 1);
1071
1072 TestDelegate d;
1073 {
1074 TestURLRequest r(temp_url, &d);
[email protected]87a09a92011-07-14 15:50:501075 r.set_context(default_context_);
[email protected]661376a2009-04-29 02:04:231076
[email protected]7461a402011-03-24 23:19:511077 HttpRequestHeaders headers;
1078 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]d8eb84242010-09-25 02:25:061079 base::StringPrintf(
1080 "bytes=%" PRIuS "-%" PRIuS,
1081 first_byte_position, last_byte_position));
[email protected]88e6b6f32010-05-07 23:14:251082 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:231083 r.Start();
1084 EXPECT_TRUE(r.is_pending());
1085
1086 MessageLoop::current()->Run();
1087 EXPECT_TRUE(!r.is_pending());
1088 EXPECT_EQ(1, d.response_started_count());
1089 EXPECT_FALSE(d.received_data_before_response());
1090 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
1091 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
1092 EXPECT_TRUE(partial_buffer_string == d.data_received());
1093 }
1094
1095 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:231096}
1097
1098TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
1099 const size_t buffer_size = 4000;
1100 scoped_array<char> buffer(new char[buffer_size]);
1101 FillBuffer(buffer.get(), buffer_size);
1102
1103 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:551104 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]7461a402011-03-24 23:19:511105 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:531106 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:231107
1108 int64 file_size;
1109 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
1110
1111 const size_t first_byte_position = 500;
1112 const size_t last_byte_position = buffer_size - 1;
1113 const size_t content_length = last_byte_position - first_byte_position + 1;
1114 std::string partial_buffer_string(buffer.get() + first_byte_position,
1115 buffer.get() + last_byte_position + 1);
1116
1117 TestDelegate d;
1118 {
1119 TestURLRequest r(temp_url, &d);
[email protected]87a09a92011-07-14 15:50:501120 r.set_context(default_context_);
[email protected]661376a2009-04-29 02:04:231121
[email protected]7461a402011-03-24 23:19:511122 HttpRequestHeaders headers;
1123 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]d8eb84242010-09-25 02:25:061124 base::StringPrintf("bytes=%" PRIuS "-",
1125 first_byte_position));
[email protected]88e6b6f32010-05-07 23:14:251126 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:231127 r.Start();
1128 EXPECT_TRUE(r.is_pending());
1129
1130 MessageLoop::current()->Run();
1131 EXPECT_TRUE(!r.is_pending());
1132 EXPECT_EQ(1, d.response_started_count());
1133 EXPECT_FALSE(d.received_data_before_response());
1134 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
1135 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
1136 EXPECT_TRUE(partial_buffer_string == d.data_received());
1137 }
1138
1139 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:231140}
1141
1142TEST_F(URLRequestTest, FileTestMultipleRanges) {
1143 const size_t buffer_size = 400000;
1144 scoped_array<char> buffer(new char[buffer_size]);
1145 FillBuffer(buffer.get(), buffer_size);
1146
1147 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:551148 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]7461a402011-03-24 23:19:511149 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:531150 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:231151
1152 int64 file_size;
1153 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
1154
1155 TestDelegate d;
1156 {
1157 TestURLRequest r(temp_url, &d);
[email protected]87a09a92011-07-14 15:50:501158 r.set_context(default_context_);
[email protected]661376a2009-04-29 02:04:231159
[email protected]7461a402011-03-24 23:19:511160 HttpRequestHeaders headers;
1161 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]88e6b6f32010-05-07 23:14:251162 "bytes=0-0,10-200,200-300");
1163 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:231164 r.Start();
1165 EXPECT_TRUE(r.is_pending());
1166
1167 MessageLoop::current()->Run();
1168 EXPECT_TRUE(d.request_failed());
1169 }
1170
1171 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:231172}
1173
[email protected]7a0bb4bf2008-11-19 21:41:481174TEST_F(URLRequestTest, InvalidUrlTest) {
initial.commit586acc5fe2008-07-26 22:42:521175 TestDelegate d;
1176 {
1177 TestURLRequest r(GURL("invalid url"), &d);
[email protected]87a09a92011-07-14 15:50:501178 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521179
1180 r.Start();
1181 EXPECT_TRUE(r.is_pending());
1182
1183 MessageLoop::current()->Run();
1184 EXPECT_TRUE(d.request_failed());
1185 }
initial.commit586acc5fe2008-07-26 22:42:521186}
1187
[email protected]37314622009-08-17 20:29:391188TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
[email protected]95409e12010-08-17 20:07:111189 ASSERT_TRUE(test_server_.Start());
1190
initial.commit586acc5fe2008-07-26 22:42:521191 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111192 TestURLRequest req(test_server_.GetURL("files/with-headers.html"), &d);
[email protected]87a09a92011-07-14 15:50:501193 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521194 req.Start();
1195 MessageLoop::current()->Run();
1196
[email protected]7461a402011-03-24 23:19:511197 const HttpResponseHeaders* headers = req.response_headers();
[email protected]589deddb2009-10-05 23:41:401198
1199 // Simple sanity check that response_info() accesses the same data.
1200 EXPECT_EQ(headers, req.response_info().headers.get());
1201
initial.commit586acc5fe2008-07-26 22:42:521202 std::string header;
1203 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
1204 EXPECT_EQ("private", header);
1205
1206 header.clear();
1207 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
1208 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
1209
1210 // The response has two "X-Multiple-Entries" headers.
1211 // This verfies our output has them concatenated together.
1212 header.clear();
1213 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
1214 EXPECT_EQ("a, b", header);
1215}
1216
[email protected]9396b252008-09-29 17:29:381217#if defined(OS_WIN)
[email protected]7a0bb4bf2008-11-19 21:41:481218TEST_F(URLRequestTest, ResolveShortcutTest) {
[email protected]399b8702009-05-01 20:34:021219 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:521220 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:021221 app_path = app_path.AppendASCII("net");
1222 app_path = app_path.AppendASCII("data");
1223 app_path = app_path.AppendASCII("url_request_unittest");
1224 app_path = app_path.AppendASCII("with-headers.html");
initial.commit586acc5fe2008-07-26 22:42:521225
[email protected]399b8702009-05-01 20:34:021226 std::wstring lnk_path = app_path.value() + L".lnk";
initial.commit586acc5fe2008-07-26 22:42:521227
1228 HRESULT result;
1229 IShellLink *shell = NULL;
1230 IPersistFile *persist = NULL;
1231
1232 CoInitialize(NULL);
1233 // Temporarily create a shortcut for test
1234 result = CoCreateInstance(CLSID_ShellLink, NULL,
[email protected]5d7b373e2009-09-02 07:19:031235 CLSCTX_INPROC_SERVER, IID_IShellLink,
1236 reinterpret_cast<LPVOID*>(&shell));
[email protected]d149ce82009-07-01 23:57:021237 ASSERT_TRUE(SUCCEEDED(result));
initial.commit586acc5fe2008-07-26 22:42:521238 result = shell->QueryInterface(IID_IPersistFile,
[email protected]5d7b373e2009-09-02 07:19:031239 reinterpret_cast<LPVOID*>(&persist));
[email protected]d149ce82009-07-01 23:57:021240 ASSERT_TRUE(SUCCEEDED(result));
[email protected]399b8702009-05-01 20:34:021241 result = shell->SetPath(app_path.value().c_str());
initial.commit586acc5fe2008-07-26 22:42:521242 EXPECT_TRUE(SUCCEEDED(result));
1243 result = shell->SetDescription(L"ResolveShortcutTest");
1244 EXPECT_TRUE(SUCCEEDED(result));
1245 result = persist->Save(lnk_path.c_str(), TRUE);
1246 EXPECT_TRUE(SUCCEEDED(result));
1247 if (persist)
1248 persist->Release();
1249 if (shell)
1250 shell->Release();
1251
1252 TestDelegate d;
1253 {
[email protected]7461a402011-03-24 23:19:511254 TestURLRequest r(FilePathToFileURL(FilePath(lnk_path)), &d);
[email protected]87a09a92011-07-14 15:50:501255 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521256
1257 r.Start();
1258 EXPECT_TRUE(r.is_pending());
1259
1260 MessageLoop::current()->Run();
1261
1262 WIN32_FILE_ATTRIBUTE_DATA data;
[email protected]399b8702009-05-01 20:34:021263 GetFileAttributesEx(app_path.value().c_str(),
1264 GetFileExInfoStandard, &data);
1265 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
initial.commit586acc5fe2008-07-26 22:42:521266 FILE_SHARE_READ, NULL, OPEN_EXISTING,
1267 FILE_ATTRIBUTE_NORMAL, NULL);
1268 EXPECT_NE(INVALID_HANDLE_VALUE, file);
1269 scoped_array<char> buffer(new char[data.nFileSizeLow]);
1270 DWORD read_size;
1271 BOOL result;
1272 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
1273 &read_size, NULL);
1274 std::string content(buffer.get(), read_size);
1275 CloseHandle(file);
1276
1277 EXPECT_TRUE(!r.is_pending());
1278 EXPECT_EQ(1, d.received_redirect_count());
1279 EXPECT_EQ(content, d.data_received());
1280 }
1281
1282 // Clean the shortcut
1283 DeleteFile(lnk_path.c_str());
1284 CoUninitialize();
initial.commit586acc5fe2008-07-26 22:42:521285}
[email protected]9396b252008-09-29 17:29:381286#endif // defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:521287
[email protected]37314622009-08-17 20:29:391288TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
[email protected]95409e12010-08-17 20:07:111289 ASSERT_TRUE(test_server_.Start());
[email protected]dd265012009-01-08 20:45:271290
initial.commit586acc5fe2008-07-26 22:42:521291 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111292 TestURLRequest req(test_server_.GetURL(
initial.commit586acc5fe2008-07-26 22:42:521293 "files/content-type-normalization.html"), &d);
[email protected]87a09a92011-07-14 15:50:501294 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521295 req.Start();
1296 MessageLoop::current()->Run();
1297
1298 std::string mime_type;
1299 req.GetMimeType(&mime_type);
1300 EXPECT_EQ("text/html", mime_type);
1301
1302 std::string charset;
1303 req.GetCharset(&charset);
1304 EXPECT_EQ("utf-8", charset);
1305 req.Cancel();
1306}
1307
[email protected]7a0bb4bf2008-11-19 21:41:481308TEST_F(URLRequestTest, FileDirCancelTest) {
initial.commit586acc5fe2008-07-26 22:42:521309 // Put in mock resource provider.
[email protected]7461a402011-03-24 23:19:511310 NetModule::SetResourceProvider(TestNetResourceProvider);
initial.commit586acc5fe2008-07-26 22:42:521311
1312 TestDelegate d;
1313 {
[email protected]72cbd322009-04-07 10:17:121314 FilePath file_path;
initial.commit586acc5fe2008-07-26 22:42:521315 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
[email protected]72cbd322009-04-07 10:17:121316 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
1317 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
initial.commit586acc5fe2008-07-26 22:42:521318
[email protected]7461a402011-03-24 23:19:511319 TestURLRequest req(FilePathToFileURL(file_path), &d);
[email protected]87a09a92011-07-14 15:50:501320 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521321 req.Start();
1322 EXPECT_TRUE(req.is_pending());
1323
1324 d.set_cancel_in_received_data_pending(true);
1325
1326 MessageLoop::current()->Run();
1327 }
initial.commit586acc5fe2008-07-26 22:42:521328
1329 // Take out mock resource provider.
[email protected]7461a402011-03-24 23:19:511330 NetModule::SetResourceProvider(NULL);
initial.commit586acc5fe2008-07-26 22:42:521331}
1332
[email protected]7886a8c2009-08-21 04:11:091333TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
1334 // There is an implicit redirect when loading a file path that matches a
1335 // directory and does not end with a slash. Ensure that following such
1336 // redirects does not crash. See http://crbug.com/18686.
1337
1338 FilePath path;
1339 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1340 path = path.Append(FILE_PATH_LITERAL("net"));
1341 path = path.Append(FILE_PATH_LITERAL("data"));
1342 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1343
1344 TestDelegate d;
[email protected]7461a402011-03-24 23:19:511345 TestURLRequest req(FilePathToFileURL(path), &d);
[email protected]87a09a92011-07-14 15:50:501346 req.set_context(default_context_);
[email protected]7886a8c2009-08-21 04:11:091347 req.Start();
1348 MessageLoop::current()->Run();
1349
[email protected]0db55772010-06-14 22:27:441350 ASSERT_EQ(1, d.received_redirect_count());
1351 ASSERT_LT(0, d.bytes_received());
1352 ASSERT_FALSE(d.request_failed());
1353 ASSERT_TRUE(req.status().is_success());
[email protected]7886a8c2009-08-21 04:11:091354}
1355
[email protected]0db55772010-06-14 22:27:441356#if defined(OS_WIN)
1357// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
1358TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
1359 TestDelegate d;
1360 TestURLRequest req(GURL("file:///"), &d);
[email protected]87a09a92011-07-14 15:50:501361 req.set_context(default_context_);
[email protected]0db55772010-06-14 22:27:441362 req.Start();
1363 MessageLoop::current()->Run();
1364
1365 ASSERT_EQ(1, d.received_redirect_count());
1366 ASSERT_FALSE(req.status().is_success());
1367}
1368#endif
1369
[email protected]37314622009-08-17 20:29:391370TEST_F(URLRequestTestHTTP, RestrictRedirects) {
[email protected]95409e12010-08-17 20:07:111371 ASSERT_TRUE(test_server_.Start());
[email protected]dd265012009-01-08 20:45:271372
initial.commit586acc5fe2008-07-26 22:42:521373 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111374 TestURLRequest req(test_server_.GetURL(
initial.commit586acc5fe2008-07-26 22:42:521375 "files/redirect-to-file.html"), &d);
[email protected]87a09a92011-07-14 15:50:501376 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521377 req.Start();
1378 MessageLoop::current()->Run();
1379
[email protected]7461a402011-03-24 23:19:511380 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1381 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().os_error());
initial.commit586acc5fe2008-07-26 22:42:521382}
1383
[email protected]37314622009-08-17 20:29:391384TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
[email protected]95409e12010-08-17 20:07:111385 ASSERT_TRUE(test_server_.Start());
[email protected]4e66ed12009-07-21 23:38:421386
1387 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111388 TestURLRequest req(test_server_.GetURL(
[email protected]4e66ed12009-07-21 23:38:421389 "files/redirect-to-invalid-url.html"), &d);
[email protected]87a09a92011-07-14 15:50:501390 req.set_context(default_context_);
[email protected]4e66ed12009-07-21 23:38:421391 req.Start();
1392 MessageLoop::current()->Run();
1393
[email protected]7461a402011-03-24 23:19:511394 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1395 EXPECT_EQ(ERR_INVALID_URL, req.status().os_error());
[email protected]4e66ed12009-07-21 23:38:421396}
1397
[email protected]37314622009-08-17 20:29:391398TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
[email protected]95409e12010-08-17 20:07:111399 ASSERT_TRUE(test_server_.Start());
1400
initial.commit586acc5fe2008-07-26 22:42:521401 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:501402 TestURLRequest req(test_server_.GetURL("echoheader?Referer"), &d);
1403 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521404 req.set_referrer("http://user:[email protected]/");
1405 req.Start();
1406 MessageLoop::current()->Run();
1407
1408 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
1409}
1410
[email protected]37314622009-08-17 20:29:391411TEST_F(URLRequestTestHTTP, CancelRedirect) {
[email protected]95409e12010-08-17 20:07:111412 ASSERT_TRUE(test_server_.Start());
1413
initial.commit586acc5fe2008-07-26 22:42:521414 TestDelegate d;
1415 {
1416 d.set_cancel_in_received_redirect(true);
[email protected]87a09a92011-07-14 15:50:501417 TestURLRequest req(test_server_.GetURL("files/redirect-test.html"), &d);
1418 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521419 req.Start();
1420 MessageLoop::current()->Run();
1421
1422 EXPECT_EQ(1, d.response_started_count());
1423 EXPECT_EQ(0, d.bytes_received());
1424 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:511425 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
initial.commit586acc5fe2008-07-26 22:42:521426 }
1427}
1428
[email protected]37314622009-08-17 20:29:391429TEST_F(URLRequestTestHTTP, DeferredRedirect) {
[email protected]95409e12010-08-17 20:07:111430 ASSERT_TRUE(test_server_.Start());
1431
[email protected]195e77d2009-07-23 19:10:231432 TestDelegate d;
1433 {
1434 d.set_quit_on_redirect(true);
[email protected]87a09a92011-07-14 15:50:501435 TestURLRequest req(test_server_.GetURL("files/redirect-test.html"), &d);
1436 req.set_context(default_context_);
[email protected]195e77d2009-07-23 19:10:231437 req.Start();
1438 MessageLoop::current()->Run();
1439
1440 EXPECT_EQ(1, d.received_redirect_count());
1441
1442 req.FollowDeferredRedirect();
1443 MessageLoop::current()->Run();
1444
1445 EXPECT_EQ(1, d.response_started_count());
1446 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:511447 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
[email protected]195e77d2009-07-23 19:10:231448
1449 FilePath path;
1450 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1451 path = path.Append(FILE_PATH_LITERAL("net"));
1452 path = path.Append(FILE_PATH_LITERAL("data"));
1453 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1454 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
1455
1456 std::string contents;
1457 EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
1458 EXPECT_EQ(contents, d.data_received());
1459 }
1460}
1461
[email protected]37314622009-08-17 20:29:391462TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
[email protected]95409e12010-08-17 20:07:111463 ASSERT_TRUE(test_server_.Start());
1464
[email protected]195e77d2009-07-23 19:10:231465 TestDelegate d;
1466 {
1467 d.set_quit_on_redirect(true);
[email protected]87a09a92011-07-14 15:50:501468 TestURLRequest req(test_server_.GetURL("files/redirect-test.html"), &d);
1469 req.set_context(default_context_);
[email protected]195e77d2009-07-23 19:10:231470 req.Start();
1471 MessageLoop::current()->Run();
1472
1473 EXPECT_EQ(1, d.received_redirect_count());
1474
1475 req.Cancel();
1476 MessageLoop::current()->Run();
1477
1478 EXPECT_EQ(1, d.response_started_count());
1479 EXPECT_EQ(0, d.bytes_received());
1480 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:511481 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]195e77d2009-07-23 19:10:231482 }
1483}
1484
[email protected]37314622009-08-17 20:29:391485TEST_F(URLRequestTestHTTP, VaryHeader) {
[email protected]95409e12010-08-17 20:07:111486 ASSERT_TRUE(test_server_.Start());
initial.commit586acc5fe2008-07-26 22:42:521487
initial.commit586acc5fe2008-07-26 22:42:521488 // populate the cache
1489 {
1490 TestDelegate d;
[email protected]1bc869d52011-04-07 17:52:051491 URLRequest req(test_server_.GetURL("echoheadercache?foo"), &d);
[email protected]87a09a92011-07-14 15:50:501492 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:511493 HttpRequestHeaders headers;
[email protected]88e6b6f32010-05-07 23:14:251494 headers.SetHeader("foo", "1");
1495 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:521496 req.Start();
1497 MessageLoop::current()->Run();
initial.commit586acc5fe2008-07-26 22:42:521498 }
1499
initial.commit586acc5fe2008-07-26 22:42:521500 // expect a cache hit
1501 {
1502 TestDelegate d;
[email protected]1bc869d52011-04-07 17:52:051503 URLRequest req(test_server_.GetURL("echoheadercache?foo"), &d);
[email protected]87a09a92011-07-14 15:50:501504 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:511505 HttpRequestHeaders headers;
[email protected]88e6b6f32010-05-07 23:14:251506 headers.SetHeader("foo", "1");
1507 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:521508 req.Start();
1509 MessageLoop::current()->Run();
1510
[email protected]cb4ff9d2009-09-04 22:51:531511 EXPECT_TRUE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521512 }
1513
1514 // expect a cache miss
1515 {
1516 TestDelegate d;
[email protected]1bc869d52011-04-07 17:52:051517 URLRequest req(test_server_.GetURL("echoheadercache?foo"), &d);
[email protected]87a09a92011-07-14 15:50:501518 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:511519 HttpRequestHeaders headers;
[email protected]88e6b6f32010-05-07 23:14:251520 headers.SetHeader("foo", "2");
1521 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:521522 req.Start();
1523 MessageLoop::current()->Run();
1524
[email protected]cb4ff9d2009-09-04 22:51:531525 EXPECT_FALSE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521526 }
1527}
1528
[email protected]cb4ff9d2009-09-04 22:51:531529TEST_F(URLRequestTestHTTP, BasicAuth) {
[email protected]95409e12010-08-17 20:07:111530 ASSERT_TRUE(test_server_.Start());
1531
initial.commit586acc5fe2008-07-26 22:42:521532 // populate the cache
1533 {
1534 TestDelegate d;
[email protected]13c8a092010-07-29 06:15:441535 d.set_username(kUser);
1536 d.set_password(kSecret);
initial.commit586acc5fe2008-07-26 22:42:521537
[email protected]7461a402011-03-24 23:19:511538 URLRequest r(test_server_.GetURL("auth-basic"), &d);
[email protected]87a09a92011-07-14 15:50:501539 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521540 r.Start();
1541
1542 MessageLoop::current()->Run();
1543
1544 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
initial.commit586acc5fe2008-07-26 22:42:521545 }
1546
initial.commit586acc5fe2008-07-26 22:42:521547 // repeat request with end-to-end validation. since auth-basic results in a
1548 // cachable page, we expect this test to result in a 304. in which case, the
1549 // response should be fetched from the cache.
1550 {
1551 TestDelegate d;
[email protected]13c8a092010-07-29 06:15:441552 d.set_username(kUser);
1553 d.set_password(kSecret);
initial.commit586acc5fe2008-07-26 22:42:521554
[email protected]7461a402011-03-24 23:19:511555 URLRequest r(test_server_.GetURL("auth-basic"), &d);
[email protected]87a09a92011-07-14 15:50:501556 r.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:511557 r.set_load_flags(LOAD_VALIDATE_CACHE);
initial.commit586acc5fe2008-07-26 22:42:521558 r.Start();
1559
1560 MessageLoop::current()->Run();
1561
1562 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1563
[email protected]cb4ff9d2009-09-04 22:51:531564 // Should be the same cached document.
1565 EXPECT_TRUE(r.was_cached());
initial.commit586acc5fe2008-07-26 22:42:521566 }
1567}
license.botbf09a502008-08-24 00:55:551568
[email protected]0757e7702009-03-27 04:00:221569// Check that Set-Cookie headers in 401 responses are respected.
1570// http://crbug.com/6450
[email protected]be6fca6c2010-01-30 21:48:571571TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
[email protected]95409e12010-08-17 20:07:111572 ASSERT_TRUE(test_server_.Start());
[email protected]0757e7702009-03-27 04:00:221573
1574 GURL url_requiring_auth =
[email protected]95409e12010-08-17 20:07:111575 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
[email protected]0757e7702009-03-27 04:00:221576
1577 // Request a page that will give a 401 containing a Set-Cookie header.
1578 // Verify that when the transaction is restarted, it includes the new cookie.
1579 {
[email protected]87a09a92011-07-14 15:50:501580 TestNetworkDelegate network_delegate; // must outlive URLRequest
1581 scoped_refptr<TestURLRequestContext> context(
1582 new TestURLRequestContext(true));
1583 context->set_network_delegate(&network_delegate);
1584 context->Init();
1585
[email protected]0757e7702009-03-27 04:00:221586 TestDelegate d;
[email protected]13c8a092010-07-29 06:15:441587 d.set_username(kUser);
1588 d.set_password(kSecret);
[email protected]0757e7702009-03-27 04:00:221589
[email protected]7461a402011-03-24 23:19:511590 URLRequest r(url_requiring_auth, &d);
[email protected]0757e7702009-03-27 04:00:221591 r.set_context(context);
1592 r.Start();
1593
1594 MessageLoop::current()->Run();
1595
1596 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1597
1598 // Make sure we sent the cookie in the restarted transaction.
1599 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1600 != std::string::npos);
1601 }
1602
1603 // Same test as above, except this time the restart is initiated earlier
1604 // (without user intervention since identity is embedded in the URL).
1605 {
[email protected]87a09a92011-07-14 15:50:501606 TestNetworkDelegate network_delegate; // must outlive URLRequest
1607 scoped_refptr<TestURLRequestContext> context(
1608 new TestURLRequestContext(true));
1609 context->set_network_delegate(&network_delegate);
1610 context->Init();
1611
[email protected]0757e7702009-03-27 04:00:221612 TestDelegate d;
1613
1614 GURL::Replacements replacements;
1615 std::string username("user2");
1616 std::string password("secret");
1617 replacements.SetUsernameStr(username);
1618 replacements.SetPasswordStr(password);
1619 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
1620
[email protected]7461a402011-03-24 23:19:511621 URLRequest r(url_with_identity, &d);
[email protected]0757e7702009-03-27 04:00:221622 r.set_context(context);
1623 r.Start();
1624
1625 MessageLoop::current()->Run();
1626
1627 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
1628
1629 // Make sure we sent the cookie in the restarted transaction.
1630 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1631 != std::string::npos);
1632 }
1633}
1634
[email protected]54f4c9362011-07-25 21:54:461635TEST_F(URLRequestTest, DelayedCookieCallback) {
1636 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
1637 ASSERT_TRUE(test_server.Start());
1638
1639 scoped_refptr<URLRequestContext> context(new TestURLRequestContext());
1640 scoped_refptr<DelayedCookieMonster> delayed_cm =
1641 new DelayedCookieMonster();
1642 scoped_refptr<CookieStore> cookie_store = delayed_cm;
1643 context->set_cookie_store(delayed_cm);
1644
1645 // Set up a cookie.
1646 {
1647 TestDelegate d;
1648 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
1649 req.set_context(context);
1650 req.Start();
1651 MessageLoop::current()->Run();
1652 EXPECT_EQ(0, d.blocked_get_cookies_count());
1653 EXPECT_EQ(0, d.blocked_set_cookie_count());
1654 EXPECT_EQ(1, d.set_cookie_count());
1655 }
1656
1657 // Verify that the cookie is set.
1658 {
1659 TestDelegate d;
1660 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
1661 req.set_context(context);
1662 req.Start();
1663 MessageLoop::current()->Run();
1664
1665 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1666 != std::string::npos);
1667 EXPECT_EQ(0, d.blocked_get_cookies_count());
1668 EXPECT_EQ(0, d.blocked_set_cookie_count());
1669 }
1670}
1671
[email protected]be6fca6c2010-01-30 21:48:571672TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]7461a402011-03-24 23:19:511673 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
[email protected]95409e12010-08-17 20:07:111674 ASSERT_TRUE(test_server.Start());
1675
[email protected]861fcd52009-08-26 02:33:461676 // Set up a cookie.
1677 {
1678 TestDelegate d;
[email protected]7461a402011-03-24 23:19:511679 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]87a09a92011-07-14 15:50:501680 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:461681 req.Start();
1682 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201683 EXPECT_EQ(0, d.blocked_get_cookies_count());
1684 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461685 }
1686
1687 // Verify that the cookie is set.
1688 {
1689 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111690 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:501691 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:461692 req.Start();
1693 MessageLoop::current()->Run();
1694
1695 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1696 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201697 EXPECT_EQ(0, d.blocked_get_cookies_count());
1698 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461699 }
1700
1701 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
1702 {
1703 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111704 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]7461a402011-03-24 23:19:511705 req.set_load_flags(LOAD_DO_NOT_SEND_COOKIES);
[email protected]87a09a92011-07-14 15:50:501706 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:461707 req.Start();
1708 MessageLoop::current()->Run();
1709
1710 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1711 == std::string::npos);
[email protected]abf03a02010-03-12 05:02:131712
[email protected]9fb83e82010-07-02 18:24:551713 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
[email protected]abf03a02010-03-12 05:02:131714 EXPECT_EQ(0, d.blocked_get_cookies_count());
[email protected]3dbb80b2010-02-09 22:41:201715 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461716 }
1717}
1718
[email protected]be6fca6c2010-01-30 21:48:571719TEST_F(URLRequestTest, DoNotSaveCookies) {
[email protected]7461a402011-03-24 23:19:511720 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
[email protected]95409e12010-08-17 20:07:111721 ASSERT_TRUE(test_server.Start());
1722
[email protected]861fcd52009-08-26 02:33:461723 // Set up a cookie.
1724 {
1725 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:501726 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d);
1727 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:461728 req.Start();
1729 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201730
1731 EXPECT_EQ(0, d.blocked_get_cookies_count());
1732 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:551733 EXPECT_EQ(1, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461734 }
1735
1736 // Try to set-up another cookie and update the previous cookie.
1737 {
[email protected]861fcd52009-08-26 02:33:461738 TestDelegate d;
[email protected]7461a402011-03-24 23:19:511739 URLRequest req(test_server.GetURL(
[email protected]861fcd52009-08-26 02:33:461740 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
[email protected]7461a402011-03-24 23:19:511741 req.set_load_flags(LOAD_DO_NOT_SAVE_COOKIES);
[email protected]87a09a92011-07-14 15:50:501742 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:461743 req.Start();
1744
1745 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201746
[email protected]9fb83e82010-07-02 18:24:551747 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
[email protected]3dbb80b2010-02-09 22:41:201748 EXPECT_EQ(0, d.blocked_get_cookies_count());
[email protected]abf03a02010-03-12 05:02:131749 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:551750 EXPECT_EQ(0, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461751 }
1752
1753 // Verify the cookies weren't saved or updated.
1754 {
1755 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111756 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:501757 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:461758 req.Start();
1759 MessageLoop::current()->Run();
1760
1761 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1762 == std::string::npos);
1763 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1764 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201765
1766 EXPECT_EQ(0, d.blocked_get_cookies_count());
1767 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:551768 EXPECT_EQ(0, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:461769 }
1770}
1771
[email protected]34602282010-02-03 22:14:151772TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
[email protected]7461a402011-03-24 23:19:511773 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
[email protected]95409e12010-08-17 20:07:111774 ASSERT_TRUE(test_server.Start());
1775
[email protected]34602282010-02-03 22:14:151776 // Set up a cookie.
1777 {
1778 TestDelegate d;
[email protected]7461a402011-03-24 23:19:511779 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]87a09a92011-07-14 15:50:501780 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151781 req.Start();
1782 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201783
1784 EXPECT_EQ(0, d.blocked_get_cookies_count());
1785 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151786 }
1787
1788 // Verify that the cookie is set.
1789 {
1790 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111791 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:501792 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151793 req.Start();
1794 MessageLoop::current()->Run();
1795
1796 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1797 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201798
1799 EXPECT_EQ(0, d.blocked_get_cookies_count());
1800 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151801 }
1802
1803 // Verify that the cookie isn't sent.
1804 {
[email protected]34602282010-02-03 22:14:151805 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:011806 d.set_cookie_options(TestDelegate::NO_GET_COOKIES);
[email protected]95409e12010-08-17 20:07:111807 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:501808 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151809 req.Start();
1810 MessageLoop::current()->Run();
1811
1812 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1813 == std::string::npos);
1814
[email protected]3dbb80b2010-02-09 22:41:201815 EXPECT_EQ(1, d.blocked_get_cookies_count());
1816 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151817 }
1818}
1819
1820TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
[email protected]7461a402011-03-24 23:19:511821 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
[email protected]95409e12010-08-17 20:07:111822 ASSERT_TRUE(test_server.Start());
1823
[email protected]34602282010-02-03 22:14:151824 // Set up a cookie.
1825 {
1826 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:501827 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d);
1828 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151829 req.Start();
1830 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201831
1832 EXPECT_EQ(0, d.blocked_get_cookies_count());
1833 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151834 }
1835
1836 // Try to set-up another cookie and update the previous cookie.
1837 {
[email protected]34602282010-02-03 22:14:151838 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:011839 d.set_cookie_options(TestDelegate::NO_SET_COOKIE);
[email protected]7461a402011-03-24 23:19:511840 URLRequest req(test_server.GetURL(
[email protected]34602282010-02-03 22:14:151841 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
[email protected]87a09a92011-07-14 15:50:501842 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151843 req.Start();
1844
1845 MessageLoop::current()->Run();
1846
[email protected]3dbb80b2010-02-09 22:41:201847 EXPECT_EQ(0, d.blocked_get_cookies_count());
1848 EXPECT_EQ(2, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151849 }
1850
1851
1852 // Verify the cookies weren't saved or updated.
1853 {
1854 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111855 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:501856 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151857 req.Start();
1858 MessageLoop::current()->Run();
1859
1860 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1861 == std::string::npos);
1862 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1863 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201864
1865 EXPECT_EQ(0, d.blocked_get_cookies_count());
1866 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151867 }
1868}
1869
[email protected]2adf2882010-09-27 08:30:371870TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
[email protected]7461a402011-03-24 23:19:511871 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
[email protected]2adf2882010-09-27 08:30:371872 ASSERT_TRUE(test_server.Start());
1873
[email protected]2adf2882010-09-27 08:30:371874 // Set up an empty cookie.
1875 {
1876 TestDelegate d;
[email protected]7461a402011-03-24 23:19:511877 URLRequest req(test_server.GetURL("set-cookie"), &d);
[email protected]87a09a92011-07-14 15:50:501878 req.set_context(default_context_);
[email protected]2adf2882010-09-27 08:30:371879 req.Start();
1880 MessageLoop::current()->Run();
1881
1882 EXPECT_EQ(0, d.blocked_get_cookies_count());
1883 EXPECT_EQ(0, d.blocked_set_cookie_count());
1884 EXPECT_EQ(0, d.set_cookie_count());
1885 }
1886}
1887
[email protected]34602282010-02-03 22:14:151888TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
[email protected]7461a402011-03-24 23:19:511889 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
[email protected]95409e12010-08-17 20:07:111890 ASSERT_TRUE(test_server.Start());
1891
[email protected]34602282010-02-03 22:14:151892 // Set up a cookie.
1893 {
1894 TestDelegate d;
[email protected]7461a402011-03-24 23:19:511895 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]87a09a92011-07-14 15:50:501896 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151897 req.Start();
1898 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201899
1900 EXPECT_EQ(0, d.blocked_get_cookies_count());
1901 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151902 }
1903
1904 // Verify that the cookie is set.
1905 {
1906 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111907 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:501908 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151909 req.Start();
1910 MessageLoop::current()->Run();
1911
1912 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1913 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201914
1915 EXPECT_EQ(0, d.blocked_get_cookies_count());
1916 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151917 }
1918
1919 // Verify that the cookie isn't sent.
1920 {
[email protected]34602282010-02-03 22:14:151921 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:011922 d.set_cookie_options(TestDelegate::NO_GET_COOKIES);
[email protected]95409e12010-08-17 20:07:111923 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:501924 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151925 req.Start();
1926 MessageLoop::current()->Run();
1927
1928 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1929 == std::string::npos);
1930
[email protected]3dbb80b2010-02-09 22:41:201931 EXPECT_EQ(1, d.blocked_get_cookies_count());
1932 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151933 }
1934}
1935
1936TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
[email protected]7461a402011-03-24 23:19:511937 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
[email protected]95409e12010-08-17 20:07:111938 ASSERT_TRUE(test_server.Start());
1939
[email protected]34602282010-02-03 22:14:151940 // Set up a cookie.
1941 {
1942 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:501943 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d);
1944 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151945 req.Start();
1946 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201947
1948 EXPECT_EQ(0, d.blocked_get_cookies_count());
1949 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151950 }
1951
1952 // Try to set-up another cookie and update the previous cookie.
1953 {
[email protected]34602282010-02-03 22:14:151954 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:011955 d.set_cookie_options(TestDelegate::NO_SET_COOKIE);
[email protected]7461a402011-03-24 23:19:511956 URLRequest req(test_server.GetURL(
[email protected]34602282010-02-03 22:14:151957 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
[email protected]87a09a92011-07-14 15:50:501958 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151959 req.Start();
1960
1961 MessageLoop::current()->Run();
1962
[email protected]3dbb80b2010-02-09 22:41:201963 EXPECT_EQ(0, d.blocked_get_cookies_count());
1964 EXPECT_EQ(2, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151965 }
1966
1967 // Verify the cookies weren't saved or updated.
1968 {
1969 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111970 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:501971 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:151972 req.Start();
1973 MessageLoop::current()->Run();
1974
1975 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1976 == std::string::npos);
1977 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1978 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:201979
1980 EXPECT_EQ(0, d.blocked_get_cookies_count());
1981 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:151982 }
1983}
1984
[email protected]4f79b3f2010-02-05 04:27:471985TEST_F(URLRequestTest, CookiePolicy_ForceSession) {
[email protected]7461a402011-03-24 23:19:511986 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
[email protected]95409e12010-08-17 20:07:111987 ASSERT_TRUE(test_server.Start());
1988
[email protected]4f79b3f2010-02-05 04:27:471989 // Set up a cookie.
1990 {
1991 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:011992 d.set_cookie_options(TestDelegate::FORCE_SESSION);
[email protected]7461a402011-03-24 23:19:511993 URLRequest req(test_server.GetURL(
[email protected]4f79b3f2010-02-05 04:27:471994 "set-cookie?A=1;expires=\"Fri, 05 Feb 2010 23:42:01 GMT\""), &d);
[email protected]87a09a92011-07-14 15:50:501995 req.set_context(default_context_);
[email protected]4f79b3f2010-02-05 04:27:471996 req.Start(); // Triggers an asynchronous cookie policy check.
1997
1998 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:201999
2000 EXPECT_EQ(0, d.blocked_get_cookies_count());
2001 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]4f79b3f2010-02-05 04:27:472002 }
2003
2004 // Now, check the cookie store.
[email protected]7461a402011-03-24 23:19:512005 CookieList cookies =
[email protected]87a09a92011-07-14 15:50:502006 default_context_->cookie_store()->GetCookieMonster()->GetAllCookies();
[email protected]4f79b3f2010-02-05 04:27:472007 EXPECT_EQ(1U, cookies.size());
[email protected]65781e92010-07-21 15:29:572008 EXPECT_FALSE(cookies[0].IsPersistent());
[email protected]4f79b3f2010-02-05 04:27:472009}
2010
[email protected]71c64f62008-11-15 04:36:512011// In this test, we do a POST which the server will 302 redirect.
2012// The subsequent transaction should use GET, and should not send the
2013// Content-Type header.
2014// http://code.google.com/p/chromium/issues/detail?id=843
[email protected]37314622009-08-17 20:29:392015TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
[email protected]95409e12010-08-17 20:07:112016 ASSERT_TRUE(test_server_.Start());
2017
[email protected]195e77d2009-07-23 19:10:232018 const char kData[] = "hello world";
[email protected]95409e12010-08-17 20:07:112019
[email protected]71c64f62008-11-15 04:36:512020 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112021 TestURLRequest req(test_server_.GetURL("files/redirect-to-echoall"), &d);
[email protected]87a09a92011-07-14 15:50:502022 req.set_context(default_context_);
[email protected]71c64f62008-11-15 04:36:512023 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:232024 req.set_upload(CreateSimpleUploadData(kData));
[email protected]71c64f62008-11-15 04:36:512025
2026 // Set headers (some of which are specific to the POST).
[email protected]7461a402011-03-24 23:19:512027 HttpRequestHeaders headers;
[email protected]88e6b6f32010-05-07 23:14:252028 headers.AddHeadersFromString(
[email protected]dd265012009-01-08 20:45:272029 "Content-Type: multipart/form-data; "
2030 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
2031 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
2032 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
[email protected]71c64f62008-11-15 04:36:512033 "Accept-Language: en-US,en\r\n"
2034 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
[email protected]195e77d2009-07-23 19:10:232035 "Content-Length: 11\r\n"
[email protected]a86c97cc2009-06-24 21:26:272036 "Origin: http://localhost:1337/");
[email protected]88e6b6f32010-05-07 23:14:252037 req.SetExtraRequestHeaders(headers);
[email protected]71c64f62008-11-15 04:36:512038 req.Start();
2039 MessageLoop::current()->Run();
2040
2041 std::string mime_type;
2042 req.GetMimeType(&mime_type);
2043 EXPECT_EQ("text/html", mime_type);
2044
2045 const std::string& data = d.data_received();
2046
2047 // Check that the post-specific headers were stripped:
2048 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
2049 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
2050 EXPECT_FALSE(ContainsString(data, "Origin:"));
2051
2052 // These extra request headers should not have been stripped.
2053 EXPECT_TRUE(ContainsString(data, "Accept:"));
2054 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
2055 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
2056}
2057
[email protected]37314622009-08-17 20:29:392058TEST_F(URLRequestTestHTTP, Post307RedirectPost) {
[email protected]95409e12010-08-17 20:07:112059 ASSERT_TRUE(test_server_.Start());
2060
[email protected]195e77d2009-07-23 19:10:232061 const char kData[] = "hello world";
[email protected]95409e12010-08-17 20:07:112062
[email protected]140932f2008-12-12 03:58:062063 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:502064 TestURLRequest req(test_server_.GetURL("files/redirect307-to-echo"), &d);
2065 req.set_context(default_context_);
[email protected]140932f2008-12-12 03:58:062066 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:232067 req.set_upload(CreateSimpleUploadData(kData).get());
[email protected]7461a402011-03-24 23:19:512068 HttpRequestHeaders headers;
2069 headers.SetHeader(HttpRequestHeaders::kContentLength,
[email protected]528c56d2010-07-30 19:28:442070 base::UintToString(arraysize(kData) - 1));
[email protected]88e6b6f32010-05-07 23:14:252071 req.SetExtraRequestHeaders(headers);
[email protected]140932f2008-12-12 03:58:062072 req.Start();
2073 MessageLoop::current()->Run();
[email protected]195e77d2009-07-23 19:10:232074 EXPECT_EQ("POST", req.method());
2075 EXPECT_EQ(kData, d.data_received());
[email protected]140932f2008-12-12 03:58:062076}
[email protected]dd265012009-01-08 20:45:272077
[email protected]a5c713f2009-04-16 21:05:472078// Custom URLRequestJobs for use with interceptor tests
[email protected]7461a402011-03-24 23:19:512079class RestartTestJob : public URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:472080 public:
[email protected]7461a402011-03-24 23:19:512081 explicit RestartTestJob(URLRequest* request)
2082 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:472083 protected:
2084 virtual void StartAsync() {
2085 this->NotifyRestartRequired();
2086 }
[email protected]5389bc72009-11-05 23:34:242087 private:
[email protected]13c8a092010-07-29 06:15:442088 ~RestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:472089};
2090
[email protected]7461a402011-03-24 23:19:512091class CancelTestJob : public URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:472092 public:
[email protected]7461a402011-03-24 23:19:512093 explicit CancelTestJob(URLRequest* request)
2094 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:472095 protected:
2096 virtual void StartAsync() {
2097 request_->Cancel();
2098 }
[email protected]5389bc72009-11-05 23:34:242099 private:
2100 ~CancelTestJob() {}
[email protected]a5c713f2009-04-16 21:05:472101};
2102
[email protected]7461a402011-03-24 23:19:512103class CancelThenRestartTestJob : public URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:472104 public:
[email protected]7461a402011-03-24 23:19:512105 explicit CancelThenRestartTestJob(URLRequest* request)
2106 : URLRequestTestJob(request, true) {
[email protected]a5c713f2009-04-16 21:05:472107 }
2108 protected:
2109 virtual void StartAsync() {
2110 request_->Cancel();
2111 this->NotifyRestartRequired();
2112 }
[email protected]5389bc72009-11-05 23:34:242113 private:
2114 ~CancelThenRestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:472115};
2116
2117// An Interceptor for use with interceptor tests
[email protected]7461a402011-03-24 23:19:512118class TestInterceptor : URLRequest::Interceptor {
[email protected]a5c713f2009-04-16 21:05:472119 public:
2120 TestInterceptor()
2121 : intercept_main_request_(false), restart_main_request_(false),
2122 cancel_main_request_(false), cancel_then_restart_main_request_(false),
2123 simulate_main_network_error_(false),
2124 intercept_redirect_(false), cancel_redirect_request_(false),
2125 intercept_final_response_(false), cancel_final_request_(false),
2126 did_intercept_main_(false), did_restart_main_(false),
2127 did_cancel_main_(false), did_cancel_then_restart_main_(false),
2128 did_simulate_error_main_(false),
2129 did_intercept_redirect_(false), did_cancel_redirect_(false),
2130 did_intercept_final_(false), did_cancel_final_(false) {
[email protected]e3539402011-07-19 09:31:082131 URLRequest::Deprecated::RegisterRequestInterceptor(this);
[email protected]a5c713f2009-04-16 21:05:472132 }
2133
2134 ~TestInterceptor() {
[email protected]e3539402011-07-19 09:31:082135 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
[email protected]a5c713f2009-04-16 21:05:472136 }
2137
[email protected]7461a402011-03-24 23:19:512138 virtual URLRequestJob* MaybeIntercept(URLRequest* request) {
[email protected]a5c713f2009-04-16 21:05:472139 if (restart_main_request_) {
2140 restart_main_request_ = false;
2141 did_restart_main_ = true;
2142 return new RestartTestJob(request);
2143 }
2144 if (cancel_main_request_) {
2145 cancel_main_request_ = false;
2146 did_cancel_main_ = true;
2147 return new CancelTestJob(request);
2148 }
2149 if (cancel_then_restart_main_request_) {
2150 cancel_then_restart_main_request_ = false;
2151 did_cancel_then_restart_main_ = true;
2152 return new CancelThenRestartTestJob(request);
2153 }
2154 if (simulate_main_network_error_) {
2155 simulate_main_network_error_ = false;
2156 did_simulate_error_main_ = true;
2157 // will error since the requeted url is not one of its canned urls
[email protected]7461a402011-03-24 23:19:512158 return new URLRequestTestJob(request, true);
[email protected]a5c713f2009-04-16 21:05:472159 }
2160 if (!intercept_main_request_)
2161 return NULL;
2162 intercept_main_request_ = false;
2163 did_intercept_main_ = true;
[email protected]7461a402011-03-24 23:19:512164 return new URLRequestTestJob(request,
[email protected]cbe04ef2011-01-11 00:13:242165 main_headers_,
2166 main_data_,
2167 true);
[email protected]a5c713f2009-04-16 21:05:472168 }
2169
[email protected]7461a402011-03-24 23:19:512170 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
[email protected]9acd869e2010-12-11 10:18:592171 const GURL& location) {
[email protected]a5c713f2009-04-16 21:05:472172 if (cancel_redirect_request_) {
2173 cancel_redirect_request_ = false;
2174 did_cancel_redirect_ = true;
2175 return new CancelTestJob(request);
2176 }
2177 if (!intercept_redirect_)
2178 return NULL;
2179 intercept_redirect_ = false;
2180 did_intercept_redirect_ = true;
[email protected]7461a402011-03-24 23:19:512181 return new URLRequestTestJob(request,
[email protected]cbe04ef2011-01-11 00:13:242182 redirect_headers_,
2183 redirect_data_,
2184 true);
[email protected]a5c713f2009-04-16 21:05:472185 }
2186
[email protected]7461a402011-03-24 23:19:512187 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) {
[email protected]a5c713f2009-04-16 21:05:472188 if (cancel_final_request_) {
2189 cancel_final_request_ = false;
2190 did_cancel_final_ = true;
2191 return new CancelTestJob(request);
2192 }
2193 if (!intercept_final_response_)
2194 return NULL;
2195 intercept_final_response_ = false;
2196 did_intercept_final_ = true;
[email protected]7461a402011-03-24 23:19:512197 return new URLRequestTestJob(request,
[email protected]cbe04ef2011-01-11 00:13:242198 final_headers_,
2199 final_data_,
2200 true);
[email protected]a5c713f2009-04-16 21:05:472201 }
2202
2203 // Whether to intercept the main request, and if so the response to return.
2204 bool intercept_main_request_;
2205 std::string main_headers_;
2206 std::string main_data_;
2207
2208 // Other actions we take at MaybeIntercept time
2209 bool restart_main_request_;
2210 bool cancel_main_request_;
2211 bool cancel_then_restart_main_request_;
2212 bool simulate_main_network_error_;
2213
2214 // Whether to intercept redirects, and if so the response to return.
2215 bool intercept_redirect_;
2216 std::string redirect_headers_;
2217 std::string redirect_data_;
2218
2219 // Other actions we can take at MaybeInterceptRedirect time
2220 bool cancel_redirect_request_;
2221
2222 // Whether to intercept final response, and if so the response to return.
2223 bool intercept_final_response_;
2224 std::string final_headers_;
2225 std::string final_data_;
2226
2227 // Other actions we can take at MaybeInterceptResponse time
2228 bool cancel_final_request_;
2229
2230 // If we did something or not
2231 bool did_intercept_main_;
2232 bool did_restart_main_;
2233 bool did_cancel_main_;
2234 bool did_cancel_then_restart_main_;
2235 bool did_simulate_error_main_;
2236 bool did_intercept_redirect_;
2237 bool did_cancel_redirect_;
2238 bool did_intercept_final_;
2239 bool did_cancel_final_;
2240
2241 // Static getters for canned response header and data strings
2242
2243 static std::string ok_data() {
[email protected]7461a402011-03-24 23:19:512244 return URLRequestTestJob::test_data_1();
[email protected]a5c713f2009-04-16 21:05:472245 }
2246
2247 static std::string ok_headers() {
[email protected]7461a402011-03-24 23:19:512248 return URLRequestTestJob::test_headers();
[email protected]a5c713f2009-04-16 21:05:472249 }
2250
2251 static std::string redirect_data() {
2252 return std::string();
2253 }
2254
2255 static std::string redirect_headers() {
[email protected]7461a402011-03-24 23:19:512256 return URLRequestTestJob::test_redirect_headers();
[email protected]a5c713f2009-04-16 21:05:472257 }
2258
2259 static std::string error_data() {
2260 return std::string("ohhh nooooo mr. bill!");
2261 }
2262
2263 static std::string error_headers() {
[email protected]7461a402011-03-24 23:19:512264 return URLRequestTestJob::test_error_headers();
[email protected]a5c713f2009-04-16 21:05:472265 }
2266};
2267
2268TEST_F(URLRequestTest, Intercept) {
2269 TestInterceptor interceptor;
2270
2271 // intercept the main request and respond with a simple response
2272 interceptor.intercept_main_request_ = true;
2273 interceptor.main_headers_ = TestInterceptor::ok_headers();
2274 interceptor.main_data_ = TestInterceptor::ok_data();
2275
2276 TestDelegate d;
2277 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:502278 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:512279 URLRequest::UserData* user_data0 = new URLRequest::UserData();
2280 URLRequest::UserData* user_data1 = new URLRequest::UserData();
2281 URLRequest::UserData* user_data2 = new URLRequest::UserData();
[email protected]a5c713f2009-04-16 21:05:472282 req.SetUserData(NULL, user_data0);
2283 req.SetUserData(&user_data1, user_data1);
2284 req.SetUserData(&user_data2, user_data2);
2285 req.set_method("GET");
2286 req.Start();
2287 MessageLoop::current()->Run();
2288
2289 // Make sure we can retrieve our specific user data
2290 EXPECT_EQ(user_data0, req.GetUserData(NULL));
2291 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
2292 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
2293
2294 // Check the interceptor got called as expected
2295 EXPECT_TRUE(interceptor.did_intercept_main_);
2296
2297 // Check we got one good response
2298 EXPECT_TRUE(req.status().is_success());
2299 EXPECT_EQ(200, req.response_headers()->response_code());
2300 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2301 EXPECT_EQ(1, d.response_started_count());
2302 EXPECT_EQ(0, d.received_redirect_count());
2303}
2304
2305TEST_F(URLRequestTest, InterceptRedirect) {
2306 TestInterceptor interceptor;
2307
2308 // intercept the main request and respond with a redirect
2309 interceptor.intercept_main_request_ = true;
2310 interceptor.main_headers_ = TestInterceptor::redirect_headers();
2311 interceptor.main_data_ = TestInterceptor::redirect_data();
2312
2313 // intercept that redirect and respond a final OK response
2314 interceptor.intercept_redirect_ = true;
2315 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
2316 interceptor.redirect_data_ = TestInterceptor::ok_data();
2317
2318 TestDelegate d;
2319 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:502320 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:472321 req.set_method("GET");
2322 req.Start();
2323 MessageLoop::current()->Run();
2324
2325 // Check the interceptor got called as expected
2326 EXPECT_TRUE(interceptor.did_intercept_main_);
2327 EXPECT_TRUE(interceptor.did_intercept_redirect_);
2328
2329 // Check we got one good response
2330 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:112331 if (req.status().is_success()) {
2332 EXPECT_EQ(200, req.response_headers()->response_code());
2333 }
[email protected]a5c713f2009-04-16 21:05:472334 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2335 EXPECT_EQ(1, d.response_started_count());
2336 EXPECT_EQ(0, d.received_redirect_count());
2337}
2338
2339TEST_F(URLRequestTest, InterceptServerError) {
2340 TestInterceptor interceptor;
2341
2342 // intercept the main request to generate a server error response
2343 interceptor.intercept_main_request_ = true;
2344 interceptor.main_headers_ = TestInterceptor::error_headers();
2345 interceptor.main_data_ = TestInterceptor::error_data();
2346
2347 // intercept that error and respond with an OK response
2348 interceptor.intercept_final_response_ = true;
2349 interceptor.final_headers_ = TestInterceptor::ok_headers();
2350 interceptor.final_data_ = TestInterceptor::ok_data();
2351
2352 TestDelegate d;
2353 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:502354 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:472355 req.set_method("GET");
2356 req.Start();
2357 MessageLoop::current()->Run();
2358
2359 // Check the interceptor got called as expected
2360 EXPECT_TRUE(interceptor.did_intercept_main_);
2361 EXPECT_TRUE(interceptor.did_intercept_final_);
2362
2363 // Check we got one good response
2364 EXPECT_TRUE(req.status().is_success());
2365 EXPECT_EQ(200, req.response_headers()->response_code());
2366 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2367 EXPECT_EQ(1, d.response_started_count());
2368 EXPECT_EQ(0, d.received_redirect_count());
2369}
2370
2371TEST_F(URLRequestTest, InterceptNetworkError) {
2372 TestInterceptor interceptor;
2373
2374 // intercept the main request to simulate a network error
2375 interceptor.simulate_main_network_error_ = true;
2376
2377 // intercept that error and respond with an OK response
2378 interceptor.intercept_final_response_ = true;
2379 interceptor.final_headers_ = TestInterceptor::ok_headers();
2380 interceptor.final_data_ = TestInterceptor::ok_data();
2381
2382 TestDelegate d;
2383 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:502384 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:472385 req.set_method("GET");
2386 req.Start();
2387 MessageLoop::current()->Run();
2388
2389 // Check the interceptor got called as expected
2390 EXPECT_TRUE(interceptor.did_simulate_error_main_);
2391 EXPECT_TRUE(interceptor.did_intercept_final_);
2392
2393 // Check we received one good response
2394 EXPECT_TRUE(req.status().is_success());
2395 EXPECT_EQ(200, req.response_headers()->response_code());
2396 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2397 EXPECT_EQ(1, d.response_started_count());
2398 EXPECT_EQ(0, d.received_redirect_count());
2399}
2400
2401TEST_F(URLRequestTest, InterceptRestartRequired) {
2402 TestInterceptor interceptor;
2403
2404 // restart the main request
2405 interceptor.restart_main_request_ = true;
2406
2407 // then intercept the new main request and respond with an OK response
2408 interceptor.intercept_main_request_ = true;
2409 interceptor.main_headers_ = TestInterceptor::ok_headers();
2410 interceptor.main_data_ = TestInterceptor::ok_data();
2411
2412 TestDelegate d;
2413 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:502414 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:472415 req.set_method("GET");
2416 req.Start();
2417 MessageLoop::current()->Run();
2418
2419 // Check the interceptor got called as expected
2420 EXPECT_TRUE(interceptor.did_restart_main_);
2421 EXPECT_TRUE(interceptor.did_intercept_main_);
2422
2423 // Check we received one good response
2424 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:112425 if (req.status().is_success()) {
2426 EXPECT_EQ(200, req.response_headers()->response_code());
2427 }
[email protected]a5c713f2009-04-16 21:05:472428 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
2429 EXPECT_EQ(1, d.response_started_count());
2430 EXPECT_EQ(0, d.received_redirect_count());
2431}
2432
2433TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
2434 TestInterceptor interceptor;
2435
2436 // intercept the main request and cancel from within the restarted job
2437 interceptor.cancel_main_request_ = true;
2438
2439 // setup to intercept final response and override it with an OK response
2440 interceptor.intercept_final_response_ = true;
2441 interceptor.final_headers_ = TestInterceptor::ok_headers();
2442 interceptor.final_data_ = TestInterceptor::ok_data();
2443
2444 TestDelegate d;
2445 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:502446 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:472447 req.set_method("GET");
2448 req.Start();
2449 MessageLoop::current()->Run();
2450
2451 // Check the interceptor got called as expected
2452 EXPECT_TRUE(interceptor.did_cancel_main_);
2453 EXPECT_FALSE(interceptor.did_intercept_final_);
2454
2455 // Check we see a canceled request
2456 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:512457 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:472458}
2459
2460TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
2461 TestInterceptor interceptor;
2462
2463 // intercept the main request and respond with a redirect
2464 interceptor.intercept_main_request_ = true;
2465 interceptor.main_headers_ = TestInterceptor::redirect_headers();
2466 interceptor.main_data_ = TestInterceptor::redirect_data();
2467
2468 // intercept the redirect and cancel from within that job
2469 interceptor.cancel_redirect_request_ = true;
2470
2471 // setup to intercept final response and override it with an OK response
2472 interceptor.intercept_final_response_ = true;
2473 interceptor.final_headers_ = TestInterceptor::ok_headers();
2474 interceptor.final_data_ = TestInterceptor::ok_data();
2475
2476 TestDelegate d;
2477 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:502478 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:472479 req.set_method("GET");
2480 req.Start();
2481 MessageLoop::current()->Run();
2482
2483 // Check the interceptor got called as expected
2484 EXPECT_TRUE(interceptor.did_intercept_main_);
2485 EXPECT_TRUE(interceptor.did_cancel_redirect_);
2486 EXPECT_FALSE(interceptor.did_intercept_final_);
2487
2488 // Check we see a canceled request
2489 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:512490 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:472491}
2492
2493TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
2494 TestInterceptor interceptor;
2495
2496 // intercept the main request to simulate a network error
2497 interceptor.simulate_main_network_error_ = true;
2498
2499 // setup to intercept final response and cancel from within that job
2500 interceptor.cancel_final_request_ = true;
2501
2502 TestDelegate d;
2503 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:502504 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:472505 req.set_method("GET");
2506 req.Start();
2507 MessageLoop::current()->Run();
2508
2509 // Check the interceptor got called as expected
2510 EXPECT_TRUE(interceptor.did_simulate_error_main_);
2511 EXPECT_TRUE(interceptor.did_cancel_final_);
2512
2513 // Check we see a canceled request
2514 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:512515 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:472516}
2517
2518TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
2519 TestInterceptor interceptor;
2520
2521 // intercept the main request and cancel then restart from within that job
2522 interceptor.cancel_then_restart_main_request_ = true;
2523
2524 // setup to intercept final response and override it with an OK response
2525 interceptor.intercept_final_response_ = true;
2526 interceptor.final_headers_ = TestInterceptor::ok_headers();
2527 interceptor.final_data_ = TestInterceptor::ok_data();
2528
2529 TestDelegate d;
2530 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:502531 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:472532 req.set_method("GET");
2533 req.Start();
2534 MessageLoop::current()->Run();
2535
2536 // Check the interceptor got called as expected
2537 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
2538 EXPECT_FALSE(interceptor.did_intercept_final_);
2539
2540 // Check we see a canceled request
2541 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:512542 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:472543}
2544
[email protected]f4f2f512011-02-17 09:28:452545// Check that two different URL requests have different identifiers.
2546TEST_F(URLRequestTest, Identifiers) {
2547 TestDelegate d;
2548 TestURLRequest req(GURL("http://example.com"), &d);
2549 TestURLRequest other_req(GURL("http://example.com"), &d);
2550
2551 ASSERT_NE(req.identifier(), other_req.identifier());
2552}
2553
[email protected]8202d0c2011-02-23 08:31:142554// Check that a failure to connect to the proxy is reported to the network
2555// delegate.
2556TEST_F(URLRequestTest, NetworkDelegateProxyError) {
[email protected]e74aa632011-07-14 17:10:162557 MockHostResolver host_resolver;
2558 host_resolver.rules()->AddSimulatedFailure("*");
[email protected]87a09a92011-07-14 15:50:502559
2560 TestNetworkDelegate network_delegate; // must outlive URLRequests
2561 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
[email protected]8202d0c2011-02-23 08:31:142562 context->set_network_delegate(&network_delegate);
[email protected]87a09a92011-07-14 15:50:502563 context->SetProxyFromString("myproxy:70");
[email protected]e74aa632011-07-14 17:10:162564 context->set_host_resolver(&host_resolver);
[email protected]87a09a92011-07-14 15:50:502565 context->Init();
2566
2567 TestDelegate d;
2568 TestURLRequest req(GURL("http://example.com"), &d);
[email protected]8202d0c2011-02-23 08:31:142569 req.set_context(context);
[email protected]87a09a92011-07-14 15:50:502570 req.set_method("GET");
[email protected]8202d0c2011-02-23 08:31:142571
2572 req.Start();
2573 MessageLoop::current()->Run();
2574
2575 // Check we see a failed request.
2576 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:512577 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
2578 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().os_error());
[email protected]8202d0c2011-02-23 08:31:142579
2580 EXPECT_EQ(1, network_delegate.error_count());
[email protected]7461a402011-03-24 23:19:512581 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_os_error());
[email protected]a83dd332011-07-13 10:41:012582 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]8202d0c2011-02-23 08:31:142583}
2584
[email protected]c10450102011-06-27 09:06:162585// Check that it is impossible to change the referrer in the extra headers of
2586// an URLRequest.
2587TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2588 TestServer test_server(TestServer::TYPE_HTTP, FilePath());
2589 ASSERT_TRUE(test_server.Start());
2590
[email protected]c10450102011-06-27 09:06:162591 // If extra headers contain referer and the request contains a referer,
2592 // only the latter shall be respected.
2593 {
2594 TestDelegate d;
2595 TestURLRequest req(test_server.GetURL("echoheader?Referer"), &d);
2596 req.set_referrer("http://foo.com/");
[email protected]87a09a92011-07-14 15:50:502597 req.set_context(default_context_);
[email protected]c10450102011-06-27 09:06:162598
2599 HttpRequestHeaders headers;
2600 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2601 req.SetExtraRequestHeaders(headers);
2602
2603 req.Start();
2604 MessageLoop::current()->Run();
2605
2606 EXPECT_EQ("http://foo.com/", d.data_received());
2607 }
2608
2609 // If extra headers contain a referer but the request does not, no referer
2610 // shall be sent in the header.
2611 {
2612 TestDelegate d;
2613 TestURLRequest req(test_server.GetURL("echoheader?Referer"), &d);
[email protected]87a09a92011-07-14 15:50:502614 req.set_context(default_context_);
[email protected]c10450102011-06-27 09:06:162615
2616 HttpRequestHeaders headers;
2617 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2618 req.SetExtraRequestHeaders(headers);
[email protected]52c287592011-06-28 17:39:102619 req.set_load_flags(LOAD_VALIDATE_CACHE);
[email protected]c10450102011-06-27 09:06:162620
2621 req.Start();
2622 MessageLoop::current()->Run();
2623
2624 EXPECT_EQ("None", d.data_received());
2625 }
2626}
2627
[email protected]a83dd332011-07-13 10:41:012628// Make sure that net::NetworkDelegate::NotifyCompleted is called if
2629// content is empty.
2630TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
[email protected]a83dd332011-07-13 10:41:012631 TestDelegate d;
2632 TestURLRequest req(GURL("data:,"), &d);
[email protected]87a09a92011-07-14 15:50:502633 req.set_context(new TestURLRequestContext());
2634 req.set_context(default_context_);
[email protected]a83dd332011-07-13 10:41:012635 req.Start();
2636 MessageLoop::current()->Run();
2637 EXPECT_EQ("", d.data_received());
[email protected]87a09a92011-07-14 15:50:502638 EXPECT_EQ(1, default_network_delegate_.completed_requests());
[email protected]a83dd332011-07-13 10:41:012639}
2640
[email protected]b89ca032009-08-31 21:41:312641class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:112642 public:
[email protected]7461a402011-03-24 23:19:512643 URLRequestTestFTP() : test_server_(TestServer::TYPE_FTP, FilePath()) {
[email protected]95409e12010-08-17 20:07:112644 }
2645
[email protected]b89ca032009-08-31 21:41:312646 protected:
[email protected]7461a402011-03-24 23:19:512647 TestServer test_server_;
[email protected]b89ca032009-08-31 21:41:312648};
2649
[email protected]5accf7332009-11-24 03:41:382650// Flaky, see http://crbug.com/25045.
2651TEST_F(URLRequestTestFTP, FLAKY_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:112652 ASSERT_TRUE(test_server_.Start());
2653
[email protected]a25e90e2009-09-09 17:05:372654 TestDelegate d;
2655 {
[email protected]95409e12010-08-17 20:07:112656 TestURLRequest r(test_server_.GetURL("/"), &d);
[email protected]87a09a92011-07-14 15:50:502657 r.set_context(default_context_);
[email protected]a25e90e2009-09-09 17:05:372658 r.Start();
2659 EXPECT_TRUE(r.is_pending());
2660
2661 MessageLoop::current()->Run();
2662
2663 EXPECT_FALSE(r.is_pending());
2664 EXPECT_EQ(1, d.response_started_count());
2665 EXPECT_FALSE(d.received_data_before_response());
2666 EXPECT_LT(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:192667 EXPECT_EQ(test_server_.host_port_pair().host(),
2668 r.GetSocketAddress().host());
2669 EXPECT_EQ(test_server_.host_port_pair().port(),
2670 r.GetSocketAddress().port());
[email protected]a25e90e2009-09-09 17:05:372671 }
2672}
2673
[email protected]7df70012010-02-04 00:09:552674// Flaky, see http://crbug.com/25045.
2675TEST_F(URLRequestTestFTP, FLAKY_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:112676 ASSERT_TRUE(test_server_.Start());
2677
[email protected]399b8702009-05-01 20:34:022678 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272679 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022680 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272681 TestDelegate d;
2682 {
[email protected]95409e12010-08-17 20:07:112683 TestURLRequest r(test_server_.GetURL("/LICENSE"), &d);
[email protected]87a09a92011-07-14 15:50:502684 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:272685 r.Start();
2686 EXPECT_TRUE(r.is_pending());
2687
2688 MessageLoop::current()->Run();
2689
2690 int64 file_size = 0;
2691 file_util::GetFileSize(app_path, &file_size);
2692
[email protected]ba2f3342009-07-30 18:08:422693 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272694 EXPECT_EQ(1, d.response_started_count());
2695 EXPECT_FALSE(d.received_data_before_response());
2696 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:192697 EXPECT_EQ(test_server_.host_port_pair().host(),
2698 r.GetSocketAddress().host());
2699 EXPECT_EQ(test_server_.host_port_pair().port(),
2700 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:272701 }
2702}
2703
[email protected]e9ecbd12009-12-20 18:44:402704// Flaky, see http://crbug.com/25045.
2705TEST_F(URLRequestTestFTP, FLAKY_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:112706 ASSERT_TRUE(test_server_.Start());
2707
[email protected]399b8702009-05-01 20:34:022708 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272709 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022710 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272711 TestDelegate d;
2712 {
[email protected]95409e12010-08-17 20:07:112713 TestURLRequest r(
2714 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
2715 &d);
[email protected]87a09a92011-07-14 15:50:502716 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:272717 r.Start();
2718 EXPECT_TRUE(r.is_pending());
2719
2720 MessageLoop::current()->Run();
2721
2722 int64 file_size = 0;
2723 file_util::GetFileSize(app_path, &file_size);
2724
[email protected]ba2f3342009-07-30 18:08:422725 EXPECT_FALSE(r.is_pending());
[email protected]6d81b482011-02-22 19:47:192726 EXPECT_EQ(test_server_.host_port_pair().host(),
2727 r.GetSocketAddress().host());
2728 EXPECT_EQ(test_server_.host_port_pair().port(),
2729 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:272730 EXPECT_EQ(1, d.response_started_count());
2731 EXPECT_FALSE(d.received_data_before_response());
2732 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2733 }
2734}
2735
[email protected]49abd652010-08-05 05:04:532736// Flaky, see http://crbug.com/25045.
2737TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPassword) {
[email protected]95409e12010-08-17 20:07:112738 ASSERT_TRUE(test_server_.Start());
2739
[email protected]399b8702009-05-01 20:34:022740 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272741 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022742 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272743 TestDelegate d;
2744 {
[email protected]95409e12010-08-17 20:07:112745 TestURLRequest r(
2746 test_server_.GetURLWithUserAndPassword("/LICENSE",
2747 "chrome",
2748 "wrong_password"),
2749 &d);
[email protected]87a09a92011-07-14 15:50:502750 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:272751 r.Start();
2752 EXPECT_TRUE(r.is_pending());
2753
2754 MessageLoop::current()->Run();
2755
2756 int64 file_size = 0;
2757 file_util::GetFileSize(app_path, &file_size);
2758
[email protected]ba2f3342009-07-30 18:08:422759 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272760 EXPECT_EQ(1, d.response_started_count());
2761 EXPECT_FALSE(d.received_data_before_response());
2762 EXPECT_EQ(d.bytes_received(), 0);
2763 }
2764}
2765
[email protected]cde4e80d2009-10-16 19:58:152766// Flaky, see http://crbug.com/25045.
2767TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:112768 ASSERT_TRUE(test_server_.Start());
2769
[email protected]8b8a197d2009-08-26 15:57:582770 FilePath app_path;
2771 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2772 app_path = app_path.AppendASCII("LICENSE");
2773 TestDelegate d;
2774 // Set correct login credentials. The delegate will be asked for them when
2775 // the initial login with wrong credentials will fail.
[email protected]13c8a092010-07-29 06:15:442776 d.set_username(kChrome);
2777 d.set_password(kChrome);
[email protected]8b8a197d2009-08-26 15:57:582778 {
[email protected]95409e12010-08-17 20:07:112779 TestURLRequest r(
2780 test_server_.GetURLWithUserAndPassword("/LICENSE",
2781 "chrome",
2782 "wrong_password"),
2783 &d);
[email protected]87a09a92011-07-14 15:50:502784 r.set_context(default_context_);
[email protected]8b8a197d2009-08-26 15:57:582785 r.Start();
2786 EXPECT_TRUE(r.is_pending());
2787
2788 MessageLoop::current()->Run();
2789
2790 int64 file_size = 0;
2791 file_util::GetFileSize(app_path, &file_size);
2792
2793 EXPECT_FALSE(r.is_pending());
2794 EXPECT_EQ(1, d.response_started_count());
2795 EXPECT_FALSE(d.received_data_before_response());
2796 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2797 }
2798}
2799
[email protected]49abd652010-08-05 05:04:532800// Flaky, see http://crbug.com/25045.
2801TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:112802 ASSERT_TRUE(test_server_.Start());
2803
[email protected]399b8702009-05-01 20:34:022804 FilePath app_path;
[email protected]dd265012009-01-08 20:45:272805 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022806 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:272807 TestDelegate d;
2808 {
[email protected]95409e12010-08-17 20:07:112809 TestURLRequest r(
2810 test_server_.GetURLWithUserAndPassword("/LICENSE",
2811 "wrong_user",
2812 "chrome"),
2813 &d);
[email protected]87a09a92011-07-14 15:50:502814 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:272815 r.Start();
2816 EXPECT_TRUE(r.is_pending());
2817
2818 MessageLoop::current()->Run();
2819
2820 int64 file_size = 0;
2821 file_util::GetFileSize(app_path, &file_size);
2822
[email protected]ba2f3342009-07-30 18:08:422823 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:272824 EXPECT_EQ(1, d.response_started_count());
2825 EXPECT_FALSE(d.received_data_before_response());
2826 EXPECT_EQ(d.bytes_received(), 0);
2827 }
2828}
[email protected]8b8a197d2009-08-26 15:57:582829
[email protected]cde4e80d2009-10-16 19:58:152830// Flaky, see http://crbug.com/25045.
2831TEST_F(URLRequestTestFTP, FLAKY_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:112832 ASSERT_TRUE(test_server_.Start());
2833
[email protected]8b8a197d2009-08-26 15:57:582834 FilePath app_path;
2835 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2836 app_path = app_path.AppendASCII("LICENSE");
2837 TestDelegate d;
2838 // Set correct login credentials. The delegate will be asked for them when
2839 // the initial login with wrong credentials will fail.
[email protected]13c8a092010-07-29 06:15:442840 d.set_username(kChrome);
2841 d.set_password(kChrome);
[email protected]8b8a197d2009-08-26 15:57:582842 {
[email protected]95409e12010-08-17 20:07:112843 TestURLRequest r(
2844 test_server_.GetURLWithUserAndPassword("/LICENSE",
2845 "wrong_user",
2846 "chrome"),
2847 &d);
[email protected]87a09a92011-07-14 15:50:502848 r.set_context(default_context_);
[email protected]8b8a197d2009-08-26 15:57:582849 r.Start();
2850 EXPECT_TRUE(r.is_pending());
2851
2852 MessageLoop::current()->Run();
2853
2854 int64 file_size = 0;
2855 file_util::GetFileSize(app_path, &file_size);
2856
2857 EXPECT_FALSE(r.is_pending());
2858 EXPECT_EQ(1, d.response_started_count());
2859 EXPECT_FALSE(d.received_data_before_response());
2860 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
2861 }
2862}
[email protected]60a3df52009-09-22 16:13:242863
[email protected]cde4e80d2009-10-16 19:58:152864// Flaky, see http://crbug.com/25045.
2865TEST_F(URLRequestTestFTP, FLAKY_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:112866 ASSERT_TRUE(test_server_.Start());
2867
[email protected]60a3df52009-09-22 16:13:242868 FilePath app_path;
2869 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2870 app_path = app_path.AppendASCII("LICENSE");
2871
2872 scoped_ptr<TestDelegate> d(new TestDelegate);
2873 {
2874 // Pass correct login identity in the URL.
[email protected]95409e12010-08-17 20:07:112875 TestURLRequest r(
2876 test_server_.GetURLWithUserAndPassword("/LICENSE",
2877 "chrome",
2878 "chrome"),
2879 d.get());
[email protected]87a09a92011-07-14 15:50:502880 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:242881 r.Start();
2882 EXPECT_TRUE(r.is_pending());
2883
2884 MessageLoop::current()->Run();
2885
2886 int64 file_size = 0;
2887 file_util::GetFileSize(app_path, &file_size);
2888
2889 EXPECT_FALSE(r.is_pending());
2890 EXPECT_EQ(1, d->response_started_count());
2891 EXPECT_FALSE(d->received_data_before_response());
2892 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2893 }
2894
2895 d.reset(new TestDelegate);
2896 {
2897 // This request should use cached identity from previous request.
[email protected]95409e12010-08-17 20:07:112898 TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get());
[email protected]87a09a92011-07-14 15:50:502899 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:242900 r.Start();
2901 EXPECT_TRUE(r.is_pending());
2902
2903 MessageLoop::current()->Run();
2904
2905 int64 file_size = 0;
2906 file_util::GetFileSize(app_path, &file_size);
2907
2908 EXPECT_FALSE(r.is_pending());
2909 EXPECT_EQ(1, d->response_started_count());
2910 EXPECT_FALSE(d->received_data_before_response());
2911 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2912 }
2913}
2914
[email protected]cde4e80d2009-10-16 19:58:152915// Flaky, see http://crbug.com/25045.
2916TEST_F(URLRequestTestFTP, FLAKY_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:112917 ASSERT_TRUE(test_server_.Start());
2918
[email protected]60a3df52009-09-22 16:13:242919 FilePath app_path;
2920 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
2921 app_path = app_path.AppendASCII("LICENSE");
2922
2923 scoped_ptr<TestDelegate> d(new TestDelegate);
2924 // Set correct login credentials. The delegate will be asked for them when
2925 // the initial login with wrong credentials will fail.
[email protected]13c8a092010-07-29 06:15:442926 d->set_username(kChrome);
2927 d->set_password(kChrome);
[email protected]60a3df52009-09-22 16:13:242928 {
[email protected]95409e12010-08-17 20:07:112929 TestURLRequest r(
2930 test_server_.GetURLWithUserAndPassword("/LICENSE",
2931 "chrome",
2932 "wrong_password"),
2933 d.get());
[email protected]87a09a92011-07-14 15:50:502934 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:242935 r.Start();
2936 EXPECT_TRUE(r.is_pending());
2937
2938 MessageLoop::current()->Run();
2939
2940 int64 file_size = 0;
2941 file_util::GetFileSize(app_path, &file_size);
2942
2943 EXPECT_FALSE(r.is_pending());
2944 EXPECT_EQ(1, d->response_started_count());
2945 EXPECT_FALSE(d->received_data_before_response());
2946 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2947 }
2948
2949 // Use a new delegate without explicit credentials. The cached ones should be
2950 // used.
2951 d.reset(new TestDelegate);
2952 {
2953 // Don't pass wrong credentials in the URL, they would override valid cached
2954 // ones.
[email protected]95409e12010-08-17 20:07:112955 TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get());
[email protected]87a09a92011-07-14 15:50:502956 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:242957 r.Start();
2958 EXPECT_TRUE(r.is_pending());
2959
2960 MessageLoop::current()->Run();
2961
2962 int64 file_size = 0;
2963 file_util::GetFileSize(app_path, &file_size);
2964
2965 EXPECT_FALSE(r.is_pending());
2966 EXPECT_EQ(1, d->response_started_count());
2967 EXPECT_FALSE(d->received_data_before_response());
2968 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
2969 }
2970}
[email protected]09a362d32009-09-24 18:01:332971
2972// Check that default A-L header is sent.
2973TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
[email protected]95409e12010-08-17 20:07:112974 ASSERT_TRUE(test_server_.Start());
2975
[email protected]87a09a92011-07-14 15:50:502976 TestNetworkDelegate network_delegate; // must outlive URLRequests
2977 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
2978 context->set_network_delegate(&network_delegate);
2979 context->set_accept_language("en");
2980 context->Init();
2981
[email protected]09a362d32009-09-24 18:01:332982 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112983 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
[email protected]6dae6b82011-03-30 00:35:342984 req.set_context(context);
[email protected]09a362d32009-09-24 18:01:332985 req.Start();
2986 MessageLoop::current()->Run();
[email protected]6dae6b82011-03-30 00:35:342987 EXPECT_EQ("en", d.data_received());
2988}
2989
2990// Check that an empty A-L header is not sent. http://crbug.com/77365.
2991TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
2992 ASSERT_TRUE(test_server_.Start());
2993
[email protected]87a09a92011-07-14 15:50:502994 TestNetworkDelegate network_delegate; // must outlive URLRequests
2995 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
2996 context->set_network_delegate(&network_delegate);
2997 context->Init();
2998 // We override the language after initialization because empty entries
2999 // get overridden by Init().
[email protected]6dae6b82011-03-30 00:35:343000 context->set_accept_language("");
[email protected]87a09a92011-07-14 15:50:503001
3002 TestDelegate d;
3003 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
[email protected]6dae6b82011-03-30 00:35:343004 req.set_context(context);
3005 req.Start();
3006 MessageLoop::current()->Run();
3007 EXPECT_EQ("None", d.data_received());
[email protected]09a362d32009-09-24 18:01:333008}
3009
3010// Check that if request overrides the A-L header, the default is not appended.
3011// See http://crbug.com/20894
3012TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
[email protected]95409e12010-08-17 20:07:113013 ASSERT_TRUE(test_server_.Start());
3014
[email protected]09a362d32009-09-24 18:01:333015 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:503016 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
3017 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:513018 HttpRequestHeaders headers;
3019 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
[email protected]88e6b6f32010-05-07 23:14:253020 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:333021 req.Start();
3022 MessageLoop::current()->Run();
3023 EXPECT_EQ(std::string("ru"), d.data_received());
3024}
3025
[email protected]c7bef94c2011-06-21 18:05:513026// Check that default A-E header is sent.
3027TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
3028 ASSERT_TRUE(test_server_.Start());
3029
3030 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:503031 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), &d);
3032 req.set_context(default_context_);
[email protected]c7bef94c2011-06-21 18:05:513033 HttpRequestHeaders headers;
3034 req.SetExtraRequestHeaders(headers);
3035 req.Start();
3036 MessageLoop::current()->Run();
3037 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
3038}
3039
3040// Check that if request overrides the A-E header, the default is not appended.
3041// See http://crbug.com/47381
3042TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
3043 ASSERT_TRUE(test_server_.Start());
3044
3045 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:503046 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), &d);
3047 req.set_context(default_context_);
[email protected]c7bef94c2011-06-21 18:05:513048 HttpRequestHeaders headers;
3049 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
3050 req.SetExtraRequestHeaders(headers);
3051 req.Start();
3052 MessageLoop::current()->Run();
3053 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
3054 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
3055}
3056
[email protected]09a362d32009-09-24 18:01:333057// Check that default A-C header is sent.
3058TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) {
[email protected]95409e12010-08-17 20:07:113059 ASSERT_TRUE(test_server_.Start());
3060
[email protected]87a09a92011-07-14 15:50:503061 TestNetworkDelegate network_delegate; // must outlive URLRequests
3062 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
3063 context->set_network_delegate(&network_delegate);
3064 context->set_accept_charset("en");
3065 context->Init();
3066
[email protected]09a362d32009-09-24 18:01:333067 TestDelegate d;
[email protected]95409e12010-08-17 20:07:113068 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
[email protected]6dae6b82011-03-30 00:35:343069 req.set_context(context);
[email protected]09a362d32009-09-24 18:01:333070 req.Start();
3071 MessageLoop::current()->Run();
[email protected]6dae6b82011-03-30 00:35:343072 EXPECT_EQ("en", d.data_received());
3073}
3074
3075// Check that an empty A-C header is not sent. http://crbug.com/77365.
3076TEST_F(URLRequestTestHTTP, EmptyAcceptCharset) {
3077 ASSERT_TRUE(test_server_.Start());
3078
[email protected]87a09a92011-07-14 15:50:503079 TestNetworkDelegate network_delegate; // must outlive URLRequests
3080 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
3081 context->set_network_delegate(&network_delegate);
3082 context->Init();
3083 // We override the accepted charset after initialization because empty
3084 // entries get overridden otherwise.
[email protected]6dae6b82011-03-30 00:35:343085 context->set_accept_charset("");
[email protected]87a09a92011-07-14 15:50:503086
3087 TestDelegate d;
3088 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
[email protected]6dae6b82011-03-30 00:35:343089 req.set_context(context);
3090 req.Start();
3091 MessageLoop::current()->Run();
3092 EXPECT_EQ("None", d.data_received());
[email protected]09a362d32009-09-24 18:01:333093}
3094
3095// Check that if request overrides the A-C header, the default is not appended.
3096// See http://crbug.com/20894
3097TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) {
[email protected]95409e12010-08-17 20:07:113098 ASSERT_TRUE(test_server_.Start());
3099
[email protected]09a362d32009-09-24 18:01:333100 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:503101 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
3102 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:513103 HttpRequestHeaders headers;
3104 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
[email protected]88e6b6f32010-05-07 23:14:253105 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:333106 req.Start();
3107 MessageLoop::current()->Run();
3108 EXPECT_EQ(std::string("koi-8r"), d.data_received());
3109}
[email protected]50ba404b2011-01-21 13:38:453110
3111// Check that default User-Agent header is sent.
3112TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
3113 ASSERT_TRUE(test_server_.Start());
3114
3115 TestDelegate d;
3116 TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d);
[email protected]87a09a92011-07-14 15:50:503117 req.set_context(default_context_);
[email protected]50ba404b2011-01-21 13:38:453118 req.Start();
3119 MessageLoop::current()->Run();
3120 EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received());
3121}
3122
3123// Check that if request overrides the User-Agent header,
3124// the default is not appended.
3125TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
3126 ASSERT_TRUE(test_server_.Start());
3127
3128 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:503129 TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d);
3130 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:513131 HttpRequestHeaders headers;
3132 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
[email protected]50ba404b2011-01-21 13:38:453133 req.SetExtraRequestHeaders(headers);
3134 req.Start();
3135 MessageLoop::current()->Run();
[email protected]ebbad602011-01-21 23:30:473136 // If the net tests are being run with ChromeFrame then we need to allow for
[email protected]0464e16a2011-02-04 22:56:573137 // the 'chromeframe' suffix which is added to the user agent before the
3138 // closing parentheses.
3139 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true));
[email protected]50ba404b2011-01-21 13:38:453140}
[email protected]7461a402011-03-24 23:19:513141
3142} // namespace net