| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
| 6 | 6 |
| 7 #include "base/hash_tables.h" | 7 #include "base/hash_tables.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/base/load_flags.h" | 11 #include "net/base/load_flags.h" |
| 12 #include "net/base/load_log_unittest.h" |
| 12 #include "net/base/ssl_cert_request_info.h" | 13 #include "net/base/ssl_cert_request_info.h" |
| 13 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
| 14 #include "net/http/http_byte_range.h" | 15 #include "net/http/http_byte_range.h" |
| 15 #include "net/http/http_request_info.h" | 16 #include "net/http/http_request_info.h" |
| 16 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 17 #include "net/http/http_response_info.h" | 18 #include "net/http/http_response_info.h" |
| 18 #include "net/http/http_transaction.h" | 19 #include "net/http/http_transaction.h" |
| 19 #include "net/http/http_transaction_unittest.h" | 20 #include "net/http/http_transaction_unittest.h" |
| 20 #include "net/http/http_util.h" | 21 #include "net/http/http_util.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 void ReadAndVerifyTransaction(net::HttpTransaction* trans, | 378 void ReadAndVerifyTransaction(net::HttpTransaction* trans, |
| 378 const MockTransaction& trans_info) { | 379 const MockTransaction& trans_info) { |
| 379 std::string content; | 380 std::string content; |
| 380 int rv = ReadTransaction(trans, &content); | 381 int rv = ReadTransaction(trans, &content); |
| 381 | 382 |
| 382 EXPECT_EQ(net::OK, rv); | 383 EXPECT_EQ(net::OK, rv); |
| 383 std::string expected(trans_info.data); | 384 std::string expected(trans_info.data); |
| 384 EXPECT_EQ(expected, content); | 385 EXPECT_EQ(expected, content); |
| 385 } | 386 } |
| 386 | 387 |
| 387 void RunTransactionTestWithRequest(net::HttpCache* cache, | 388 void RunTransactionTestWithRequestAndLog(net::HttpCache* cache, |
| 388 const MockTransaction& trans_info, | 389 const MockTransaction& trans_info, |
| 389 const MockHttpRequest& request, | 390 const MockHttpRequest& request, |
| 390 net::HttpResponseInfo* response_info) { | 391 net::HttpResponseInfo* response_info, |
| 392 net::LoadLog* load_log) { |
| 391 TestCompletionCallback callback; | 393 TestCompletionCallback callback; |
| 392 | 394 |
| 393 // write to the cache | 395 // write to the cache |
| 394 | 396 |
| 395 scoped_ptr<net::HttpTransaction> trans(cache->CreateTransaction()); | 397 scoped_ptr<net::HttpTransaction> trans(cache->CreateTransaction()); |
| 396 ASSERT_TRUE(trans.get()); | 398 ASSERT_TRUE(trans.get()); |
| 397 | 399 |
| 398 int rv = trans->Start(&request, &callback, NULL); | 400 int rv = trans->Start(&request, &callback, load_log); |
| 399 if (rv == net::ERR_IO_PENDING) | 401 if (rv == net::ERR_IO_PENDING) |
| 400 rv = callback.WaitForResult(); | 402 rv = callback.WaitForResult(); |
| 401 ASSERT_EQ(net::OK, rv); | 403 ASSERT_EQ(net::OK, rv); |
| 402 | 404 |
| 403 const net::HttpResponseInfo* response = trans->GetResponseInfo(); | 405 const net::HttpResponseInfo* response = trans->GetResponseInfo(); |
| 404 ASSERT_TRUE(response); | 406 ASSERT_TRUE(response); |
| 405 | 407 |
| 406 if (response_info) | 408 if (response_info) |
| 407 *response_info = *response; | 409 *response_info = *response; |
| 408 | 410 |
| 409 ReadAndVerifyTransaction(trans.get(), trans_info); | 411 ReadAndVerifyTransaction(trans.get(), trans_info); |
| 410 } | 412 } |
| 411 | 413 |
| 414 void RunTransactionTestWithRequest(net::HttpCache* cache, |
| 415 const MockTransaction& trans_info, |
| 416 const MockHttpRequest& request, |
| 417 net::HttpResponseInfo* response_info) { |
| 418 RunTransactionTestWithRequestAndLog(cache, trans_info, request, |
| 419 response_info, NULL); |
| 420 } |
| 421 |
| 422 void RunTransactionTestWithLog(net::HttpCache* cache, |
| 423 const MockTransaction& trans_info, |
| 424 net::LoadLog* log) { |
| 425 RunTransactionTestWithRequestAndLog( |
| 426 cache, trans_info, MockHttpRequest(trans_info), NULL, log); |
| 427 } |
| 428 |
| 412 void RunTransactionTest(net::HttpCache* cache, | 429 void RunTransactionTest(net::HttpCache* cache, |
| 413 const MockTransaction& trans_info) { | 430 const MockTransaction& trans_info) { |
| 414 RunTransactionTestWithRequest( | 431 RunTransactionTestWithLog(cache, trans_info, NULL); |
| 415 cache, trans_info, MockHttpRequest(trans_info), NULL); | |
| 416 } | 432 } |
| 417 | 433 |
| 418 void RunTransactionTestWithResponseInfo(net::HttpCache* cache, | 434 void RunTransactionTestWithResponseInfo(net::HttpCache* cache, |
| 419 const MockTransaction& trans_info, | 435 const MockTransaction& trans_info, |
| 420 net::HttpResponseInfo* response) { | 436 net::HttpResponseInfo* response) { |
| 421 RunTransactionTestWithRequest( | 437 RunTransactionTestWithRequest( |
| 422 cache, trans_info, MockHttpRequest(trans_info), response); | 438 cache, trans_info, MockHttpRequest(trans_info), response); |
| 423 } | 439 } |
| 424 | 440 |
| 425 void RunTransactionTestWithResponse(net::HttpCache* cache, | 441 void RunTransactionTestWithResponse(net::HttpCache* cache, |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 665 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 650 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 666 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 651 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 667 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 652 } | 668 } |
| 653 | 669 |
| 654 TEST(HttpCache, SimpleGETNoDiskCache) { | 670 TEST(HttpCache, SimpleGETNoDiskCache) { |
| 655 MockHttpCache cache; | 671 MockHttpCache cache; |
| 656 | 672 |
| 657 cache.disk_cache()->set_fail_requests(); | 673 cache.disk_cache()->set_fail_requests(); |
| 658 | 674 |
| 675 scoped_refptr<net::LoadLog> log(new net::LoadLog); |
| 676 |
| 659 // Read from the network, and don't use the cache. | 677 // Read from the network, and don't use the cache. |
| 660 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 678 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, log); |
| 679 |
| 680 // Check that the LoadLog was filled as expected. |
| 681 // (We attempted to both Open and Create entries, but both failed). |
| 682 EXPECT_EQ(4u, log->events().size()); |
| 683 net::ExpectLogContains(log, 0, net::LoadLog::TYPE_HTTP_CACHE_OPEN_ENTRY, |
| 684 net::LoadLog::PHASE_BEGIN); |
| 685 net::ExpectLogContains(log, 1, net::LoadLog::TYPE_HTTP_CACHE_OPEN_ENTRY, |
| 686 net::LoadLog::PHASE_END); |
| 687 net::ExpectLogContains(log, 2, net::LoadLog::TYPE_HTTP_CACHE_CREATE_ENTRY, |
| 688 net::LoadLog::PHASE_BEGIN); |
| 689 net::ExpectLogContains(log, 3, net::LoadLog::TYPE_HTTP_CACHE_CREATE_ENTRY, |
| 690 net::LoadLog::PHASE_END); |
| 661 | 691 |
| 662 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 692 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 663 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 693 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 664 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 694 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 665 } | 695 } |
| 666 | 696 |
| 667 TEST(HttpCache, SimpleGETWithDiskFailures) { | 697 TEST(HttpCache, SimpleGETWithDiskFailures) { |
| 668 MockHttpCache cache; | 698 MockHttpCache cache; |
| 669 | 699 |
| 670 cache.disk_cache()->set_soft_failures(true); | 700 cache.disk_cache()->set_soft_failures(true); |
| 671 | 701 |
| 672 // Read from the network, and fail to write to the cache. | 702 // Read from the network, and fail to write to the cache. |
| 673 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 703 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 674 | 704 |
| 675 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 705 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 676 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 706 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 677 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 707 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 678 | 708 |
| 679 // This one should see an empty cache again. | 709 // This one should see an empty cache again. |
| 680 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 710 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 681 | 711 |
| 682 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 712 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 683 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 713 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 684 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 714 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 685 } | 715 } |
| 686 | 716 |
| 687 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { | 717 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { |
| 688 MockHttpCache cache; | 718 MockHttpCache cache; |
| 689 | 719 |
| 720 scoped_refptr<net::LoadLog> log(new net::LoadLog); |
| 721 |
| 690 // write to the cache | 722 // write to the cache |
| 691 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 723 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, log); |
| 724 |
| 725 // Check that the LoadLog was filled as expected. |
| 726 EXPECT_EQ(6u, log->events().size()); |
| 727 net::ExpectLogContains(log, 0, net::LoadLog::TYPE_HTTP_CACHE_OPEN_ENTRY, |
| 728 net::LoadLog::PHASE_BEGIN); |
| 729 net::ExpectLogContains(log, 1, net::LoadLog::TYPE_HTTP_CACHE_OPEN_ENTRY, |
| 730 net::LoadLog::PHASE_END); |
| 731 net::ExpectLogContains(log, 2, net::LoadLog::TYPE_HTTP_CACHE_CREATE_ENTRY, |
| 732 net::LoadLog::PHASE_BEGIN); |
| 733 net::ExpectLogContains(log, 3, net::LoadLog::TYPE_HTTP_CACHE_CREATE_ENTRY, |
| 734 net::LoadLog::PHASE_END); |
| 735 net::ExpectLogContains(log, 4, net::LoadLog::TYPE_HTTP_CACHE_WAITING, |
| 736 net::LoadLog::PHASE_BEGIN); |
| 737 net::ExpectLogContains(log, 5, net::LoadLog::TYPE_HTTP_CACHE_WAITING, |
| 738 net::LoadLog::PHASE_END); |
| 692 | 739 |
| 693 // force this transaction to read from the cache | 740 // force this transaction to read from the cache |
| 694 MockTransaction transaction(kSimpleGET_Transaction); | 741 MockTransaction transaction(kSimpleGET_Transaction); |
| 695 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 742 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 696 | 743 |
| 697 RunTransactionTest(cache.http_cache(), transaction); | 744 log = new net::LoadLog; |
| 745 |
| 746 RunTransactionTestWithLog(cache.http_cache(), transaction, log); |
| 747 |
| 748 // Check that the LoadLog was filled as expected. |
| 749 EXPECT_EQ(6u, log->events().size()); |
| 750 net::ExpectLogContains(log, 0, net::LoadLog::TYPE_HTTP_CACHE_OPEN_ENTRY, |
| 751 net::LoadLog::PHASE_BEGIN); |
| 752 net::ExpectLogContains(log, 1, net::LoadLog::TYPE_HTTP_CACHE_OPEN_ENTRY, |
| 753 net::LoadLog::PHASE_END); |
| 754 net::ExpectLogContains(log, 2, net::LoadLog::TYPE_HTTP_CACHE_WAITING, |
| 755 net::LoadLog::PHASE_BEGIN); |
| 756 net::ExpectLogContains(log, 3, net::LoadLog::TYPE_HTTP_CACHE_WAITING, |
| 757 net::LoadLog::PHASE_END); |
| 758 net::ExpectLogContains(log, 4, net::LoadLog::TYPE_HTTP_CACHE_READ_INFO, |
| 759 net::LoadLog::PHASE_BEGIN); |
| 760 net::ExpectLogContains(log, 5, net::LoadLog::TYPE_HTTP_CACHE_READ_INFO, |
| 761 net::LoadLog::PHASE_END); |
| 698 | 762 |
| 699 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 763 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 700 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 764 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 701 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 765 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 702 } | 766 } |
| 703 | 767 |
| 704 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { | 768 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { |
| 705 MockHttpCache cache; | 769 MockHttpCache cache; |
| 706 | 770 |
| 707 // force this transaction to read from the cache | 771 // force this transaction to read from the cache |
| (...skipping 2044 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2752 std::string headers; | 2816 std::string headers; |
| 2753 response.headers->GetNormalizedHeaders(&headers); | 2817 response.headers->GetNormalizedHeaders(&headers); |
| 2754 | 2818 |
| 2755 EXPECT_EQ("HTTP/1.1 200 OK\n" | 2819 EXPECT_EQ("HTTP/1.1 200 OK\n" |
| 2756 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | 2820 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2757 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | 2821 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2758 headers); | 2822 headers); |
| 2759 | 2823 |
| 2760 RemoveMockTransaction(&mock_network_response); | 2824 RemoveMockTransaction(&mock_network_response); |
| 2761 } | 2825 } |
| OLD | NEW |