OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/net/url_fetcher_impl.h" | 5 #include "content/common/net/url_fetcher_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | 117 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
118 | 118 |
119 std::string data; | 119 std::string data; |
120 EXPECT_TRUE(source->GetResponseAsString(&data)); | 120 EXPECT_TRUE(source->GetResponseAsString(&data)); |
121 EXPECT_FALSE(data.empty()); | 121 EXPECT_FALSE(data.empty()); |
122 | 122 |
123 delete fetcher_; // Have to delete this here and not in the destructor, | 123 delete fetcher_; // Have to delete this here and not in the destructor, |
124 // because the destructor won't necessarily run on the | 124 // because the destructor won't necessarily run on the |
125 // same thread that CreateFetcher() did. | 125 // same thread that CreateFetcher() did. |
126 | 126 |
127 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 127 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
128 // If the current message loop is not the IO loop, it will be shut down when | 128 // If the current message loop is not the IO loop, it will be shut down when |
129 // the main loop returns and this thread subsequently goes out of scope. | 129 // the main loop returns and this thread subsequently goes out of scope. |
130 } | 130 } |
131 | 131 |
132 namespace { | 132 namespace { |
133 | 133 |
134 // Version of URLFetcherTest that does a POST instead | 134 // Version of URLFetcherTest that does a POST instead |
135 class URLFetcherPostTest : public URLFetcherTest { | 135 class URLFetcherPostTest : public URLFetcherTest { |
136 public: | 136 public: |
137 virtual void CreateFetcher(const GURL& url); | 137 virtual void CreateFetcher(const GURL& url); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 199 |
200 void CancelRequest(); | 200 void CancelRequest(); |
201 }; | 201 }; |
202 | 202 |
203 // Version of TestURLRequestContext that posts a Quit task to the IO | 203 // Version of TestURLRequestContext that posts a Quit task to the IO |
204 // thread once it is deleted. | 204 // thread once it is deleted. |
205 class CancelTestURLRequestContext : public TestURLRequestContext { | 205 class CancelTestURLRequestContext : public TestURLRequestContext { |
206 virtual ~CancelTestURLRequestContext() { | 206 virtual ~CancelTestURLRequestContext() { |
207 // The d'tor should execute in the IO thread. Post the quit task to the | 207 // The d'tor should execute in the IO thread. Post the quit task to the |
208 // current thread. | 208 // current thread. |
209 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 209 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
210 } | 210 } |
211 }; | 211 }; |
212 | 212 |
213 class CancelTestURLRequestContextGetter : public net::URLRequestContextGetter { | 213 class CancelTestURLRequestContextGetter : public net::URLRequestContextGetter { |
214 public: | 214 public: |
215 explicit CancelTestURLRequestContextGetter( | 215 explicit CancelTestURLRequestContextGetter( |
216 base::MessageLoopProxy* io_message_loop_proxy) | 216 base::MessageLoopProxy* io_message_loop_proxy) |
217 : io_message_loop_proxy_(io_message_loop_proxy), | 217 : io_message_loop_proxy_(io_message_loop_proxy), |
218 context_created_(false, false) { | 218 context_created_(false, false) { |
219 } | 219 } |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 390 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
391 if (source->GetResponseCode() >= 500) { | 391 if (source->GetResponseCode() >= 500) { |
392 // Now running ServerUnavailable test. | 392 // Now running ServerUnavailable test. |
393 // It takes more than 1 second to finish all 11 requests. | 393 // It takes more than 1 second to finish all 11 requests. |
394 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 394 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
395 EXPECT_TRUE(source->GetStatus().is_success()); | 395 EXPECT_TRUE(source->GetStatus().is_success()); |
396 std::string data; | 396 std::string data; |
397 EXPECT_TRUE(source->GetResponseAsString(&data)); | 397 EXPECT_TRUE(source->GetResponseAsString(&data)); |
398 EXPECT_FALSE(data.empty()); | 398 EXPECT_FALSE(data.empty()); |
399 delete fetcher_; | 399 delete fetcher_; |
400 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 400 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
401 } else { | 401 } else { |
402 // Now running Overload test. | 402 // Now running Overload test. |
403 static int count = 0; | 403 static int count = 0; |
404 count++; | 404 count++; |
405 if (count < 20) { | 405 if (count < 20) { |
406 fetcher_->StartWithRequestContextGetter(new TestURLRequestContextGetter( | 406 fetcher_->StartWithRequestContextGetter(new TestURLRequestContextGetter( |
407 io_message_loop_proxy())); | 407 io_message_loop_proxy())); |
408 } else { | 408 } else { |
409 // We have already sent 20 requests continuously. And we expect that | 409 // We have already sent 20 requests continuously. And we expect that |
410 // it takes more than 1 second due to the overload protection settings. | 410 // it takes more than 1 second due to the overload protection settings. |
(...skipping 26 matching lines...) Expand all Loading... |
437 EXPECT_GT(fetcher_->GetBackoffDelay().InMicroseconds(), 0); | 437 EXPECT_GT(fetcher_->GetBackoffDelay().InMicroseconds(), 0); |
438 std::string data; | 438 std::string data; |
439 EXPECT_TRUE(source->GetResponseAsString(&data)); | 439 EXPECT_TRUE(source->GetResponseAsString(&data)); |
440 EXPECT_FALSE(data.empty()); | 440 EXPECT_FALSE(data.empty()); |
441 } else { | 441 } else { |
442 // We should not get here! | 442 // We should not get here! |
443 ADD_FAILURE(); | 443 ADD_FAILURE(); |
444 } | 444 } |
445 | 445 |
446 delete fetcher_; | 446 delete fetcher_; |
447 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 447 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
448 } | 448 } |
449 | 449 |
450 | 450 |
451 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { | 451 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { |
452 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); | 452 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); |
453 cert_dir_ = cert_dir_.AppendASCII("chrome"); | 453 cert_dir_ = cert_dir_.AppendASCII("chrome"); |
454 cert_dir_ = cert_dir_.AppendASCII("test"); | 454 cert_dir_ = cert_dir_.AppendASCII("test"); |
455 cert_dir_ = cert_dir_.AppendASCII("data"); | 455 cert_dir_ = cert_dir_.AppendASCII("data"); |
456 cert_dir_ = cert_dir_.AppendASCII("ssl"); | 456 cert_dir_ = cert_dir_.AppendASCII("ssl"); |
457 cert_dir_ = cert_dir_.AppendASCII("certificates"); | 457 cert_dir_ = cert_dir_.AppendASCII("certificates"); |
458 } | 458 } |
459 | 459 |
460 // The "server certificate expired" error should result in automatic | 460 // The "server certificate expired" error should result in automatic |
461 // cancellation of the request by | 461 // cancellation of the request by |
462 // net::URLRequest::Delegate::OnSSLCertificateError. | 462 // net::URLRequest::Delegate::OnSSLCertificateError. |
463 void URLFetcherBadHTTPSTest::OnURLFetchComplete( | 463 void URLFetcherBadHTTPSTest::OnURLFetchComplete( |
464 const content::URLFetcher* source) { | 464 const content::URLFetcher* source) { |
465 // This part is different from URLFetcherTest::OnURLFetchComplete | 465 // This part is different from URLFetcherTest::OnURLFetchComplete |
466 // because this test expects the request to be cancelled. | 466 // because this test expects the request to be cancelled. |
467 EXPECT_EQ(net::URLRequestStatus::CANCELED, source->GetStatus().status()); | 467 EXPECT_EQ(net::URLRequestStatus::CANCELED, source->GetStatus().status()); |
468 EXPECT_EQ(net::ERR_ABORTED, source->GetStatus().error()); | 468 EXPECT_EQ(net::ERR_ABORTED, source->GetStatus().error()); |
469 EXPECT_EQ(-1, source->GetResponseCode()); | 469 EXPECT_EQ(-1, source->GetResponseCode()); |
470 EXPECT_TRUE(source->GetCookies().empty()); | 470 EXPECT_TRUE(source->GetCookies().empty()); |
471 std::string data; | 471 std::string data; |
472 EXPECT_TRUE(source->GetResponseAsString(&data)); | 472 EXPECT_TRUE(source->GetResponseAsString(&data)); |
473 EXPECT_TRUE(data.empty()); | 473 EXPECT_TRUE(data.empty()); |
474 | 474 |
475 // The rest is the same as URLFetcherTest::OnURLFetchComplete. | 475 // The rest is the same as URLFetcherTest::OnURLFetchComplete. |
476 delete fetcher_; | 476 delete fetcher_; |
477 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 477 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
478 } | 478 } |
479 | 479 |
480 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | 480 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { |
481 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 481 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
482 CancelTestURLRequestContextGetter* context_getter = | 482 CancelTestURLRequestContextGetter* context_getter = |
483 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); | 483 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); |
484 fetcher_->SetRequestContext(context_getter); | 484 fetcher_->SetRequestContext(context_getter); |
485 fetcher_->SetMaxRetries(2); | 485 fetcher_->SetMaxRetries(2); |
486 fetcher_->Start(); | 486 fetcher_->Start(); |
487 // We need to wait for the creation of the net::URLRequestContext, since we | 487 // We need to wait for the creation of the net::URLRequestContext, since we |
488 // rely on it being destroyed as a signal to end the test. | 488 // rely on it being destroyed as a signal to end the test. |
489 context_getter->WaitForContextCreation(); | 489 context_getter->WaitForContextCreation(); |
490 CancelRequest(); | 490 CancelRequest(); |
491 } | 491 } |
492 | 492 |
493 void URLFetcherCancelTest::OnURLFetchComplete( | 493 void URLFetcherCancelTest::OnURLFetchComplete( |
494 const content::URLFetcher* source) { | 494 const content::URLFetcher* source) { |
495 // We should have cancelled the request before completion. | 495 // We should have cancelled the request before completion. |
496 ADD_FAILURE(); | 496 ADD_FAILURE(); |
497 delete fetcher_; | 497 delete fetcher_; |
498 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 498 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
499 } | 499 } |
500 | 500 |
501 void URLFetcherCancelTest::CancelRequest() { | 501 void URLFetcherCancelTest::CancelRequest() { |
502 delete fetcher_; | 502 delete fetcher_; |
503 // The URLFetcher's test context will post a Quit task once it is | 503 // The URLFetcher's test context will post a Quit task once it is |
504 // deleted. So if this test simply hangs, it means cancellation | 504 // deleted. So if this test simply hangs, it means cancellation |
505 // did not work. | 505 // did not work. |
506 } | 506 } |
507 | 507 |
508 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( | 508 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( |
509 const content::URLFetcher* source) { | 509 const content::URLFetcher* source) { |
510 EXPECT_TRUE(source->GetStatus().is_success()); | 510 EXPECT_TRUE(source->GetStatus().is_success()); |
511 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | 511 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
512 std::string data; | 512 std::string data; |
513 EXPECT_TRUE(source->GetResponseAsString(&data)); | 513 EXPECT_TRUE(source->GetResponseAsString(&data)); |
514 EXPECT_FALSE(data.empty()); | 514 EXPECT_FALSE(data.empty()); |
515 if (!data.empty() && data_.empty()) { | 515 if (!data.empty() && data_.empty()) { |
516 data_ = data; | 516 data_ = data; |
517 fetcher_->StartWithRequestContextGetter( | 517 fetcher_->StartWithRequestContextGetter( |
518 new TestURLRequestContextGetter(io_message_loop_proxy())); | 518 new TestURLRequestContextGetter(io_message_loop_proxy())); |
519 } else { | 519 } else { |
520 EXPECT_EQ(data, data_); | 520 EXPECT_EQ(data, data_); |
521 delete fetcher_; // Have to delete this here and not in the destructor, | 521 delete fetcher_; // Have to delete this here and not in the destructor, |
522 // because the destructor won't necessarily run on the | 522 // because the destructor won't necessarily run on the |
523 // same thread that CreateFetcher() did. | 523 // same thread that CreateFetcher() did. |
524 | 524 |
525 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 525 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
526 // If the current message loop is not the IO loop, it will be shut down when | 526 // If the current message loop is not the IO loop, it will be shut down when |
527 // the main loop returns and this thread subsequently goes out of scope. | 527 // the main loop returns and this thread subsequently goes out of scope. |
528 } | 528 } |
529 } | 529 } |
530 | 530 |
531 void URLFetcherTempFileTest::OnURLFetchComplete( | 531 void URLFetcherTempFileTest::OnURLFetchComplete( |
532 const content::URLFetcher* source) { | 532 const content::URLFetcher* source) { |
533 EXPECT_TRUE(source->GetStatus().is_success()); | 533 EXPECT_TRUE(source->GetStatus().is_success()); |
534 EXPECT_EQ(source->GetResponseCode(), 200); | 534 EXPECT_EQ(source->GetResponseCode(), 200); |
535 | 535 |
536 EXPECT_TRUE(source->GetResponseAsFilePath( | 536 EXPECT_TRUE(source->GetResponseAsFilePath( |
537 take_ownership_of_temp_file_, &temp_file_)); | 537 take_ownership_of_temp_file_, &temp_file_)); |
538 | 538 |
539 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); | 539 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); |
540 | 540 |
541 delete fetcher_; | 541 delete fetcher_; |
542 | 542 |
543 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 543 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
544 } | 544 } |
545 | 545 |
546 TEST_F(URLFetcherTest, SameThreadsTest) { | 546 TEST_F(URLFetcherTest, SameThreadsTest) { |
547 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 547 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
548 ASSERT_TRUE(test_server.Start()); | 548 ASSERT_TRUE(test_server.Start()); |
549 | 549 |
550 // Create the fetcher on the main thread. Since IO will happen on the main | 550 // Create the fetcher on the main thread. Since IO will happen on the main |
551 // thread, this will test URLFetcher's ability to do everything on one | 551 // thread, this will test URLFetcher's ability to do everything on one |
552 // thread. | 552 // thread. |
553 CreateFetcher(test_server.GetURL("defaultresponse")); | 553 CreateFetcher(test_server.GetURL("defaultresponse")); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 FROM_HERE, | 793 FROM_HERE, |
794 base::Bind(&CancelAllOnIO), | 794 base::Bind(&CancelAllOnIO), |
795 base::Bind(&MessageLoop::Quit, | 795 base::Bind(&MessageLoop::Quit, |
796 base::Unretained(MessageLoop::current()))); | 796 base::Unretained(MessageLoop::current()))); |
797 MessageLoop::current()->Run(); | 797 MessageLoop::current()->Run(); |
798 EXPECT_EQ(0, GetNumFetcherCores()); | 798 EXPECT_EQ(0, GetNumFetcherCores()); |
799 delete fetcher_; | 799 delete fetcher_; |
800 } | 800 } |
801 | 801 |
802 } // namespace. | 802 } // namespace. |
OLD | NEW |