Index: net/http/http_cache_unittest.cc |
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc |
index 519936829b735347b954a8506a88a24891391e0a..df651790928d2484a07011ee625672ce49146bf1 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" |
@@ -6773,6 +6775,110 @@ 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()->set_clock_for_test(make_scoped_ptr(clock_)); |
+ cache_.network_layer()->SetClock(clock_); |
+ |
+ transaction_.response_headers = "Cache-Control: max-age=100\n"; |
+ } |
+ |
+ 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.
|
+ int pre_transaction_count = transaction_count(); |
+ |
+ transaction_.load_flags = load_flags; |
+ |
+ 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.
|
+ RunTransactionAndGetReceivedBytes(cache_, transaction_); |
+ |
+ bool network_used = pre_transaction_count != transaction_count(); |
+ EXPECT_EQ(network_used ? TransactionSize(transaction_) : 0, received_bytes); |
+ |
+ return network_used; |
+ } |
+ |
+ 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(TransactionRequiresNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(prefetch_reuse_mins() * net::kNumSecondsPerMinute); |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_ONLY_FROM_CACHE)); |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_BYPASS_CACHE)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, |
+ SkipValidationOnExistingEntryThatNeedsValidation) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_F(HttpCachePrefetchValidationTest, |
+ SkipValidationOnExistingEntryThatDoesNotNeedValidation) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+ EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+ EXPECT_TRUE(TransactionRequiresNetwork(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 |