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..8e1a31419d1918919c7917ac0cc86186bbb4a730 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" |
@@ -40,6 +42,8 @@ |
using base::Time; |
+namespace net { |
+ |
namespace { |
// Tests the load timing values of a request that goes through a |
@@ -6773,6 +6777,125 @@ 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_(new ScopedMockTransaction(kSimpleGET_Transaction)) { |
rvargas (doing something else)
2014/12/12 02:20:50
This class is borderline to require separation bet
jkarlin
2014/12/15 17:19:25
I've removed the 206 tests as they're unnecessary,
|
+ DCHECK(kMaxAge < prefetch_reuse_mins() * kNumSecondsPerMinute) |
rvargas (doing something else)
2014/12/12 02:20:50
_GT
jkarlin
2014/12/15 17:19:25
Done, but _LT.
|
+ << "Test requirement"; |
rvargas (doing something else)
2014/12/12 02:20:50
Does this string really provide any info?
jkarlin
2014/12/15 17:19:25
Done.
|
+ clock_ = new base::SimpleTestClock(); |
+ cache_.http_cache()->clock_.reset(clock_); |
rvargas (doing something else)
2014/12/12 02:20:50
If the accessor is public (for tests), this class
jkarlin
2014/12/15 17:19:25
I somewhat agree on making the accessors public (a
rvargas (doing something else)
2014/12/24 02:15:24
If the name of the method clearly specify that it
|
+ cache_.network_layer()->SetClock(clock_); |
+ |
+ response_headers_ = base::StringPrintf( |
rvargas (doing something else)
2014/12/12 02:20:50
it is slightly weird to use StringPrintf with all
jkarlin
2014/12/15 17:19:25
Done. I only need this string once now so it's jus
|
+ "HTTP/1.1 200 OK\n" |
+ "Cache-Control: max-age=%d\n", |
+ kMaxAge); |
+ transaction_->response_headers = response_headers_.c_str(); |
+ |
+ if (ShouldUseRangeTransaction()) |
+ SetUpRangeTransaction(); |
+ } |
+ |
+ bool TransactionRequiresNetwork(int load_flags) { |
+ int pre_transaction_count = transaction_count(); |
+ |
+ transaction_->load_flags = load_flags; |
+ |
+ int64 received_bytes = |
+ RunTransactionAndGetReceivedBytes(cache_, *transaction_); |
rvargas (doing something else)
2014/12/12 02:20:50
you mean some form of get() ?
jkarlin
2014/12/15 17:19:25
No longer pertinent as transaction_ is no longer o
|
+ |
+ bool network_used = pre_transaction_count != transaction_count(); |
+ EXPECT_EQ(network_used ? TransactionSize(*transaction_) : 0, |
+ received_bytes); |
+ |
+ return network_used; |
+ } |
+ |
+ void SetUpRangeTransaction() { |
+ transaction_.reset(new ScopedMockTransaction(kRangeGET_TransactionOK)); |
+ response_headers_ = std::string(transaction_->response_headers); |
+ response_headers_ += |
+ base::StringPrintf("Cache-Control: max-age=%d\n", kMaxAge); |
+ |
+ transaction_->response_headers = response_headers_.c_str(); |
+ } |
+ |
+ virtual bool ShouldUseRangeTransaction() const { return false; } |
rvargas (doing something else)
2014/12/12 02:20:50
wait until this is actually used?
jkarlin
2014/12/15 17:19:25
I've deleted the range request parameterized test
|
+ |
+ 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_; |
+ scoped_ptr<ScopedMockTransaction> transaction_; |
rvargas (doing something else)
2014/12/12 02:20:50
why a pointer?
jkarlin
2014/12/15 17:19:25
So that the original transaction can be swapped at
|
+ std::string response_headers_; |
+ base::SimpleTestClock* clock_; |
+}; |
+ |
+class HttpCachePrefetchValidationTestP |
+ : public HttpCachePrefetchValidationTest, |
+ public testing::WithParamInterface<bool> { |
+ bool ShouldUseRangeTransaction() const override { return GetParam(); } |
+}; |
+ |
+TEST_P(HttpCachePrefetchValidationTestP, SkipValidationShortlyAfterPrefetch) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_P(HttpCachePrefetchValidationTestP, ValidateLongAfterPrefetch) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_PREFETCH)); |
+ AdvanceTime(prefetch_reuse_mins() * kNumSecondsPerMinute); |
+ EXPECT_TRUE(TransactionRequiresNetwork(net::LOAD_NORMAL)); |
+} |
+ |
+TEST_P(HttpCachePrefetchValidationTestP, SkipValidationOnceOnly) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiresNetwork(LOAD_NORMAL)); |
+ EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); |
+} |
+ |
+TEST_P(HttpCachePrefetchValidationTestP, |
+ SkipValidationOnExistingEntryThatNeedsValidation) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_TRUE(TransactionRequiresNetwork(LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiresNetwork(LOAD_NORMAL)); |
+ EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); |
+} |
+ |
+TEST_P(HttpCachePrefetchValidationTestP, |
+ SkipValidationOnExistingEntryThatDoesNotNeedValidation) { |
+ EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); |
+ EXPECT_FALSE(TransactionRequiresNetwork(LOAD_PREFETCH)); |
+ AdvanceTime(kRequireValidation); |
+ EXPECT_FALSE(TransactionRequiresNetwork(LOAD_NORMAL)); |
+ EXPECT_TRUE(TransactionRequiresNetwork(LOAD_NORMAL)); |
+} |
+ |
+INSTANTIATE_TEST_CASE_P(HttpCachePrefetchValidationTests, |
+ HttpCachePrefetchValidationTestP, |
+ ::testing::Values(false, true)); |
+ |
// 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 |
@@ -7348,3 +7471,5 @@ TEST(HttpCache, NoStoreResponseShouldNotBlockFollowingRequests) { |
"Cache-Control", "no-store")); |
ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); |
} |
+ |
+} // namespace net |