OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/test/simple_test_clock.h" | |
16 #include "net/base/cache_type.h" | 17 #include "net/base/cache_type.h" |
17 #include "net/base/elements_upload_data_stream.h" | 18 #include "net/base/elements_upload_data_stream.h" |
18 #include "net/base/host_port_pair.h" | 19 #include "net/base/host_port_pair.h" |
19 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
20 #include "net/base/load_timing_info.h" | 21 #include "net/base/load_timing_info.h" |
21 #include "net/base/load_timing_info_test_util.h" | 22 #include "net/base/load_timing_info_test_util.h" |
22 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
23 #include "net/base/net_log_unittest.h" | 24 #include "net/base/net_log_unittest.h" |
24 #include "net/base/upload_bytes_element_reader.h" | 25 #include "net/base/upload_bytes_element_reader.h" |
25 #include "net/cert/cert_status_flags.h" | 26 #include "net/cert/cert_status_flags.h" |
26 #include "net/disk_cache/disk_cache.h" | 27 #include "net/disk_cache/disk_cache.h" |
27 #include "net/http/http_byte_range.h" | 28 #include "net/http/http_byte_range.h" |
29 #include "net/http/http_cache_transaction.h" | |
28 #include "net/http/http_request_headers.h" | 30 #include "net/http/http_request_headers.h" |
29 #include "net/http/http_request_info.h" | 31 #include "net/http/http_request_info.h" |
30 #include "net/http/http_response_headers.h" | 32 #include "net/http/http_response_headers.h" |
31 #include "net/http/http_response_info.h" | 33 #include "net/http/http_response_info.h" |
32 #include "net/http/http_transaction.h" | 34 #include "net/http/http_transaction.h" |
33 #include "net/http/http_transaction_test_util.h" | 35 #include "net/http/http_transaction_test_util.h" |
34 #include "net/http/http_util.h" | 36 #include "net/http/http_util.h" |
35 #include "net/http/mock_http_cache.h" | 37 #include "net/http/mock_http_cache.h" |
36 #include "net/socket/client_socket_handle.h" | 38 #include "net/socket/client_socket_handle.h" |
37 #include "net/ssl/ssl_cert_request_info.h" | 39 #include "net/ssl/ssl_cert_request_info.h" |
(...skipping 6728 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6766 | 6768 |
6767 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. | 6769 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. |
6768 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | 6770 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
6769 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | 6771 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
6770 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 6772 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); |
6771 EXPECT_EQ(range_response_size * 2, received_bytes); | 6773 EXPECT_EQ(range_response_size * 2, received_bytes); |
6772 | 6774 |
6773 RemoveMockTransaction(&kRangeGET_TransactionOK); | 6775 RemoveMockTransaction(&kRangeGET_TransactionOK); |
6774 } | 6776 } |
6775 | 6777 |
6778 class HttpCachePrefetchValidationTest : public ::testing::Test { | |
6779 protected: | |
6780 static const int kMaxAge = 100; | |
6781 static const int kRequireValidation = kMaxAge + 1; | |
6782 | |
6783 HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { | |
6784 DCHECK_LT(kMaxAge, prefetch_reuse_mins() * net::kNumSecondsPerMinute); | |
6785 | |
6786 clock_ = new base::SimpleTestClock(); | |
6787 cache_.http_cache()->set_clock_for_test(make_scoped_ptr(clock_)); | |
6788 cache_.network_layer()->SetClock(clock_); | |
6789 | |
6790 transaction_.response_headers = "Cache-Control: max-age=100\n"; | |
6791 } | |
6792 | |
6793 bool TransactionRequiresNetwork(int load_flags) { | |
rvargas (doing something else)
2014/12/24 02:15:25
Given that this method runs the transaction, maybe
jkarlin
2015/01/08 16:13:04
Done.
| |
6794 int pre_transaction_count = transaction_count(); | |
6795 | |
6796 transaction_.load_flags = load_flags; | |
6797 | |
6798 int64 received_bytes = | |
rvargas (doing something else)
2014/12/24 02:15:25
I'm wondering if this is not a distraction for thi
jkarlin
2015/01/08 16:13:04
Done.
| |
6799 RunTransactionAndGetReceivedBytes(cache_, transaction_); | |
6800 | |
6801 bool network_used = pre_transaction_count != transaction_count(); | |
6802 EXPECT_EQ(network_used ? TransactionSize(transaction_) : 0, received_bytes); | |
6803 | |
6804 return network_used; | |
6805 } | |
6806 | |
6807 void AdvanceTime(int seconds) { | |
6808 clock_->Advance(base::TimeDelta::FromSeconds(seconds)); | |
6809 } | |
6810 | |
6811 int prefetch_reuse_mins() { return net::HttpCache::kPrefetchReuseMins; } | |
6812 | |
6813 // How many times this test has sent requests to the (fake) origin | |
6814 // server. Every test case needs to make at least one request to initialise | |
6815 // the cache. | |
6816 int transaction_count() { | |
6817 return cache_.network_layer()->transaction_count(); | |
6818 } | |
6819 | |
6820 // How many times an existing cache entry was opened during the test case. | |
6821 int open_count() { return cache_.disk_cache()->open_count(); } | |
6822 | |
6823 MockHttpCache cache_; | |
6824 ScopedMockTransaction transaction_; | |
6825 std::string response_headers_; | |
6826 base::SimpleTestClock* clock_; | |
6827 }; | |
6828 | |
6829 TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) { | |
6830 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); | |
6831 AdvanceTime(kRequireValidation); | |
6832 EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6833 } | |
6834 | |
6835 TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) { | |
6836 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); | |
6837 AdvanceTime(prefetch_reuse_mins() * net::kNumSecondsPerMinute); | |
6838 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6839 } | |
6840 | |
6841 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { | |
6842 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); | |
6843 AdvanceTime(kRequireValidation); | |
6844 EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6845 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6846 } | |
6847 | |
6848 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { | |
6849 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); | |
6850 AdvanceTime(kRequireValidation); | |
6851 EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_ONLY_FROM_CACHE)); | |
6852 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6853 } | |
6854 | |
6855 TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { | |
6856 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); | |
6857 AdvanceTime(kRequireValidation); | |
6858 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_BYPASS_CACHE)); | |
6859 AdvanceTime(kRequireValidation); | |
6860 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6861 } | |
6862 | |
6863 TEST_F(HttpCachePrefetchValidationTest, | |
6864 SkipValidationOnExistingEntryThatNeedsValidation) { | |
6865 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6866 AdvanceTime(kRequireValidation); | |
6867 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); | |
6868 AdvanceTime(kRequireValidation); | |
6869 EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6870 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6871 } | |
6872 | |
6873 TEST_F(HttpCachePrefetchValidationTest, | |
6874 SkipValidationOnExistingEntryThatDoesNotNeedValidation) { | |
6875 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6876 EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); | |
6877 AdvanceTime(kRequireValidation); | |
6878 EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6879 EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); | |
6880 } | |
6881 | |
6776 // Framework for tests of stale-while-revalidate related functionality. With | 6882 // Framework for tests of stale-while-revalidate related functionality. With |
6777 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it | 6883 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it |
6778 // will trigger the stale-while-revalidate asynchronous revalidation. Setting | 6884 // will trigger the stale-while-revalidate asynchronous revalidation. Setting |
6779 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause | 6885 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause |
6780 // synchronous revalidation. | 6886 // synchronous revalidation. |
6781 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { | 6887 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { |
6782 protected: | 6888 protected: |
6783 HttpCacheStaleWhileRevalidateTest() | 6889 HttpCacheStaleWhileRevalidateTest() |
6784 : transaction_(kSimpleGET_Transaction), | 6890 : transaction_(kSimpleGET_Transaction), |
6785 age_(3601), | 6891 age_(3601), |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7341 | 7447 |
7342 // Here the second transaction proceeds without reading the first body. | 7448 // Here the second transaction proceeds without reading the first body. |
7343 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); | 7449 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); |
7344 base::MessageLoop::current()->RunUntilIdle(); | 7450 base::MessageLoop::current()->RunUntilIdle(); |
7345 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); | 7451 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); |
7346 ASSERT_TRUE(second->trans->GetResponseInfo()); | 7452 ASSERT_TRUE(second->trans->GetResponseInfo()); |
7347 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( | 7453 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( |
7348 "Cache-Control", "no-store")); | 7454 "Cache-Control", "no-store")); |
7349 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); | 7455 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); |
7350 } | 7456 } |
OLD | NEW |