blob: fbfaafef461460ea7c26af69b25e729a3462c5b3 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]9396b252008-09-29 17:29:385#include "net/url_request/url_request_unittest.h"
6
[email protected]ea224582008-12-07 20:25:467#include "build/build_config.h"
8
[email protected]9396b252008-09-29 17:29:389#if defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:5210#include <windows.h>
11#include <shlobj.h>
[email protected]ea224582008-12-07 20:25:4612#elif defined(OS_LINUX)
13#include "base/nss_init.h"
[email protected]9396b252008-09-29 17:29:3814#endif
15
initial.commit586acc5fe2008-07-26 22:42:5216#include <algorithm>
17#include <string>
18
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]73f5d662008-11-20 01:08:1722#include "base/string_piece.h"
initial.commit586acc5fe2008-07-26 22:42:5223#include "base/string_util.h"
[email protected]0757e7702009-03-27 04:00:2224#include "net/base/cookie_monster.h"
initial.commit586acc5fe2008-07-26 22:42:5225#include "net/base/load_flags.h"
26#include "net/base/net_errors.h"
27#include "net/base/net_module.h"
28#include "net/base/net_util.h"
[email protected]ea224582008-12-07 20:25:4629#include "net/base/ssl_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5230#include "net/disk_cache/disk_cache.h"
31#include "net/http/http_cache.h"
32#include "net/http/http_network_layer.h"
[email protected]319d9e6f2009-02-18 19:47:2133#include "net/http/http_response_headers.h"
[email protected]63de95b2008-12-10 04:11:2734#include "net/proxy/proxy_service.h"
initial.commit586acc5fe2008-07-26 22:42:5235#include "net/url_request/url_request.h"
36#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1537#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5238
[email protected]e1acf6f2008-10-27 20:43:3339using base::Time;
40
initial.commit586acc5fe2008-07-26 22:42:5241namespace {
42
initial.commit586acc5fe2008-07-26 22:42:5243class URLRequestHttpCacheContext : public URLRequestContext {
44 public:
45 URLRequestHttpCacheContext() {
[email protected]51fff29d2008-12-19 22:17:5346 proxy_service_ = net::ProxyService::CreateNull();
[email protected]db8f44c2008-12-13 04:52:0147 http_transaction_factory_ =
[email protected]63de95b2008-12-10 04:11:2748 new net::HttpCache(net::HttpNetworkLayer::CreateFactory(proxy_service_),
[email protected]db8f44c2008-12-13 04:52:0149 disk_cache::CreateInMemoryCacheBackend(0));
[email protected]0757e7702009-03-27 04:00:2250 // In-memory cookie store.
51 cookie_store_ = new net::CookieMonster();
[email protected]db8f44c2008-12-13 04:52:0152 }
53
54 virtual ~URLRequestHttpCacheContext() {
[email protected]0757e7702009-03-27 04:00:2255 delete cookie_store_;
[email protected]db8f44c2008-12-13 04:52:0156 delete http_transaction_factory_;
57 delete proxy_service_;
initial.commit586acc5fe2008-07-26 22:42:5258 }
59};
60
61class TestURLRequest : public URLRequest {
62 public:
[email protected]d1ec59082009-02-11 02:48:1563 TestURLRequest(const GURL& url, Delegate* delegate)
64 : URLRequest(url, delegate) {
65 set_context(new URLRequestHttpCacheContext());
66 }
initial.commit586acc5fe2008-07-26 22:42:5267};
68
[email protected]73f5d662008-11-20 01:08:1769StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:5270 return "header";
71}
72
[email protected]71c64f62008-11-15 04:36:5173// Do a case-insensitive search through |haystack| for |needle|.
74bool ContainsString(const std::string& haystack, const char* needle) {
75 std::string::const_iterator it =
76 std::search(haystack.begin(),
77 haystack.end(),
78 needle,
79 needle + strlen(needle),
80 CaseInsensitiveCompare<char>());
81 return it != haystack.end();
82}
83
[email protected]9396b252008-09-29 17:29:3884} // namespace
initial.commit586acc5fe2008-07-26 22:42:5285
[email protected]7a0bb4bf2008-11-19 21:41:4886// Inherit PlatformTest since we require the autorelease pool on Mac OS X.f
87class URLRequestTest : public PlatformTest {
88};
89
[email protected]d1ec59082009-02-11 02:48:1590TEST_F(URLRequestTest, ProxyTunnelRedirectTest) {
91 // In this unit test, we're using the HTTPTestServer as a proxy server and
[email protected]dc651782009-02-14 01:45:0892 // issuing a CONNECT request with the magic host name "www.redirect.com".
93 // The HTTPTestServer will return a 302 response, which we should not
[email protected]d1ec59082009-02-11 02:48:1594 // follow.
95 scoped_refptr<HTTPTestServer> server =
96 HTTPTestServer::CreateServer(L"", NULL);
97 ASSERT_TRUE(NULL != server.get());
98 TestDelegate d;
99 {
100 URLRequest r(GURL("https://www.redirect.com/"), &d);
101 std::string proxy("localhost:");
102 proxy.append(IntToString(kHTTPDefaultPort));
103 r.set_context(new TestURLRequestContext(proxy));
104
105 r.Start();
106 EXPECT_TRUE(r.is_pending());
107
108 MessageLoop::current()->Run();
109
[email protected]c744cf22009-02-27 07:28:08110 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
111 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08112 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:15113 // We should not have followed the redirect.
114 EXPECT_EQ(0, d.received_redirect_count());
115 }
116}
117
[email protected]dc651782009-02-14 01:45:08118TEST_F(URLRequestTest, UnexpectedServerAuthTest) {
119 // In this unit test, we're using the HTTPTestServer as a proxy server and
120 // issuing a CONNECT request with the magic host name "www.server-auth.com".
121 // The HTTPTestServer will return a 401 response, which we should balk at.
122 scoped_refptr<HTTPTestServer> server =
123 HTTPTestServer::CreateServer(L"", NULL);
124 ASSERT_TRUE(NULL != server.get());
125 TestDelegate d;
126 {
127 URLRequest r(GURL("https://www.server-auth.com/"), &d);
128 std::string proxy("localhost:");
129 proxy.append(IntToString(kHTTPDefaultPort));
130 r.set_context(new TestURLRequestContext(proxy));
131
132 r.Start();
133 EXPECT_TRUE(r.is_pending());
134
135 MessageLoop::current()->Run();
136
137 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]c744cf22009-02-27 07:28:08138 EXPECT_EQ(net::ERR_TUNNEL_CONNECTION_FAILED, r.status().os_error());
[email protected]dc651782009-02-14 01:45:08139 }
140}
141
[email protected]7a0bb4bf2008-11-19 21:41:48142TEST_F(URLRequestTest, GetTest_NoCache) {
[email protected]dd265012009-01-08 20:45:27143 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55144 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27145 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52146 TestDelegate d;
147 {
[email protected]dd265012009-01-08 20:45:27148 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52149
150 r.Start();
151 EXPECT_TRUE(r.is_pending());
152
153 MessageLoop::current()->Run();
154
155 EXPECT_EQ(1, d.response_started_count());
156 EXPECT_FALSE(d.received_data_before_response());
157 EXPECT_NE(0, d.bytes_received());
158 }
159#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15160 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52161#endif
162}
163
[email protected]7a0bb4bf2008-11-19 21:41:48164TEST_F(URLRequestTest, GetTest) {
[email protected]dd265012009-01-08 20:45:27165 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55166 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27167 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52168 TestDelegate d;
169 {
[email protected]dd265012009-01-08 20:45:27170 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52171
172 r.Start();
173 EXPECT_TRUE(r.is_pending());
174
175 MessageLoop::current()->Run();
176
177 EXPECT_EQ(1, d.response_started_count());
178 EXPECT_FALSE(d.received_data_before_response());
179 EXPECT_NE(0, d.bytes_received());
180 }
181#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15182 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52183#endif
184}
185
[email protected]73e0bba2009-02-19 22:57:09186TEST_F(URLRequestTest, QuitTest) {
187 scoped_refptr<HTTPTestServer> server =
188 HTTPTestServer::CreateServer(L"", NULL);
189 ASSERT_TRUE(NULL != server.get());
190 server->SendQuit();
191 EXPECT_TRUE(server->WaitToFinish(20000));
[email protected]ac4335c2009-02-19 00:48:06192
[email protected]73e0bba2009-02-19 22:57:09193#ifndef NDEBUG
194 DCHECK_EQ(url_request_metrics.object_count, 0);
195#endif
196}
197
198class HTTPSRequestTest : public testing::Test {
[email protected]ea224582008-12-07 20:25:46199};
200
201#if defined(OS_MACOSX)
[email protected]73e0bba2009-02-19 22:57:09202// ssl_client_socket_mac.cc crashes currently in GetSSLInfo
203// when called on a connection with an unrecognized certificate
[email protected]bacff652009-03-31 17:50:33204#define MAYBE_HTTPSGetTest DISABLED_HTTPSGetTest
205#define MAYBE_HTTPSMismatchedTest DISABLED_HTTPSMismatchedTest
206#define MAYBE_HTTPSExpiredTest DISABLED_HTTPSExpiredTest
[email protected]ea224582008-12-07 20:25:46207#else
[email protected]bacff652009-03-31 17:50:33208#define MAYBE_HTTPSGetTest HTTPSGetTest
209#define MAYBE_HTTPSMismatchedTest HTTPSMismatchedTest
210#define MAYBE_HTTPSExpiredTest HTTPSExpiredTest
[email protected]ea224582008-12-07 20:25:46211#endif
212
213TEST_F(HTTPSRequestTest, MAYBE_HTTPSGetTest) {
214 // Note: tools/testserver/testserver.py does not need
215 // a working document root to server the pages / and /hello.html,
216 // so this test doesn't really need to specify a document root.
217 // But if it did, a good one would be net/data/ssl.
[email protected]dd265012009-01-08 20:45:27218 scoped_refptr<HTTPSTestServer> server =
[email protected]73e0bba2009-02-19 22:57:09219 HTTPSTestServer::CreateGoodServer(L"net/data/ssl");
[email protected]dd265012009-01-08 20:45:27220 ASSERT_TRUE(NULL != server.get());
[email protected]ea224582008-12-07 20:25:46221
[email protected]ea224582008-12-07 20:25:46222 TestDelegate d;
223 {
[email protected]dd265012009-01-08 20:45:27224 TestURLRequest r(server->TestServerPage(""), &d);
[email protected]ea224582008-12-07 20:25:46225
226 r.Start();
227 EXPECT_TRUE(r.is_pending());
228
229 MessageLoop::current()->Run();
230
231 EXPECT_EQ(1, d.response_started_count());
232 EXPECT_FALSE(d.received_data_before_response());
233 EXPECT_NE(0, d.bytes_received());
234 }
235#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15236 DCHECK_EQ(url_request_metrics.object_count, 0);
[email protected]ea224582008-12-07 20:25:46237#endif
238}
239
[email protected]bacff652009-03-31 17:50:33240TEST_F(HTTPSRequestTest, MAYBE_HTTPSMismatchedTest) {
241 scoped_refptr<HTTPSTestServer> server =
242 HTTPSTestServer::CreateMismatchedServer(L"net/data/ssl");
243 ASSERT_TRUE(NULL != server.get());
244
245 bool err_allowed = true;
246 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
247 TestDelegate d;
248 {
249 d.set_allow_certificate_errors(err_allowed);
250 TestURLRequest r(server->TestServerPage(""), &d);
251
252 r.Start();
253 EXPECT_TRUE(r.is_pending());
254
255 MessageLoop::current()->Run();
256
257 EXPECT_EQ(1, d.response_started_count());
258 EXPECT_FALSE(d.received_data_before_response());
259 EXPECT_TRUE(d.have_certificate_errors());
260 if (err_allowed)
261 EXPECT_NE(0, d.bytes_received());
262 else
263 EXPECT_EQ(0, d.bytes_received());
264 }
265 }
266}
267
268TEST_F(HTTPSRequestTest, MAYBE_HTTPSExpiredTest) {
269 scoped_refptr<HTTPSTestServer> server =
270 HTTPSTestServer::CreateExpiredServer(L"net/data/ssl");
271 ASSERT_TRUE(NULL != server.get());
272
273 // Iterate from false to true, just so that we do the opposite of the
274 // previous test in order to increase test coverage.
275 bool err_allowed = false;
276 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
277 TestDelegate d;
278 {
279 d.set_allow_certificate_errors(err_allowed);
280 TestURLRequest r(server->TestServerPage(""), &d);
281
282 r.Start();
283 EXPECT_TRUE(r.is_pending());
284
285 MessageLoop::current()->Run();
286
287 EXPECT_EQ(1, d.response_started_count());
288 EXPECT_FALSE(d.received_data_before_response());
289 EXPECT_TRUE(d.have_certificate_errors());
290 if (err_allowed)
291 EXPECT_NE(0, d.bytes_received());
292 else
293 EXPECT_EQ(0, d.bytes_received());
294 }
295 }
296}
[email protected]73e0bba2009-02-19 22:57:09297
[email protected]7a0bb4bf2008-11-19 21:41:48298TEST_F(URLRequestTest, CancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52299 TestDelegate d;
300 {
301 TestURLRequest r(GURL("http://www.google.com/"), &d);
302
303 r.Start();
304 EXPECT_TRUE(r.is_pending());
305
306 r.Cancel();
307
308 MessageLoop::current()->Run();
309
310 // We expect to receive OnResponseStarted even though the request has been
311 // cancelled.
312 EXPECT_EQ(1, d.response_started_count());
313 EXPECT_EQ(0, d.bytes_received());
314 EXPECT_FALSE(d.received_data_before_response());
315 }
316#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15317 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52318#endif
319}
320
[email protected]7a0bb4bf2008-11-19 21:41:48321TEST_F(URLRequestTest, CancelTest2) {
[email protected]dd265012009-01-08 20:45:27322 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55323 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27324 ASSERT_TRUE(NULL != server.get());
325
326 // error C2446: '!=' : no conversion from 'HTTPTestServer *const '
327 // to 'const int'
328
initial.commit586acc5fe2008-07-26 22:42:52329 TestDelegate d;
330 {
[email protected]dd265012009-01-08 20:45:27331 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52332
333 d.set_cancel_in_response_started(true);
334
335 r.Start();
336 EXPECT_TRUE(r.is_pending());
337
338 MessageLoop::current()->Run();
339
340 EXPECT_EQ(1, d.response_started_count());
341 EXPECT_EQ(0, d.bytes_received());
342 EXPECT_FALSE(d.received_data_before_response());
343 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
344 }
345#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15346 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52347#endif
348}
349
[email protected]7a0bb4bf2008-11-19 21:41:48350TEST_F(URLRequestTest, CancelTest3) {
[email protected]dd265012009-01-08 20:45:27351 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55352 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27353 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52354 TestDelegate d;
355 {
[email protected]dd265012009-01-08 20:45:27356 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52357
358 d.set_cancel_in_received_data(true);
359
360 r.Start();
361 EXPECT_TRUE(r.is_pending());
362
363 MessageLoop::current()->Run();
364
365 EXPECT_EQ(1, d.response_started_count());
366 // There is no guarantee about how much data was received
367 // before the cancel was issued. It could have been 0 bytes,
368 // or it could have been all the bytes.
369 // EXPECT_EQ(0, d.bytes_received());
370 EXPECT_FALSE(d.received_data_before_response());
371 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
372 }
373#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15374 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52375#endif
376}
377
[email protected]7a0bb4bf2008-11-19 21:41:48378TEST_F(URLRequestTest, CancelTest4) {
[email protected]dd265012009-01-08 20:45:27379 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55380 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27381 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52382 TestDelegate d;
383 {
[email protected]dd265012009-01-08 20:45:27384 TestURLRequest r(server->TestServerPage(""), &d);
initial.commit586acc5fe2008-07-26 22:42:52385
386 r.Start();
387 EXPECT_TRUE(r.is_pending());
388
389 // The request will be implicitly canceled when it is destroyed. The
390 // test delegate must not post a quit message when this happens because
391 // this test doesn't actually have a message loop. The quit message would
392 // get put on this thread's message queue and the next test would exit
393 // early, causing problems.
394 d.set_quit_on_complete(false);
395 }
396 // expect things to just cleanup properly.
397
398 // we won't actually get a received reponse here because we've never run the
399 // message loop
400 EXPECT_FALSE(d.received_data_before_response());
401 EXPECT_EQ(0, d.bytes_received());
402}
403
[email protected]7a0bb4bf2008-11-19 21:41:48404TEST_F(URLRequestTest, CancelTest5) {
[email protected]dd265012009-01-08 20:45:27405 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55406 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27407 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52408 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
409
410 // populate cache
411 {
412 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27413 URLRequest r(server->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52414 r.set_context(context);
415 r.Start();
416 MessageLoop::current()->Run();
417 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
418 }
419
420 // cancel read from cache (see bug 990242)
421 {
422 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27423 URLRequest r(server->TestServerPage("cachetime"), &d);
initial.commit586acc5fe2008-07-26 22:42:52424 r.set_context(context);
425 r.Start();
426 r.Cancel();
427 MessageLoop::current()->Run();
428
429 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
430 EXPECT_EQ(1, d.response_started_count());
431 EXPECT_EQ(0, d.bytes_received());
432 EXPECT_FALSE(d.received_data_before_response());
433 }
434
435#ifndef NDEBUG
436 DCHECK_EQ(url_request_metrics.object_count, 0);
437#endif
438}
439
[email protected]7a0bb4bf2008-11-19 21:41:48440TEST_F(URLRequestTest, PostTest) {
[email protected]dd265012009-01-08 20:45:27441 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55442 HTTPTestServer::CreateServer(L"net/data", NULL);
[email protected]dd265012009-01-08 20:45:27443 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52444 const int kMsgSize = 20000; // multiple of 10
445 const int kIterations = 50;
446 char *uploadBytes = new char[kMsgSize+1];
447 char *ptr = uploadBytes;
448 char marker = 'a';
[email protected]d1ec59082009-02-11 02:48:15449 for (int idx = 0; idx < kMsgSize/10; idx++) {
initial.commit586acc5fe2008-07-26 22:42:52450 memcpy(ptr, "----------", 10);
451 ptr += 10;
452 if (idx % 100 == 0) {
453 ptr--;
454 *ptr++ = marker;
455 if (++marker > 'z')
456 marker = 'a';
457 }
initial.commit586acc5fe2008-07-26 22:42:52458 }
459 uploadBytes[kMsgSize] = '\0';
460
461 scoped_refptr<URLRequestContext> context =
462 new URLRequestHttpCacheContext();
463
464 for (int i = 0; i < kIterations; ++i) {
465 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27466 URLRequest r(server->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52467 r.set_context(context);
468 r.set_method("POST");
469
470 r.AppendBytesToUpload(uploadBytes, kMsgSize);
471
472 r.Start();
473 EXPECT_TRUE(r.is_pending());
474
475 MessageLoop::current()->Run();
476
477 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
478 (int) r.status().status() << ", os error: " << r.status().os_error();
479
480 EXPECT_FALSE(d.received_data_before_response());
481 EXPECT_EQ(uploadBytes, d.data_received());
[email protected]d1ec59082009-02-11 02:48:15482 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0);
initial.commit586acc5fe2008-07-26 22:42:52483 EXPECT_EQ(d.data_received().compare(uploadBytes), 0);
484 }
485 delete[] uploadBytes;
486#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15487 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52488#endif
489}
490
[email protected]7a0bb4bf2008-11-19 21:41:48491TEST_F(URLRequestTest, PostEmptyTest) {
[email protected]dd265012009-01-08 20:45:27492 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55493 HTTPTestServer::CreateServer(L"net/data", NULL);
[email protected]dd265012009-01-08 20:45:27494 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52495 TestDelegate d;
496 {
[email protected]dd265012009-01-08 20:45:27497 TestURLRequest r(server->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52498 r.set_method("POST");
499
500 r.Start();
501 EXPECT_TRUE(r.is_pending());
502
503 MessageLoop::current()->Run();
504
505 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
506 (int) r.status().status() << ", os error: " << r.status().os_error();
507
508 EXPECT_FALSE(d.received_data_before_response());
509 EXPECT_TRUE(d.data_received().empty());
510 }
511#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15512 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52513#endif
514}
515
[email protected]7a0bb4bf2008-11-19 21:41:48516TEST_F(URLRequestTest, PostFileTest) {
[email protected]dd265012009-01-08 20:45:27517 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55518 HTTPTestServer::CreateServer(L"net/data", NULL);
[email protected]dd265012009-01-08 20:45:27519 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52520 TestDelegate d;
521 {
[email protected]dd265012009-01-08 20:45:27522 TestURLRequest r(server->TestServerPage("echo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52523 r.set_method("POST");
524
525 std::wstring dir;
526 PathService::Get(base::DIR_EXE, &dir);
[email protected]9396b252008-09-29 17:29:38527 file_util::SetCurrentDirectory(dir);
initial.commit586acc5fe2008-07-26 22:42:52528
529 std::wstring path;
530 PathService::Get(base::DIR_SOURCE_ROOT, &path);
531 file_util::AppendToPath(&path, L"net");
532 file_util::AppendToPath(&path, L"data");
533 file_util::AppendToPath(&path, L"url_request_unittest");
534 file_util::AppendToPath(&path, L"with-headers.html");
535 r.AppendFileToUpload(path);
536
537 // This file should just be ignored in the upload stream.
538 r.AppendFileToUpload(L"c:\\path\\to\\non\\existant\\file.randomness.12345");
539
540 r.Start();
541 EXPECT_TRUE(r.is_pending());
542
543 MessageLoop::current()->Run();
544
[email protected]10a1fe92008-11-04 21:47:02545 int64 longsize;
546 ASSERT_EQ(true, file_util::GetFileSize(path, &longsize));
547 int size = static_cast<int>(longsize);
initial.commit586acc5fe2008-07-26 22:42:52548 scoped_array<char> buf(new char[size]);
549
[email protected]dd265012009-01-08 20:45:27550 int size_read = static_cast<int>(file_util::ReadFile(path,
551 buf.get(), size));
[email protected]eac0709a2008-11-04 21:00:46552 ASSERT_EQ(size, size_read);
initial.commit586acc5fe2008-07-26 22:42:52553
554 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
555 (int) r.status().status() << ", os error: " << r.status().os_error();
556
557 EXPECT_FALSE(d.received_data_before_response());
558
559 ASSERT_EQ(size, d.bytes_received());
560 EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size));
561 }
562#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15563 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52564#endif
565}
566
[email protected]7a0bb4bf2008-11-19 21:41:48567TEST_F(URLRequestTest, AboutBlankTest) {
initial.commit586acc5fe2008-07-26 22:42:52568 TestDelegate d;
569 {
570 TestURLRequest r(GURL("about:blank"), &d);
571
572 r.Start();
573 EXPECT_TRUE(r.is_pending());
574
575 MessageLoop::current()->Run();
576
577 EXPECT_TRUE(!r.is_pending());
578 EXPECT_FALSE(d.received_data_before_response());
579 EXPECT_EQ(d.bytes_received(), 0);
580 }
581#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15582 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52583#endif
584}
585
[email protected]7a0bb4bf2008-11-19 21:41:48586TEST_F(URLRequestTest, FileTest) {
[email protected]b9e04f02008-11-27 04:03:57587 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:52588 PathService::Get(base::FILE_EXE, &app_path);
[email protected]b9e04f02008-11-27 04:03:57589 GURL app_url = net::FilePathToFileURL(app_path.ToWStringHack());
initial.commit586acc5fe2008-07-26 22:42:52590
591 TestDelegate d;
592 {
[email protected]b9e04f02008-11-27 04:03:57593 TestURLRequest r(app_url, &d);
initial.commit586acc5fe2008-07-26 22:42:52594
595 r.Start();
596 EXPECT_TRUE(r.is_pending());
597
598 MessageLoop::current()->Run();
599
[email protected]b77280c2009-03-20 17:27:47600 int64 file_size = -1;
601 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
initial.commit586acc5fe2008-07-26 22:42:52602
603 EXPECT_TRUE(!r.is_pending());
604 EXPECT_EQ(1, d.response_started_count());
605 EXPECT_FALSE(d.received_data_before_response());
[email protected]9396b252008-09-29 17:29:38606 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
initial.commit586acc5fe2008-07-26 22:42:52607 }
608#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15609 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52610#endif
611}
612
[email protected]7a0bb4bf2008-11-19 21:41:48613TEST_F(URLRequestTest, InvalidUrlTest) {
initial.commit586acc5fe2008-07-26 22:42:52614 TestDelegate d;
615 {
616 TestURLRequest r(GURL("invalid url"), &d);
617
618 r.Start();
619 EXPECT_TRUE(r.is_pending());
620
621 MessageLoop::current()->Run();
622 EXPECT_TRUE(d.request_failed());
623 }
624#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15625 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52626#endif
627}
628
[email protected]6412d322008-11-21 23:49:30629// This test is disabled because it fails on some computers due to proxies
630// returning a page in response to this request rather than reporting failure.
631TEST_F(URLRequestTest, DISABLED_DnsFailureTest) {
initial.commit586acc5fe2008-07-26 22:42:52632 TestDelegate d;
633 {
634 URLRequest r(GURL("http://thisisnotavalidurl0123456789foo.com/"), &d);
635
636 r.Start();
637 EXPECT_TRUE(r.is_pending());
638
639 MessageLoop::current()->Run();
640 EXPECT_TRUE(d.request_failed());
641 }
642#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15643 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52644#endif
645}
initial.commit586acc5fe2008-07-26 22:42:52646
[email protected]7a0bb4bf2008-11-19 21:41:48647TEST_F(URLRequestTest, ResponseHeadersTest) {
[email protected]dd265012009-01-08 20:45:27648 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55649 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27650 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52651 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27652 TestURLRequest req(server->TestServerPage("files/with-headers.html"), &d);
initial.commit586acc5fe2008-07-26 22:42:52653 req.Start();
654 MessageLoop::current()->Run();
655
656 const net::HttpResponseHeaders* headers = req.response_headers();
657 std::string header;
658 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
659 EXPECT_EQ("private", header);
660
661 header.clear();
662 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
663 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
664
665 // The response has two "X-Multiple-Entries" headers.
666 // This verfies our output has them concatenated together.
667 header.clear();
668 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
669 EXPECT_EQ("a, b", header);
670}
671
[email protected]7a0bb4bf2008-11-19 21:41:48672TEST_F(URLRequestTest, BZip2ContentTest) {
[email protected]dd265012009-01-08 20:45:27673 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55674 HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL);
[email protected]dd265012009-01-08 20:45:27675 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52676
677 // for localhost domain, we also should support bzip2 encoding
678 // first, get the original file
679 TestDelegate d1;
[email protected]dd265012009-01-08 20:45:27680 TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1);
initial.commit586acc5fe2008-07-26 22:42:52681 req1.Start();
682 MessageLoop::current()->Run();
683
684 const std::string& got_content = d1.data_received();
685
686 // second, get bzip2 content
687 TestDelegate d2;
[email protected]dd265012009-01-08 20:45:27688 TestURLRequest req2(server->TestServerPage("realbz2files/google.txt"), &d2);
initial.commit586acc5fe2008-07-26 22:42:52689 req2.Start();
690 MessageLoop::current()->Run();
691
692 const std::string& got_bz2_content = d2.data_received();
693
694 // compare those two results
[email protected]24cfabd2008-08-15 00:05:39695 EXPECT_EQ(got_content, got_bz2_content);
initial.commit586acc5fe2008-07-26 22:42:52696}
697
[email protected]7a0bb4bf2008-11-19 21:41:48698TEST_F(URLRequestTest, BZip2ContentTest_IncrementalHeader) {
[email protected]dd265012009-01-08 20:45:27699 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55700 HTTPTestServer::CreateServer(L"net/data/filter_unittests", NULL);
[email protected]dd265012009-01-08 20:45:27701 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52702
703 // for localhost domain, we also should support bzip2 encoding
704 // first, get the original file
705 TestDelegate d1;
[email protected]dd265012009-01-08 20:45:27706 TestURLRequest req1(server->TestServerPage("realfiles/google.txt"), &d1);
initial.commit586acc5fe2008-07-26 22:42:52707 req1.Start();
708 MessageLoop::current()->Run();
709
710 const std::string& got_content = d1.data_received();
711
712 // second, get bzip2 content. ask the testserver to send the BZ2 header in
713 // two chunks with a delay between them. this tests our fix for bug 867161.
714 TestDelegate d2;
[email protected]dd265012009-01-08 20:45:27715 TestURLRequest req2(server->TestServerPage(
716 "realbz2files/google.txt?incremental-header"), &d2);
initial.commit586acc5fe2008-07-26 22:42:52717 req2.Start();
718 MessageLoop::current()->Run();
719
720 const std::string& got_bz2_content = d2.data_received();
721
722 // compare those two results
[email protected]24cfabd2008-08-15 00:05:39723 EXPECT_EQ(got_content, got_bz2_content);
initial.commit586acc5fe2008-07-26 22:42:52724}
725
[email protected]9396b252008-09-29 17:29:38726#if defined(OS_WIN)
[email protected]7a0bb4bf2008-11-19 21:41:48727TEST_F(URLRequestTest, ResolveShortcutTest) {
initial.commit586acc5fe2008-07-26 22:42:52728 std::wstring app_path;
729 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
730 file_util::AppendToPath(&app_path, L"net");
731 file_util::AppendToPath(&app_path, L"data");
732 file_util::AppendToPath(&app_path, L"url_request_unittest");
733 file_util::AppendToPath(&app_path, L"with-headers.html");
734
735 std::wstring lnk_path = app_path + L".lnk";
736
737 HRESULT result;
738 IShellLink *shell = NULL;
739 IPersistFile *persist = NULL;
740
741 CoInitialize(NULL);
742 // Temporarily create a shortcut for test
743 result = CoCreateInstance(CLSID_ShellLink, NULL,
744 CLSCTX_INPROC_SERVER, IID_IShellLink,
745 reinterpret_cast<LPVOID*>(&shell));
746 EXPECT_TRUE(SUCCEEDED(result));
747 result = shell->QueryInterface(IID_IPersistFile,
748 reinterpret_cast<LPVOID*>(&persist));
749 EXPECT_TRUE(SUCCEEDED(result));
750 result = shell->SetPath(app_path.c_str());
751 EXPECT_TRUE(SUCCEEDED(result));
752 result = shell->SetDescription(L"ResolveShortcutTest");
753 EXPECT_TRUE(SUCCEEDED(result));
754 result = persist->Save(lnk_path.c_str(), TRUE);
755 EXPECT_TRUE(SUCCEEDED(result));
756 if (persist)
757 persist->Release();
758 if (shell)
759 shell->Release();
760
761 TestDelegate d;
762 {
[email protected]8ac1a752008-07-31 19:40:37763 TestURLRequest r(net::FilePathToFileURL(lnk_path), &d);
initial.commit586acc5fe2008-07-26 22:42:52764
765 r.Start();
766 EXPECT_TRUE(r.is_pending());
767
768 MessageLoop::current()->Run();
769
770 WIN32_FILE_ATTRIBUTE_DATA data;
771 GetFileAttributesEx(app_path.c_str(), GetFileExInfoStandard, &data);
772 HANDLE file = CreateFile(app_path.c_str(), GENERIC_READ,
773 FILE_SHARE_READ, NULL, OPEN_EXISTING,
774 FILE_ATTRIBUTE_NORMAL, NULL);
775 EXPECT_NE(INVALID_HANDLE_VALUE, file);
776 scoped_array<char> buffer(new char[data.nFileSizeLow]);
777 DWORD read_size;
778 BOOL result;
779 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
780 &read_size, NULL);
781 std::string content(buffer.get(), read_size);
782 CloseHandle(file);
783
784 EXPECT_TRUE(!r.is_pending());
785 EXPECT_EQ(1, d.received_redirect_count());
786 EXPECT_EQ(content, d.data_received());
787 }
788
789 // Clean the shortcut
790 DeleteFile(lnk_path.c_str());
791 CoUninitialize();
792
793#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15794 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52795#endif
796}
[email protected]9396b252008-09-29 17:29:38797#endif // defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:52798
[email protected]7a0bb4bf2008-11-19 21:41:48799TEST_F(URLRequestTest, ContentTypeNormalizationTest) {
[email protected]dd265012009-01-08 20:45:27800 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55801 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27802 ASSERT_TRUE(NULL != server.get());
803
initial.commit586acc5fe2008-07-26 22:42:52804 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27805 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52806 "files/content-type-normalization.html"), &d);
807 req.Start();
808 MessageLoop::current()->Run();
809
810 std::string mime_type;
811 req.GetMimeType(&mime_type);
812 EXPECT_EQ("text/html", mime_type);
813
814 std::string charset;
815 req.GetCharset(&charset);
816 EXPECT_EQ("utf-8", charset);
817 req.Cancel();
818}
819
[email protected]7a0bb4bf2008-11-19 21:41:48820TEST_F(URLRequestTest, FileDirCancelTest) {
initial.commit586acc5fe2008-07-26 22:42:52821 // Put in mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37822 net::NetModule::SetResourceProvider(TestNetResourceProvider);
initial.commit586acc5fe2008-07-26 22:42:52823
824 TestDelegate d;
825 {
826 std::wstring file_path;
827 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
828 file_util::AppendToPath(&file_path, L"net");
829 file_util::AppendToPath(&file_path, L"data");
830 file_util::AppendToPath(&file_path, L"");
831
[email protected]8ac1a752008-07-31 19:40:37832 TestURLRequest req(net::FilePathToFileURL(file_path), &d);
initial.commit586acc5fe2008-07-26 22:42:52833 req.Start();
834 EXPECT_TRUE(req.is_pending());
835
836 d.set_cancel_in_received_data_pending(true);
837
838 MessageLoop::current()->Run();
839 }
840#ifndef NDEBUG
[email protected]d1ec59082009-02-11 02:48:15841 DCHECK_EQ(url_request_metrics.object_count, 0);
initial.commit586acc5fe2008-07-26 22:42:52842#endif
843
844 // Take out mock resource provider.
[email protected]8ac1a752008-07-31 19:40:37845 net::NetModule::SetResourceProvider(NULL);
initial.commit586acc5fe2008-07-26 22:42:52846}
847
[email protected]7a0bb4bf2008-11-19 21:41:48848TEST_F(URLRequestTest, RestrictRedirects) {
[email protected]dd265012009-01-08 20:45:27849 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55850 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27851 ASSERT_TRUE(NULL != server.get());
852
initial.commit586acc5fe2008-07-26 22:42:52853 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27854 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52855 "files/redirect-to-file.html"), &d);
856 req.Start();
857 MessageLoop::current()->Run();
858
859 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
860 EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error());
861}
862
[email protected]7a0bb4bf2008-11-19 21:41:48863TEST_F(URLRequestTest, NoUserPassInReferrer) {
[email protected]dd265012009-01-08 20:45:27864 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55865 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27866 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52867 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27868 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52869 "echoheader?Referer"), &d);
870 req.set_referrer("http://user:[email protected]/");
871 req.Start();
872 MessageLoop::current()->Run();
873
874 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
875}
876
[email protected]7a0bb4bf2008-11-19 21:41:48877TEST_F(URLRequestTest, CancelRedirect) {
[email protected]dd265012009-01-08 20:45:27878 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55879 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27880 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52881 TestDelegate d;
882 {
883 d.set_cancel_in_received_redirect(true);
[email protected]dd265012009-01-08 20:45:27884 TestURLRequest req(server->TestServerPage(
initial.commit586acc5fe2008-07-26 22:42:52885 "files/redirect-test.html"), &d);
886 req.Start();
887 MessageLoop::current()->Run();
888
889 EXPECT_EQ(1, d.response_started_count());
890 EXPECT_EQ(0, d.bytes_received());
891 EXPECT_FALSE(d.received_data_before_response());
892 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
893 }
894}
895
[email protected]7a0bb4bf2008-11-19 21:41:48896TEST_F(URLRequestTest, VaryHeader) {
[email protected]dd265012009-01-08 20:45:27897 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55898 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:27899 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52900
901 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
902
903 Time response_time;
904
905 // populate the cache
906 {
907 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27908 URLRequest req(server->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52909 req.set_context(context);
910 req.SetExtraRequestHeaders("foo:1");
911 req.Start();
912 MessageLoop::current()->Run();
913
914 response_time = req.response_time();
915 }
916
917 // Make sure that the response time of a future response will be in the
918 // future!
[email protected]82fc7c72008-11-03 23:17:33919 PlatformThread::Sleep(10);
initial.commit586acc5fe2008-07-26 22:42:52920
921 // expect a cache hit
922 {
923 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27924 URLRequest req(server->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52925 req.set_context(context);
926 req.SetExtraRequestHeaders("foo:1");
927 req.Start();
928 MessageLoop::current()->Run();
929
930 EXPECT_TRUE(req.response_time() == response_time);
931 }
932
933 // expect a cache miss
934 {
935 TestDelegate d;
[email protected]dd265012009-01-08 20:45:27936 URLRequest req(server->TestServerPage("echoheader?foo"), &d);
initial.commit586acc5fe2008-07-26 22:42:52937 req.set_context(context);
938 req.SetExtraRequestHeaders("foo:2");
939 req.Start();
940 MessageLoop::current()->Run();
941
942 EXPECT_FALSE(req.response_time() == response_time);
943 }
944}
945
[email protected]7a0bb4bf2008-11-19 21:41:48946TEST_F(URLRequestTest, BasicAuth) {
initial.commit586acc5fe2008-07-26 22:42:52947 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
[email protected]dd265012009-01-08 20:45:27948 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:55949 HTTPTestServer::CreateServer(L"", NULL);
[email protected]dd265012009-01-08 20:45:27950 ASSERT_TRUE(NULL != server.get());
initial.commit586acc5fe2008-07-26 22:42:52951
952 Time response_time;
953
954 // populate the cache
955 {
956 TestDelegate d;
957 d.set_username(L"user");
958 d.set_password(L"secret");
959
[email protected]dd265012009-01-08 20:45:27960 URLRequest r(server->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:52961 r.set_context(context);
962 r.Start();
963
964 MessageLoop::current()->Run();
965
966 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
967
968 response_time = r.response_time();
969 }
970
971 // Let some time pass so we can ensure that a future response will have a
972 // response time value in the future.
[email protected]82fc7c72008-11-03 23:17:33973 PlatformThread::Sleep(10 /* milliseconds */);
initial.commit586acc5fe2008-07-26 22:42:52974
975 // repeat request with end-to-end validation. since auth-basic results in a
976 // cachable page, we expect this test to result in a 304. in which case, the
977 // response should be fetched from the cache.
978 {
979 TestDelegate d;
980 d.set_username(L"user");
981 d.set_password(L"secret");
982
[email protected]dd265012009-01-08 20:45:27983 URLRequest r(server->TestServerPage("auth-basic"), &d);
initial.commit586acc5fe2008-07-26 22:42:52984 r.set_context(context);
985 r.set_load_flags(net::LOAD_VALIDATE_CACHE);
986 r.Start();
987
988 MessageLoop::current()->Run();
989
990 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
991
992 // Should be the same cached document, which means that the response time
993 // should not have changed.
994 EXPECT_TRUE(response_time == r.response_time());
995 }
996}
license.botbf09a502008-08-24 00:55:55997
[email protected]0757e7702009-03-27 04:00:22998// Check that Set-Cookie headers in 401 responses are respected.
999// http://crbug.com/6450
1000TEST_F(URLRequestTest, BasicAuthWithCookies) {
1001 scoped_refptr<HTTPTestServer> server =
1002 HTTPTestServer::CreateServer(L"", NULL);
1003 ASSERT_TRUE(NULL != server.get());
1004
1005 GURL url_requiring_auth =
1006 server->TestServerPage("auth-basic?set-cookie-if-challenged");
1007
1008 // Request a page that will give a 401 containing a Set-Cookie header.
1009 // Verify that when the transaction is restarted, it includes the new cookie.
1010 {
1011 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
1012 TestDelegate d;
1013 d.set_username(L"user");
1014 d.set_password(L"secret");
1015
1016 URLRequest r(url_requiring_auth, &d);
1017 r.set_context(context);
1018 r.Start();
1019
1020 MessageLoop::current()->Run();
1021
1022 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
1023
1024 // Make sure we sent the cookie in the restarted transaction.
1025 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1026 != std::string::npos);
1027 }
1028
1029 // Same test as above, except this time the restart is initiated earlier
1030 // (without user intervention since identity is embedded in the URL).
1031 {
1032 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext();
1033 TestDelegate d;
1034
1035 GURL::Replacements replacements;
1036 std::string username("user2");
1037 std::string password("secret");
1038 replacements.SetUsernameStr(username);
1039 replacements.SetPasswordStr(password);
1040 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
1041
1042 URLRequest r(url_with_identity, &d);
1043 r.set_context(context);
1044 r.Start();
1045
1046 MessageLoop::current()->Run();
1047
1048 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
1049
1050 // Make sure we sent the cookie in the restarted transaction.
1051 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
1052 != std::string::npos);
1053 }
1054}
1055
[email protected]71c64f62008-11-15 04:36:511056// In this test, we do a POST which the server will 302 redirect.
1057// The subsequent transaction should use GET, and should not send the
1058// Content-Type header.
1059// http://code.google.com/p/chromium/issues/detail?id=843
[email protected]7a0bb4bf2008-11-19 21:41:481060TEST_F(URLRequestTest, Post302RedirectGet) {
[email protected]dd265012009-01-08 20:45:271061 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:551062 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:271063 ASSERT_TRUE(NULL != server.get());
[email protected]71c64f62008-11-15 04:36:511064 TestDelegate d;
[email protected]dd265012009-01-08 20:45:271065 TestURLRequest req(server->TestServerPage("files/redirect-to-echoall"), &d);
[email protected]71c64f62008-11-15 04:36:511066 req.set_method("POST");
1067
1068 // Set headers (some of which are specific to the POST).
1069 // ("Content-Length: 10" is just a junk value to make sure it gets stripped).
1070 req.SetExtraRequestHeaders(
[email protected]dd265012009-01-08 20:45:271071 "Content-Type: multipart/form-data; "
1072 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
1073 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
1074 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
[email protected]71c64f62008-11-15 04:36:511075 "Accept-Language: en-US,en\r\n"
1076 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
1077 "Content-Length: 10\r\n"
1078 "Origin: http://localhost:1337/"
1079 );
1080 req.Start();
1081 MessageLoop::current()->Run();
1082
1083 std::string mime_type;
1084 req.GetMimeType(&mime_type);
1085 EXPECT_EQ("text/html", mime_type);
1086
1087 const std::string& data = d.data_received();
1088
1089 // Check that the post-specific headers were stripped:
1090 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
1091 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
1092 EXPECT_FALSE(ContainsString(data, "Origin:"));
1093
1094 // These extra request headers should not have been stripped.
1095 EXPECT_TRUE(ContainsString(data, "Accept:"));
1096 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
1097 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
1098}
1099
[email protected]140932f2008-12-12 03:58:061100TEST_F(URLRequestTest, Post307RedirectPost) {
[email protected]dd265012009-01-08 20:45:271101 scoped_refptr<HTTPTestServer> server =
[email protected]cad014312009-01-29 21:59:551102 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL);
[email protected]dd265012009-01-08 20:45:271103 ASSERT_TRUE(NULL != server.get());
[email protected]140932f2008-12-12 03:58:061104 TestDelegate d;
[email protected]dd265012009-01-08 20:45:271105 TestURLRequest req(server->TestServerPage("files/redirect307-to-echoall"),
1106 &d);
[email protected]140932f2008-12-12 03:58:061107 req.set_method("POST");
1108 req.Start();
1109 MessageLoop::current()->Run();
1110 EXPECT_EQ(req.method(), "POST");
1111}
[email protected]dd265012009-01-08 20:45:271112
[email protected]4261e5b2009-01-08 22:53:221113// FTP tests appear to be hanging some of the time
1114#if 1 // !defined(OS_WIN)
[email protected]dd265012009-01-08 20:45:271115 #define MAYBE_FTPGetTestAnonymous DISABLED_FTPGetTestAnonymous
1116 #define MAYBE_FTPGetTest DISABLED_FTPGetTest
1117 #define MAYBE_FTPCheckWrongUser DISABLED_FTPCheckWrongUser
1118 #define MAYBE_FTPCheckWrongPassword DISABLED_FTPCheckWrongPassword
1119#else
1120 #define MAYBE_FTPGetTestAnonymous FTPGetTestAnonymous
1121 #define MAYBE_FTPGetTest FTPGetTest
1122 #define MAYBE_FTPCheckWrongUser FTPCheckWrongUser
1123 #define MAYBE_FTPCheckWrongPassword FTPCheckWrongPassword
1124#endif
1125
1126TEST_F(URLRequestTest, MAYBE_FTPGetTestAnonymous) {
1127 scoped_refptr<FTPTestServer> server = FTPTestServer::CreateServer(L"");
1128 ASSERT_TRUE(NULL != server.get());
1129 std::wstring app_path;
1130 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1131 app_path.append(L"\\LICENSE");
1132 TestDelegate d;
1133 {
1134 TestURLRequest r(server->TestServerPage("/LICENSE"), &d);
1135 r.Start();
1136 EXPECT_TRUE(r.is_pending());
1137
1138 MessageLoop::current()->Run();
1139
1140 int64 file_size = 0;
1141 file_util::GetFileSize(app_path, &file_size);
1142
1143 EXPECT_TRUE(!r.is_pending());
1144 EXPECT_EQ(1, d.response_started_count());
1145 EXPECT_FALSE(d.received_data_before_response());
1146 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
1147 }
1148}
1149
1150TEST_F(URLRequestTest, MAYBE_FTPGetTest) {
1151 scoped_refptr<FTPTestServer> server =
1152 FTPTestServer::CreateServer(L"", "chrome", "chrome");
1153 ASSERT_TRUE(NULL != server.get());
1154 std::wstring app_path;
1155 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1156 app_path.append(L"\\LICENSE");
1157 TestDelegate d;
1158 {
1159 TestURLRequest r(server->TestServerPage("/LICENSE"), &d);
1160 r.Start();
1161 EXPECT_TRUE(r.is_pending());
1162
1163 MessageLoop::current()->Run();
1164
1165 int64 file_size = 0;
1166 file_util::GetFileSize(app_path, &file_size);
1167
1168 EXPECT_TRUE(!r.is_pending());
1169 EXPECT_EQ(1, d.response_started_count());
1170 EXPECT_FALSE(d.received_data_before_response());
1171 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
1172 }
1173}
1174
1175TEST_F(URLRequestTest, MAYBE_FTPCheckWrongPassword) {
1176 scoped_refptr<FTPTestServer> server =
1177 FTPTestServer::CreateServer(L"", "chrome", "wrong_password");
1178 ASSERT_TRUE(NULL != server.get());
1179 std::wstring app_path;
1180 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1181 app_path.append(L"\\LICENSE");
1182 TestDelegate d;
1183 {
1184 TestURLRequest r(server->TestServerPage("/LICENSE"), &d);
1185 r.Start();
1186 EXPECT_TRUE(r.is_pending());
1187
1188 MessageLoop::current()->Run();
1189
1190 int64 file_size = 0;
1191 file_util::GetFileSize(app_path, &file_size);
1192
1193 EXPECT_TRUE(!r.is_pending());
1194 EXPECT_EQ(1, d.response_started_count());
1195 EXPECT_FALSE(d.received_data_before_response());
1196 EXPECT_EQ(d.bytes_received(), 0);
1197 }
1198}
1199
1200TEST_F(URLRequestTest, MAYBE_FTPCheckWrongUser) {
1201 scoped_refptr<FTPTestServer> server =
1202 FTPTestServer::CreateServer(L"", "wrong_user", "chrome");
1203 ASSERT_TRUE(NULL != server.get());
1204 std::wstring app_path;
1205 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1206 app_path.append(L"\\LICENSE");
1207 TestDelegate d;
1208 {
1209 TestURLRequest r(server->TestServerPage("/LICENSE"), &d);
1210 r.Start();
1211 EXPECT_TRUE(r.is_pending());
1212
1213 MessageLoop::current()->Run();
1214
1215 int64 file_size = 0;
1216 file_util::GetFileSize(app_path, &file_size);
1217
1218 EXPECT_TRUE(!r.is_pending());
1219 EXPECT_EQ(1, d.response_started_count());
1220 EXPECT_FALSE(d.received_data_before_response());
1221 EXPECT_EQ(d.bytes_received(), 0);
1222 }
1223}