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" |
(...skipping 413 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)); |
| 435 ASSERT_TRUE(NULL != server.get()); |
| 436 |
434 // Create the fetcher on the main thread. Since IO will happen on the main | 437 // Create the fetcher on the main thread. Since IO will happen on the main |
435 // thread, this will test URLFetcher's ability to do everything on one | 438 // thread, this will test URLFetcher's ability to do everything on one |
436 // thread. | 439 // thread. |
437 scoped_refptr<HTTPTestServer> server = | |
438 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
439 ASSERT_TRUE(NULL != server.get()); | |
440 | |
441 CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); | 440 CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); |
442 | 441 |
443 MessageLoop::current()->Run(); | 442 MessageLoop::current()->Run(); |
444 } | 443 } |
445 | 444 |
446 TEST_F(URLFetcherTest, DifferentThreadsTest) { | 445 TEST_F(URLFetcherTest, DifferentThreadsTest) { |
447 scoped_refptr<HTTPTestServer> server = | 446 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
448 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
449 ASSERT_TRUE(NULL != server.get()); | 447 ASSERT_TRUE(NULL != server.get()); |
| 448 |
450 // Create a separate thread that will create the URLFetcher. The current | 449 // Create a separate thread that will create the URLFetcher. The current |
451 // (main) thread will do the IO, and when the fetch is complete it will | 450 // (main) thread will do the IO, and when the fetch is complete it will |
452 // terminate the main thread's message loop; then the other thread's | 451 // terminate the main thread's message loop; then the other thread's |
453 // message loop will be shut down automatically as the thread goes out of | 452 // message loop will be shut down automatically as the thread goes out of |
454 // scope. | 453 // scope. |
455 base::Thread t("URLFetcher test thread"); | 454 base::Thread t("URLFetcher test thread"); |
456 ASSERT_TRUE(t.Start()); | 455 ASSERT_TRUE(t.Start()); |
457 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, | 456 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, |
458 GURL(server->TestServerPage("defaultresponse")))); | 457 GURL(server->TestServerPage("defaultresponse")))); |
459 | 458 |
460 MessageLoop::current()->Run(); | 459 MessageLoop::current()->Run(); |
461 } | 460 } |
462 | 461 |
463 TEST_F(URLFetcherPostTest, Basic) { | 462 TEST_F(URLFetcherPostTest, Basic) { |
464 scoped_refptr<HTTPTestServer> server = | 463 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
465 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
466 ASSERT_TRUE(NULL != server.get()); | 464 ASSERT_TRUE(NULL != server.get()); |
467 CreateFetcher(GURL(server->TestServerPage("echo"))); | 465 CreateFetcher(GURL(server->TestServerPage("echo"))); |
468 MessageLoop::current()->Run(); | 466 MessageLoop::current()->Run(); |
469 } | 467 } |
470 | 468 |
471 TEST_F(URLFetcherHeadersTest, Headers) { | 469 TEST_F(URLFetcherHeadersTest, Headers) { |
472 scoped_refptr<HTTPTestServer> server = | 470 scoped_refptr<HTTPTestServer> server = |
473 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); | 471 HTTPTestServer::CreateServer(L"net/data/url_request_unittest"); |
474 ASSERT_TRUE(NULL != server.get()); | 472 ASSERT_TRUE(NULL != server.get()); |
475 CreateFetcher(GURL(server->TestServerPage("files/with-headers.html"))); | 473 CreateFetcher(GURL(server->TestServerPage("files/with-headers.html"))); |
476 MessageLoop::current()->Run(); | 474 MessageLoop::current()->Run(); |
477 // The actual tests are in the URLFetcherHeadersTest fixture. | 475 // The actual tests are in the URLFetcherHeadersTest fixture. |
478 } | 476 } |
479 | 477 |
480 TEST_F(URLFetcherProtectTest, Overload) { | 478 TEST_F(URLFetcherProtectTest, Overload) { |
481 scoped_refptr<HTTPTestServer> server = | 479 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
482 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
483 ASSERT_TRUE(NULL != server.get()); | 480 ASSERT_TRUE(NULL != server.get()); |
484 GURL url = GURL(server->TestServerPage("defaultresponse")); | 481 GURL url = GURL(server->TestServerPage("defaultresponse")); |
485 | 482 |
486 // Registers an entry for test url. It only allows 3 requests to be sent | 483 // Registers an entry for test url. It only allows 3 requests to be sent |
487 // in 200 milliseconds. | 484 // in 200 milliseconds. |
488 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); | 485 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); |
489 URLFetcherProtectEntry* entry = | 486 URLFetcherProtectEntry* entry = |
490 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); | 487 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); |
491 manager->Register(url.host(), entry); | 488 manager->Register(url.host(), entry); |
492 | 489 |
493 CreateFetcher(url); | 490 CreateFetcher(url); |
494 | 491 |
495 MessageLoop::current()->Run(); | 492 MessageLoop::current()->Run(); |
496 } | 493 } |
497 | 494 |
498 TEST_F(URLFetcherProtectTest, ServerUnavailable) { | 495 TEST_F(URLFetcherProtectTest, ServerUnavailable) { |
499 scoped_refptr<HTTPTestServer> server = | 496 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
500 HTTPTestServer::CreateServer(L"chrome/test/data", NULL); | |
501 ASSERT_TRUE(NULL != server.get()); | 497 ASSERT_TRUE(NULL != server.get()); |
502 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); | 498 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); |
503 | 499 |
504 // Registers an entry for test url. The backoff time is calculated by: | 500 // Registers an entry for test url. The backoff time is calculated by: |
505 // new_backoff = 2.0 * old_backoff + 0 | 501 // new_backoff = 2.0 * old_backoff + 0 |
506 // and maximum backoff time is 256 milliseconds. | 502 // and maximum backoff time is 256 milliseconds. |
507 // Maximum retries allowed is set to 11. | 503 // Maximum retries allowed is set to 11. |
508 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); | 504 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); |
509 URLFetcherProtectEntry* entry = | 505 URLFetcherProtectEntry* entry = |
510 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); | 506 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); |
511 manager->Register(url.host(), entry); | 507 manager->Register(url.host(), entry); |
512 | 508 |
513 CreateFetcher(url); | 509 CreateFetcher(url); |
514 | 510 |
515 MessageLoop::current()->Run(); | 511 MessageLoop::current()->Run(); |
516 } | 512 } |
517 | 513 |
518 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { | 514 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { |
519 scoped_refptr<HTTPTestServer> server = | 515 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
520 HTTPTestServer::CreateServer(L"chrome/test/data", NULL); | |
521 ASSERT_TRUE(NULL != server.get()); | 516 ASSERT_TRUE(NULL != server.get()); |
522 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); | 517 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); |
523 | 518 |
524 // Registers an entry for test url. The backoff time is calculated by: | 519 // Registers an entry for test url. The backoff time is calculated by: |
525 // new_backoff = 2.0 * old_backoff + 0 | 520 // new_backoff = 2.0 * old_backoff + 0 |
526 // and maximum backoff time is 256 milliseconds. | 521 // and maximum backoff time is 256 milliseconds. |
527 // Maximum retries allowed is set to 11. | 522 // Maximum retries allowed is set to 11. |
528 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); | 523 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); |
529 // Total time if *not* for not doing automatic backoff would be 150s. | 524 // Total time if *not* for not doing automatic backoff would be 150s. |
530 // In reality it should be "as soon as server responds". | 525 // In reality it should be "as soon as server responds". |
(...skipping 11 matching lines...) Expand all Loading... |
542 scoped_refptr<HTTPSTestServer> server = | 537 scoped_refptr<HTTPSTestServer> server = |
543 HTTPSTestServer::CreateExpiredServer(kDocRoot); | 538 HTTPSTestServer::CreateExpiredServer(kDocRoot); |
544 ASSERT_TRUE(NULL != server.get()); | 539 ASSERT_TRUE(NULL != server.get()); |
545 | 540 |
546 CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); | 541 CreateFetcher(GURL(server->TestServerPage("defaultresponse"))); |
547 | 542 |
548 MessageLoop::current()->Run(); | 543 MessageLoop::current()->Run(); |
549 } | 544 } |
550 | 545 |
551 TEST_F(URLFetcherCancelTest, ReleasesContext) { | 546 TEST_F(URLFetcherCancelTest, ReleasesContext) { |
552 scoped_refptr<HTTPTestServer> server = | 547 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
553 HTTPTestServer::CreateServer(L"chrome/test/data", NULL); | |
554 ASSERT_TRUE(NULL != server.get()); | 548 ASSERT_TRUE(NULL != server.get()); |
555 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); | 549 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); |
556 | 550 |
557 // Registers an entry for test url. The backoff time is calculated by: | 551 // Registers an entry for test url. The backoff time is calculated by: |
558 // new_backoff = 2.0 * old_backoff + 0 | 552 // new_backoff = 2.0 * old_backoff + 0 |
559 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 553 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
560 // Maximum retries allowed is set to 2. | 554 // Maximum retries allowed is set to 2. |
561 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); | 555 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); |
562 URLFetcherProtectEntry* entry = | 556 URLFetcherProtectEntry* entry = |
563 new URLFetcherProtectEntry(200, 3, 2, 2000, 2.0, 0, 4000); | 557 new URLFetcherProtectEntry(200, 3, 2, 2000, 2.0, 0, 4000); |
564 manager->Register(url.host(), entry); | 558 manager->Register(url.host(), entry); |
565 | 559 |
566 // Create a separate thread that will create the URLFetcher. The current | 560 // Create a separate thread that will create the URLFetcher. The current |
567 // (main) thread will do the IO, and when the fetch is complete it will | 561 // (main) thread will do the IO, and when the fetch is complete it will |
568 // terminate the main thread's message loop; then the other thread's | 562 // terminate the main thread's message loop; then the other thread's |
569 // message loop will be shut down automatically as the thread goes out of | 563 // message loop will be shut down automatically as the thread goes out of |
570 // scope. | 564 // scope. |
571 base::Thread t("URLFetcher test thread"); | 565 base::Thread t("URLFetcher test thread"); |
572 ASSERT_TRUE(t.Start()); | 566 ASSERT_TRUE(t.Start()); |
573 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); | 567 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); |
574 | 568 |
575 MessageLoop::current()->Run(); | 569 MessageLoop::current()->Run(); |
576 } | 570 } |
577 | 571 |
578 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { | 572 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { |
579 scoped_refptr<HTTPTestServer> server = | 573 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
580 HTTPTestServer::CreateServer(L"chrome/test/data", NULL); | |
581 ASSERT_TRUE(NULL != server.get()); | 574 ASSERT_TRUE(NULL != server.get()); |
582 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); | 575 GURL url = GURL(server->TestServerPage("files/server-unavailable.html")); |
583 | 576 |
584 // Register an entry for test url. | 577 // Register an entry for test url. |
585 // | 578 // |
586 // Ideally we would mock URLFetcherProtectEntry to return XXX seconds | 579 // Ideally we would mock URLFetcherProtectEntry to return XXX seconds |
587 // in response to entry->UpdateBackoff(SEND). | 580 // in response to entry->UpdateBackoff(SEND). |
588 // | 581 // |
589 // Unfortunately this function is time sensitive, so we fudge some numbers | 582 // Unfortunately this function is time sensitive, so we fudge some numbers |
590 // to make it at least somewhat likely to have a non-zero deferred | 583 // to make it at least somewhat likely to have a non-zero deferred |
(...skipping 13 matching lines...) Expand all Loading... |
604 // time difference from now). | 597 // time difference from now). |
605 | 598 |
606 base::Thread t("URLFetcher test thread"); | 599 base::Thread t("URLFetcher test thread"); |
607 ASSERT_TRUE(t.Start()); | 600 ASSERT_TRUE(t.Start()); |
608 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); | 601 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); |
609 | 602 |
610 MessageLoop::current()->Run(); | 603 MessageLoop::current()->Run(); |
611 } | 604 } |
612 | 605 |
613 } // namespace. | 606 } // namespace. |
OLD | NEW |