Index: net/http/http_cache_unittest.cc |
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc |
index d38d4559a649414e420efd02706304b7979f4e43..f51ffb2b72c5f28d75d2878880f2dc09d77cb070 100644 |
--- a/net/http/http_cache_unittest.cc |
+++ b/net/http/http_cache_unittest.cc |
@@ -13,6 +13,7 @@ |
#include "base/run_loop.h" |
#include "base/strings/string_util.h" |
#include "base/strings/stringprintf.h" |
+#include "base/test/simple_test_clock.h" |
#include "net/base/cache_type.h" |
#include "net/base/elements_upload_data_stream.h" |
#include "net/base/host_port_pair.h" |
@@ -25,6 +26,7 @@ |
#include "net/cert/cert_status_flags.h" |
#include "net/disk_cache/disk_cache.h" |
#include "net/http/http_byte_range.h" |
+#include "net/http/http_cache_transaction.h" |
#include "net/http/http_request_headers.h" |
#include "net/http/http_request_info.h" |
#include "net/http/http_response_headers.h" |
@@ -7025,6 +7027,120 @@ TEST(HttpCache, ReceivedBytesRange) { |
RemoveMockTransaction(&kRangeGET_TransactionOK); |
} |
+class HttpCachePrefetchValidationTest : public ::testing::Test { |
+ protected: |
+ static const int kMaxAge = 100; |
+ static const int kRequireValidation = kMaxAge + 1; |
+ |
+ HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { |
+ DCHECK_LT(kMaxAge, prefetch_reuse_mins() * net::kNumSecondsPerMinute); |
+ |
+ clock_ = new base::SimpleTestClock(); |
+ cache_.http_cache()->SetClockForTesting(make_scoped_ptr(clock_)); |
+ cache_.network_layer()->set_clock(clock_); |
+ |
+ transaction_.response_headers = "Cache-Control: max-age=100\n"; |
+ } |
+ |
+ bool TransactionRequiredNetwork(int load_flags) { |
+ int pre_transaction_count = transaction_count(); |
+ transaction_.load_flags = load_flags; |
+ 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.
|
+ MockHttpRequest(transaction_), NULL, |
+ net::BoundNetLog(), NULL, NULL); |
+ return pre_transaction_count != transaction_count(); |
+ } |
+ |
+ void AdvanceTime(int seconds) { |
+ clock_->Advance(base::TimeDelta::FromSeconds(seconds)); |
+ } |
+ |
+ int prefetch_reuse_mins() { return net::HttpCache::kPrefetchReuseMins; } |
+ |
+ // How many times this test has sent requests to the (fake) origin |
+ // server. Every test case needs to make at least one request to initialise |
+ // the cache. |
+ int transaction_count() { |
+ return cache_.network_layer()->transaction_count(); |
+ } |
+ |
+ // How many times an existing cache entry was opened during the test case. |
+ int open_count() { return cache_.disk_cache()->open_count(); } |
+ |
+ MockHttpCache cache_; |
+ ScopedMockTransaction transaction_; |
+ std::string response_headers_; |
+ base::SimpleTestClock* clock_; |
+}; |
+ |
+TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) { |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) { |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(prefetch_reuse_mins() * net::kNumSecondsPerMinute); |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_ONLY_FROM_CACHE)); |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_BYPASS_CACHE)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, |
+ SkipValidationOnExistingEntryThatNeedsValidation) { |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, |
+ SkipValidationOnExistingEntryThatDoesNotNeedValidation) { |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, PrefetchMultipleTimes) { |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) { |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); |
+} |
+ |
// Framework for tests of stale-while-revalidate related functionality. With |
// the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it |
// will trigger the stale-while-revalidate asynchronous revalidation. Setting |