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 6980 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7018 | 7020 |
7019 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. | 7021 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. |
7020 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | 7022 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; |
7021 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | 7023 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; |
7022 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | 7024 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); |
7023 EXPECT_EQ(range_response_size * 2, received_bytes); | 7025 EXPECT_EQ(range_response_size * 2, received_bytes); |
7024 | 7026 |
7025 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7027 RemoveMockTransaction(&kRangeGET_TransactionOK); |
7026 } | 7028 } |
7027 | 7029 |
7030 class HttpCachePrefetchValidationTest : public ::testing::Test { | |
7031 protected: | |
7032 static const int kMaxAge = 100; | |
7033 static const int kRequireValidation = kMaxAge + 1; | |
7034 | |
7035 HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { | |
7036 DCHECK_LT(kMaxAge, prefetch_reuse_mins() * net::kNumSecondsPerMinute); | |
7037 | |
7038 clock_ = new base::SimpleTestClock(); | |
7039 cache_.http_cache()->SetClockForTesting(make_scoped_ptr(clock_)); | |
7040 cache_.network_layer()->set_clock(clock_); | |
7041 | |
7042 transaction_.response_headers = "Cache-Control: max-age=100\n"; | |
7043 } | |
7044 | |
7045 bool TransactionRequiredNetwork(int load_flags) { | |
7046 int pre_transaction_count = transaction_count(); | |
7047 transaction_.load_flags = load_flags; | |
7048 RunTransactionTestBase(cache_.http_cache(), transaction_, | |
rvargas (doing something else)
2015/01/09 02:25:34
use RunTransacionTest
jkarlin
2015/01/09 14:03:50
Done.
| |
7049 MockHttpRequest(transaction_), NULL, | |
7050 net::BoundNetLog(), NULL, NULL); | |
7051 return pre_transaction_count != transaction_count(); | |
7052 } | |
7053 | |
7054 void AdvanceTime(int seconds) { | |
7055 clock_->Advance(base::TimeDelta::FromSeconds(seconds)); | |
7056 } | |
7057 | |
7058 int prefetch_reuse_mins() { return net::HttpCache::kPrefetchReuseMins; } | |
7059 | |
7060 // How many times this test has sent requests to the (fake) origin | |
7061 // server. Every test case needs to make at least one request to initialise | |
7062 // the cache. | |
7063 int transaction_count() { | |
7064 return cache_.network_layer()->transaction_count(); | |
7065 } | |
7066 | |
7067 // How many times an existing cache entry was opened during the test case. | |
7068 int open_count() { return cache_.disk_cache()->open_count(); } | |
7069 | |
7070 MockHttpCache cache_; | |
7071 ScopedMockTransaction transaction_; | |
7072 std::string response_headers_; | |
7073 base::SimpleTestClock* clock_; | |
7074 }; | |
7075 | |
7076 TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) { | |
7077 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7078 AdvanceTime(kRequireValidation); | |
7079 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7080 } | |
7081 | |
7082 TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) { | |
7083 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7084 AdvanceTime(prefetch_reuse_mins() * net::kNumSecondsPerMinute); | |
7085 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7086 } | |
7087 | |
7088 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { | |
7089 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7090 AdvanceTime(kRequireValidation); | |
7091 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7092 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7093 } | |
7094 | |
7095 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { | |
7096 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7097 AdvanceTime(kRequireValidation); | |
7098 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_ONLY_FROM_CACHE)); | |
7099 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7100 } | |
7101 | |
7102 TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { | |
7103 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7104 AdvanceTime(kRequireValidation); | |
7105 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_BYPASS_CACHE)); | |
7106 AdvanceTime(kRequireValidation); | |
7107 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7108 } | |
7109 | |
7110 TEST_F(HttpCachePrefetchValidationTest, | |
7111 SkipValidationOnExistingEntryThatNeedsValidation) { | |
7112 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7113 AdvanceTime(kRequireValidation); | |
7114 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7115 AdvanceTime(kRequireValidation); | |
7116 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7117 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7118 } | |
7119 | |
7120 TEST_F(HttpCachePrefetchValidationTest, | |
7121 SkipValidationOnExistingEntryThatDoesNotNeedValidation) { | |
7122 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7123 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7124 AdvanceTime(kRequireValidation); | |
7125 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7126 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7127 } | |
7128 | |
7129 TEST_F(HttpCachePrefetchValidationTest, PrefetchMultipleTimes) { | |
7130 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7131 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7132 AdvanceTime(kRequireValidation); | |
7133 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7134 } | |
7135 | |
7136 TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) { | |
7137 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7138 AdvanceTime(kRequireValidation); | |
7139 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7140 AdvanceTime(kRequireValidation); | |
7141 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7142 } | |
7143 | |
7028 // Framework for tests of stale-while-revalidate related functionality. With | 7144 // Framework for tests of stale-while-revalidate related functionality. With |
7029 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it | 7145 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it |
7030 // will trigger the stale-while-revalidate asynchronous revalidation. Setting | 7146 // will trigger the stale-while-revalidate asynchronous revalidation. Setting |
7031 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause | 7147 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause |
7032 // synchronous revalidation. | 7148 // synchronous revalidation. |
7033 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { | 7149 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { |
7034 protected: | 7150 protected: |
7035 HttpCacheStaleWhileRevalidateTest() | 7151 HttpCacheStaleWhileRevalidateTest() |
7036 : transaction_(kSimpleGET_Transaction), | 7152 : transaction_(kSimpleGET_Transaction), |
7037 age_(3601), | 7153 age_(3601), |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
7593 | 7709 |
7594 // Here the second transaction proceeds without reading the first body. | 7710 // Here the second transaction proceeds without reading the first body. |
7595 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); | 7711 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); |
7596 base::MessageLoop::current()->RunUntilIdle(); | 7712 base::MessageLoop::current()->RunUntilIdle(); |
7597 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); | 7713 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); |
7598 ASSERT_TRUE(second->trans->GetResponseInfo()); | 7714 ASSERT_TRUE(second->trans->GetResponseInfo()); |
7599 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( | 7715 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( |
7600 "Cache-Control", "no-store")); | 7716 "Cache-Control", "no-store")); |
7601 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); | 7717 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); |
7602 } | 7718 } |
OLD | NEW |