Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(679)

Unified Diff: net/http/http_cache_unittest.cc

Issue 851503003: Update from https://crrev.com/311076 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | net/http/http_network_session.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/http/http_cache_unittest.cc
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc
index 30720ae76f78126289421ff193d75c793af4722c..16a5523566025a36d1491b0d1e4c19999b9fbb99 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"
@@ -7096,6 +7098,115 @@ TEST(HttpCache, ReceivedBytesRange) {
RemoveMockTransaction(&kRangeGET_TransactionOK);
}
+class HttpCachePrefetchValidationTest : public ::testing::Test {
+ protected:
+ static const int kMaxAgeSecs = 100;
+ static const int kRequireValidationSecs = kMaxAgeSecs + 1;
+
+ HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) {
+ DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * net::kNumSecondsPerMinute);
+
+ clock_ = new base::SimpleTestClock();
+ cache_.http_cache()->SetClockForTesting(make_scoped_ptr(clock_));
+ cache_.network_layer()->SetClock(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;
+ RunTransactionTest(cache_.http_cache(), transaction_);
+ 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();
+ }
+
+ MockHttpCache cache_;
+ ScopedMockTransaction transaction_;
+ std::string response_headers_;
+ base::SimpleTestClock* clock_;
+};
+
+TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) {
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
+ AdvanceTime(kRequireValidationSecs);
+ 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(kRequireValidationSecs);
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL));
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
+}
+
+TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) {
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
+ AdvanceTime(kRequireValidationSecs);
+ 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(kRequireValidationSecs);
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_BYPASS_CACHE));
+ AdvanceTime(kRequireValidationSecs);
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
+}
+
+TEST_F(HttpCachePrefetchValidationTest,
+ SkipValidationOnExistingEntryThatNeedsValidation) {
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL));
+ AdvanceTime(kRequireValidationSecs);
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
+ AdvanceTime(kRequireValidationSecs);
+ 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(kRequireValidationSecs);
+ 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(kRequireValidationSecs);
+ EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL));
+}
+
+TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) {
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
+ AdvanceTime(kRequireValidationSecs);
+ EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH));
+ AdvanceTime(kRequireValidationSecs);
+ 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
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | net/http/http_network_session.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698