| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // 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 | 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 "base/message_loop_proxy.h" | 5 #include "base/message_loop_proxy.h" |
| 6 #include "base/thread.h" | 6 #include "base/thread.h" |
| 7 #include "base/waitable_event.h" | 7 #include "base/waitable_event.h" |
| 8 #include "chrome/common/chrome_plugin_lib.h" | 8 #include "chrome/common/chrome_plugin_lib.h" |
| 9 #include "chrome/common/net/url_fetcher.h" | 9 #include "chrome/common/net/url_fetcher.h" |
| 10 #include "chrome/common/net/url_fetcher_protect.h" | 10 #include "chrome/common/net/url_fetcher_protect.h" |
| 11 #include "chrome/common/net/url_request_context_getter.h" | 11 #include "chrome/common/net/url_request_context_getter.h" |
| 12 #include "net/http/http_response_headers.h" | 12 #include "net/http/http_response_headers.h" |
| 13 #include "net/url_request/url_request_unittest.h" |
| 13 #include "net/test/test_server.h" | 14 #include "net/test/test_server.h" |
| 14 #include "net/url_request/url_request_unittest.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using base::Time; | 17 using base::Time; |
| 18 using base::TimeDelta; | 18 using base::TimeDelta; |
| 19 | 19 |
| 20 // TODO(eroman): Add a regression test for http://crbug.com/40505. | 20 // TODO(eroman): Add a regression test for http://crbug.com/40505. |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const wchar_t kDocRoot[] = L"chrome/test/data"; | 24 const wchar_t kDocRoot[] = L"chrome/test/data"; |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 } | 424 } |
| 425 | 425 |
| 426 void URLFetcherCancelTest::CancelRequest() { | 426 void URLFetcherCancelTest::CancelRequest() { |
| 427 delete fetcher_; | 427 delete fetcher_; |
| 428 // The URLFetcher's test context will post a Quit task once it is | 428 // The URLFetcher's test context will post a Quit task once it is |
| 429 // deleted. So if this test simply hangs, it means cancellation | 429 // deleted. So if this test simply hangs, it means cancellation |
| 430 // did not work. | 430 // did not work. |
| 431 } | 431 } |
| 432 | 432 |
| 433 TEST_F(URLFetcherTest, SameThreadsTest) { | 433 TEST_F(URLFetcherTest, SameThreadsTest) { |
| 434 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 434 scoped_refptr<net::HTTPTestServer> server( |
| 435 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 435 ASSERT_TRUE(NULL != server.get()); | 436 ASSERT_TRUE(NULL != server.get()); |
| 436 | 437 |
| 437 // Create the fetcher on the main thread. Since IO will happen on the main | 438 // Create the fetcher on the main thread. Since IO will happen on the main |
| 438 // thread, this will test URLFetcher's ability to do everything on one | 439 // thread, this will test URLFetcher's ability to do everything on one |
| 439 // thread. | 440 // thread. |
| 440 CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); | 441 CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); |
| 441 | 442 |
| 442 MessageLoop::current()->Run(); | 443 MessageLoop::current()->Run(); |
| 443 } | 444 } |
| 444 | 445 |
| 445 TEST_F(URLFetcherTest, DifferentThreadsTest) { | 446 TEST_F(URLFetcherTest, DifferentThreadsTest) { |
| 446 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 447 scoped_refptr<net::HTTPTestServer> server( |
| 448 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 447 ASSERT_TRUE(NULL != server.get()); | 449 ASSERT_TRUE(NULL != server.get()); |
| 448 | 450 |
| 449 // Create a separate thread that will create the URLFetcher. The current | 451 // Create a separate thread that will create the URLFetcher. The current |
| 450 // (main) thread will do the IO, and when the fetch is complete it will | 452 // (main) thread will do the IO, and when the fetch is complete it will |
| 451 // terminate the main thread's message loop; then the other thread's | 453 // terminate the main thread's message loop; then the other thread's |
| 452 // message loop will be shut down automatically as the thread goes out of | 454 // message loop will be shut down automatically as the thread goes out of |
| 453 // scope. | 455 // scope. |
| 454 base::Thread t("URLFetcher test thread"); | 456 base::Thread t("URLFetcher test thread"); |
| 455 ASSERT_TRUE(t.Start()); | 457 ASSERT_TRUE(t.Start()); |
| 456 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, | 458 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, |
| 457 GURL(server->TestServerPage("defaultresponse")))); | 459 GURL(server->TestServerPage("defaultresponse")))); |
| 458 | 460 |
| 459 MessageLoop::current()->Run(); | 461 MessageLoop::current()->Run(); |
| 460 } | 462 } |
| 461 | 463 |
| 462 TEST_F(URLFetcherPostTest, Basic) { | 464 TEST_F(URLFetcherPostTest, Basic) { |
| 463 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 465 scoped_refptr<net::HTTPTestServer> server( |
| 466 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 464 ASSERT_TRUE(NULL != server.get()); | 467 ASSERT_TRUE(NULL != server.get()); |
| 465 CreateFetcher(GURL(server->TestServerPage("echo"))); | 468 CreateFetcher(GURL(server->TestServerPage("echo"))); |
| 466 MessageLoop::current()->Run(); | 469 MessageLoop::current()->Run(); |
| 467 } | 470 } |
| 468 | 471 |
| 469 TEST_F(URLFetcherHeadersTest, Headers) { | 472 TEST_F(URLFetcherHeadersTest, Headers) { |
| 470 scoped_refptr<HTTPTestServer> server = | 473 scoped_refptr<net::HTTPTestServer> server = |
| 471 HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); | 474 net::HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); |
| 472 ASSERT_TRUE(NULL != server.get()); | 475 ASSERT_TRUE(NULL != server.get()); |
| 473 CreateFetcher(GURL(server->TestServerPage("files/with-headers.html"))); | 476 CreateFetcher(GURL(server->TestServerPage("files/with-headers.html"))); |
| 474 MessageLoop::current()->Run(); | 477 MessageLoop::current()->Run(); |
| 475 // The actual tests are in the URLFetcherHeadersTest fixture. | 478 // The actual tests are in the URLFetcherHeadersTest fixture. |
| 476 } | 479 } |
| 477 | 480 |
| 478 TEST_F(URLFetcherProtectTest, Overload) { | 481 TEST_F(URLFetcherProtectTest, Overload) { |
| 479 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 482 scoped_refptr<net::HTTPTestServer> server( |
| 483 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 480 ASSERT_TRUE(NULL != server.get()); | 484 ASSERT_TRUE(NULL != server.get()); |
| 481 GURL url = GURL(server->TestServerPage("defaultresponse")); | 485 GURL url = GURL(server->TestServerPage("defaultresponse")); |
| 482 | 486 |
| 483 // Registers an entry for test url. It only allows 3 requests to be sent | 487 // Registers an entry for test url. It only allows 3 requests to be sent |
| 484 // in 200 milliseconds. | 488 // in 200 milliseconds. |
| 485 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); | 489 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); |
| 486 URLFetcherProtectEntry* entry = | 490 URLFetcherProtectEntry* entry = |
| 487 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); | 491 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); |
| 488 manager->Register(url.host(), entry); | 492 manager->Register(url.host(), entry); |
| 489 | 493 |
| 490 CreateFetcher(url); | 494 CreateFetcher(url); |
| 491 | 495 |
| 492 MessageLoop::current()->Run(); | 496 MessageLoop::current()->Run(); |
| 493 } | 497 } |
| 494 | 498 |
| 495 TEST_F(URLFetcherProtectTest, ServerUnavailable) { | 499 TEST_F(URLFetcherProtectTest, ServerUnavailable) { |
| 496 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 500 scoped_refptr<net::HTTPTestServer> server( |
| 501 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 497 ASSERT_TRUE(NULL != server.get()); | 502 ASSERT_TRUE(NULL != server.get()); |
| 498 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); | 503 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); |
| 499 | 504 |
| 500 // Registers an entry for test url. The backoff time is calculated by: | 505 // Registers an entry for test url. The backoff time is calculated by: |
| 501 // new_backoff = 2.0 * old_backoff + 0 | 506 // new_backoff = 2.0 * old_backoff + 0 |
| 502 // and maximum backoff time is 256 milliseconds. | 507 // and maximum backoff time is 256 milliseconds. |
| 503 // Maximum retries allowed is set to 11. | 508 // Maximum retries allowed is set to 11. |
| 504 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); | 509 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); |
| 505 URLFetcherProtectEntry* entry = | 510 URLFetcherProtectEntry* entry = |
| 506 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); | 511 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); |
| 507 manager->Register(url.host(), entry); | 512 manager->Register(url.host(), entry); |
| 508 | 513 |
| 509 CreateFetcher(url); | 514 CreateFetcher(url); |
| 510 | 515 |
| 511 MessageLoop::current()->Run(); | 516 MessageLoop::current()->Run(); |
| 512 } | 517 } |
| 513 | 518 |
| 514 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { | 519 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { |
| 515 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 520 scoped_refptr<net::HTTPTestServer> server( |
| 521 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 516 ASSERT_TRUE(NULL != server.get()); | 522 ASSERT_TRUE(NULL != server.get()); |
| 517 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); | 523 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); |
| 518 | 524 |
| 519 // Registers an entry for test url. The backoff time is calculated by: | 525 // Registers an entry for test url. The backoff time is calculated by: |
| 520 // new_backoff = 2.0 * old_backoff + 0 | 526 // new_backoff = 2.0 * old_backoff + 0 |
| 521 // and maximum backoff time is 256 milliseconds. | 527 // and maximum backoff time is 256 milliseconds. |
| 522 // Maximum retries allowed is set to 11. | 528 // Maximum retries allowed is set to 11. |
| 523 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); | 529 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); |
| 524 // Total time if *not* for not doing automatic backoff would be 150s. | 530 // Total time if *not* for not doing automatic backoff would be 150s. |
| 525 // In reality it should be "as soon as server responds". | 531 // In reality it should be "as soon as server responds". |
| 526 URLFetcherProtectEntry* entry = | 532 URLFetcherProtectEntry* entry = |
| 527 new URLFetcherProtectEntry(200, 3, 11, 100, 2.0, 0, 150000); | 533 new URLFetcherProtectEntry(200, 3, 11, 100, 2.0, 0, 150000); |
| 528 manager->Register(url.host(), entry); | 534 manager->Register(url.host(), entry); |
| 529 | 535 |
| 530 CreateFetcher(url); | 536 CreateFetcher(url); |
| 531 | 537 |
| 532 MessageLoop::current()->Run(); | 538 MessageLoop::current()->Run(); |
| 533 } | 539 } |
| 534 | 540 |
| 535 | 541 |
| 536 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) { | 542 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) { |
| 537 scoped_refptr<HTTPSTestServer> server = | 543 scoped_refptr<net::HTTPSTestServer> server = |
| 538 HTTPSTestServer::CreateExpiredServer(kDocRoot); | 544 net::HTTPSTestServer::CreateExpiredServer(kDocRoot); |
| 539 ASSERT_TRUE(NULL != server.get()); | 545 ASSERT_TRUE(NULL != server.get()); |
| 540 | 546 |
| 541 CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); | 547 CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); |
| 542 | 548 |
| 543 MessageLoop::current()->Run(); | 549 MessageLoop::current()->Run(); |
| 544 } | 550 } |
| 545 | 551 |
| 546 TEST_F(URLFetcherCancelTest, ReleasesContext) { | 552 TEST_F(URLFetcherCancelTest, ReleasesContext) { |
| 547 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 553 scoped_refptr<net::HTTPTestServer> server( |
| 554 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 548 ASSERT_TRUE(NULL != server.get()); | 555 ASSERT_TRUE(NULL != server.get()); |
| 549 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); | 556 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); |
| 550 | 557 |
| 551 // Registers an entry for test url. The backoff time is calculated by: | 558 // Registers an entry for test url. The backoff time is calculated by: |
| 552 // new_backoff = 2.0 * old_backoff + 0 | 559 // new_backoff = 2.0 * old_backoff + 0 |
| 553 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 560 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
| 554 // Maximum retries allowed is set to 2. | 561 // Maximum retries allowed is set to 2. |
| 555 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); | 562 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); |
| 556 URLFetcherProtectEntry* entry = | 563 URLFetcherProtectEntry* entry = |
| 557 new URLFetcherProtectEntry(200, 3, 2, 2000, 2.0, 0, 4000); | 564 new URLFetcherProtectEntry(200, 3, 2, 2000, 2.0, 0, 4000); |
| 558 manager->Register(url.host(), entry); | 565 manager->Register(url.host(), entry); |
| 559 | 566 |
| 560 // Create a separate thread that will create the URLFetcher. The current | 567 // Create a separate thread that will create the URLFetcher. The current |
| 561 // (main) thread will do the IO, and when the fetch is complete it will | 568 // (main) thread will do the IO, and when the fetch is complete it will |
| 562 // terminate the main thread's message loop; then the other thread's | 569 // terminate the main thread's message loop; then the other thread's |
| 563 // message loop will be shut down automatically as the thread goes out of | 570 // message loop will be shut down automatically as the thread goes out of |
| 564 // scope. | 571 // scope. |
| 565 base::Thread t("URLFetcher test thread"); | 572 base::Thread t("URLFetcher test thread"); |
| 566 ASSERT_TRUE(t.Start()); | 573 ASSERT_TRUE(t.Start()); |
| 567 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); | 574 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); |
| 568 | 575 |
| 569 MessageLoop::current()->Run(); | 576 MessageLoop::current()->Run(); |
| 570 } | 577 } |
| 571 | 578 |
| 572 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { | 579 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { |
| 573 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 580 scoped_refptr<net::HTTPTestServer> server( |
| 581 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 574 ASSERT_TRUE(NULL != server.get()); | 582 ASSERT_TRUE(NULL != server.get()); |
| 575 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); | 583 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); |
| 576 | 584 |
| 577 // Register an entry for test url. | 585 // Register an entry for test url. |
| 578 // | 586 // |
| 579 // Ideally we would mock URLFetcherProtectEntry to return XXX seconds | 587 // Ideally we would mock URLFetcherProtectEntry to return XXX seconds |
| 580 // in response to entry->UpdateBackoff(SEND). | 588 // in response to entry->UpdateBackoff(SEND). |
| 581 // | 589 // |
| 582 // Unfortunately this function is time sensitive, so we fudge some numbers | 590 // Unfortunately this function is time sensitive, so we fudge some numbers |
| 583 // to make it at least somewhat likely to have a non-zero deferred | 591 // to make it at least somewhat likely to have a non-zero deferred |
| (...skipping 13 matching lines...) Expand all Loading... |
| 597 // time difference from now). | 605 // time difference from now). |
| 598 | 606 |
| 599 base::Thread t("URLFetcher test thread"); | 607 base::Thread t("URLFetcher test thread"); |
| 600 ASSERT_TRUE(t.Start()); | 608 ASSERT_TRUE(t.Start()); |
| 601 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); | 609 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); |
| 602 | 610 |
| 603 MessageLoop::current()->Run(); | 611 MessageLoop::current()->Run(); |
| 604 } | 612 } |
| 605 | 613 |
| 606 } // namespace. | 614 } // namespace. |
| OLD | NEW |