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

Unified Diff: net/http/http_cache_unittest.cc

Issue 2959183002: Start work on splitting off the usage piece of in-memory index. This revision is just from the prev…
Patch Set: Rebase Created 3 years, 3 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') | no next file » | 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 89cfbab892c63abaf1ab83204f7a193044640491..8b585fbb467cfad36b853a3d3520fa5b27b9d2bc 100644
--- a/net/http/http_cache_unittest.cc
+++ b/net/http/http_cache_unittest.cc
@@ -1063,37 +1063,42 @@ TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMismatch) {
// Tests that was_cached was set properly on a failure, even if the cached
// response wasn't returned.
TEST(HttpCache, SimpleGET_CacheSignal_Failure) {
- MockHttpCache cache;
-
- // Prime cache.
- MockTransaction transaction(kSimpleGET_Transaction);
- transaction.response_headers = "Cache-Control: no-cache\n";
-
- AddMockTransaction(&transaction);
- RunTransactionTest(cache.http_cache(), transaction);
- EXPECT_EQ(1, cache.network_layer()->transaction_count());
- EXPECT_EQ(1, cache.disk_cache()->create_count());
- RemoveMockTransaction(&transaction);
-
- // Network failure with error; should fail but have was_cached set.
- transaction.start_return_code = ERR_FAILED;
- AddMockTransaction(&transaction);
-
- MockHttpRequest request(transaction);
- TestCompletionCallback callback;
- std::unique_ptr<HttpTransaction> trans;
- int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
- EXPECT_THAT(rv, IsOk());
- ASSERT_TRUE(trans.get());
- rv = trans->Start(&request, callback.callback(), NetLogWithSource());
- EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED));
-
- const HttpResponseInfo* response_info = trans->GetResponseInfo();
- ASSERT_TRUE(response_info);
- EXPECT_TRUE(response_info->was_cached);
- EXPECT_EQ(2, cache.network_layer()->transaction_count());
-
- RemoveMockTransaction(&transaction);
+ for (bool use_memory_entry_data : {false, true}) {
+ MockHttpCache cache;
+ cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
+
+ // Prime cache.
+ MockTransaction transaction(kSimpleGET_Transaction);
+ transaction.response_headers = "Cache-Control: no-cache\n";
+
+ AddMockTransaction(&transaction);
+ RunTransactionTest(cache.http_cache(), transaction);
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+ RemoveMockTransaction(&transaction);
+
+ // Network failure with error; should fail but have was_cached set.
+ transaction.start_return_code = ERR_FAILED;
+ AddMockTransaction(&transaction);
+
+ MockHttpRequest request(transaction);
+ TestCompletionCallback callback;
+ std::unique_ptr<HttpTransaction> trans;
+ int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans);
+ EXPECT_THAT(rv, IsOk());
+ ASSERT_TRUE(trans.get());
+ rv = trans->Start(&request, callback.callback(), NetLogWithSource());
+ EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED));
+
+ const HttpResponseInfo* response_info = trans->GetResponseInfo();
+ ASSERT_TRUE(response_info);
+ // If use_memory_entry_data is true, we will not bother opening the entry,
+ // and just kick it out, so was_cached will end up false.
+ EXPECT_EQ(!use_memory_entry_data, response_info->was_cached);
+ EXPECT_EQ(2, cache.network_layer()->transaction_count());
+
+ RemoveMockTransaction(&transaction);
+ }
}
// Confirm if we have an empty cache, a read is marked as network verified.
@@ -1316,42 +1321,58 @@ static void PreserveRequestHeaders_Handler(const HttpRequestInfo* request,
// Tests that we don't remove extra headers for simple requests.
TEST(HttpCache, SimpleGET_PreserveRequestHeaders) {
- MockHttpCache cache;
-
- MockTransaction transaction(kSimpleGET_Transaction);
- transaction.handler = PreserveRequestHeaders_Handler;
- transaction.request_headers = EXTRA_HEADER;
- transaction.response_headers = "Cache-Control: max-age=0\n";
- AddMockTransaction(&transaction);
-
- // Write, then revalidate the entry.
- RunTransactionTest(cache.http_cache(), transaction);
- RunTransactionTest(cache.http_cache(), transaction);
-
- EXPECT_EQ(2, cache.network_layer()->transaction_count());
- EXPECT_EQ(1, cache.disk_cache()->open_count());
- EXPECT_EQ(1, cache.disk_cache()->create_count());
- RemoveMockTransaction(&transaction);
+ for (bool use_memory_entry_data : {false, true}) {
+ MockHttpCache cache;
+ cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
+
+ MockTransaction transaction(kSimpleGET_Transaction);
+ transaction.handler = PreserveRequestHeaders_Handler;
+ transaction.request_headers = EXTRA_HEADER;
+ transaction.response_headers = "Cache-Control: max-age=0\n";
+ AddMockTransaction(&transaction);
+
+ // Write, then revalidate the entry.
+ RunTransactionTest(cache.http_cache(), transaction);
+ RunTransactionTest(cache.http_cache(), transaction);
+
+ EXPECT_EQ(2, cache.network_layer()->transaction_count());
+
+ // If the backend supports memory entry data, we can figure out that the
+ // entry has caching-hostile headers w/o opening it.
+ if (use_memory_entry_data) {
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(2, cache.disk_cache()->create_count());
+ } else {
+ EXPECT_EQ(1, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+ }
+ RemoveMockTransaction(&transaction);
+ }
}
// Tests that we don't remove extra headers for conditionalized requests.
TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) {
- MockHttpCache cache;
-
- // Write to the cache.
- RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
-
- MockTransaction transaction(kETagGET_Transaction);
- transaction.handler = PreserveRequestHeaders_Handler;
- transaction.request_headers = "If-None-Match: \"foopy\"\r\n" EXTRA_HEADER;
- AddMockTransaction(&transaction);
-
- RunTransactionTest(cache.http_cache(), transaction);
-
- EXPECT_EQ(2, cache.network_layer()->transaction_count());
- EXPECT_EQ(1, cache.disk_cache()->open_count());
- EXPECT_EQ(1, cache.disk_cache()->create_count());
- RemoveMockTransaction(&transaction);
+ for (bool use_memory_entry_data : {false, true}) {
+ MockHttpCache cache;
+ // Unlike in SimpleGET_PreserveRequestHeaders, this entry can be
+ // conditionalized, so memory hints don't affect behavior.
+ cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
+
+ // Write to the cache.
+ RunTransactionTest(cache.http_cache(), kETagGET_Transaction);
+
+ MockTransaction transaction(kETagGET_Transaction);
+ transaction.handler = PreserveRequestHeaders_Handler;
+ transaction.request_headers = "If-None-Match: \"foopy\"\r\n" EXTRA_HEADER;
+ AddMockTransaction(&transaction);
+
+ RunTransactionTest(cache.http_cache(), transaction);
+
+ EXPECT_EQ(2, cache.network_layer()->transaction_count());
+ EXPECT_EQ(1, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+ RemoveMockTransaction(&transaction);
+ }
}
TEST(HttpCache, SimpleGET_ManyReaders) {
@@ -5382,39 +5403,52 @@ TEST(HttpCache, RangeGET_NoValidation_LogsRestart) {
// Tests that a failure to conditionalize a regular request (no range) with a
// sparse entry results in a full response.
TEST(HttpCache, GET_NoConditionalization) {
- MockHttpCache cache;
- cache.FailConditionalizations();
- std::string headers;
-
- // Write to the cache (40-49).
- ScopedMockTransaction transaction(kRangeGET_TransactionOK);
- transaction.response_headers = "Content-Length: 10\n"
- "ETag: \"foo\"\n";
- RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
-
- Verify206Response(headers, 40, 49);
- EXPECT_EQ(1, cache.network_layer()->transaction_count());
- EXPECT_EQ(0, cache.disk_cache()->open_count());
- EXPECT_EQ(1, cache.disk_cache()->create_count());
+ for (bool use_memory_entry_data : {false, true}) {
+ MockHttpCache cache;
+ cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
+ cache.FailConditionalizations();
+ std::string headers;
- // Now verify that the cached data is not used.
- // Don't ask for a range. The cache will attempt to use the cached data but
- // should discard it as it cannot be validated. A regular request should go
- // to the server and a new entry should be created.
- transaction.request_headers = EXTRA_HEADER;
- transaction.data = "Not a range";
- RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
+ // Write to the cache (40-49).
+ ScopedMockTransaction transaction(kRangeGET_TransactionOK);
+ transaction.response_headers =
+ "Content-Length: 10\n"
+ "ETag: \"foo\"\n";
+ RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
- EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
- EXPECT_EQ(2, cache.network_layer()->transaction_count());
- EXPECT_EQ(1, cache.disk_cache()->open_count());
- EXPECT_EQ(2, cache.disk_cache()->create_count());
+ Verify206Response(headers, 40, 49);
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+
+ // Now verify that the cached data is not used.
+ // Don't ask for a range. The cache will attempt to use the cached data but
+ // should discard it as it cannot be validated. A regular request should go
+ // to the server and a new entry should be created.
+ transaction.request_headers = EXTRA_HEADER;
+ transaction.data = "Not a range";
+ RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers);
- // The last response was saved.
- RunTransactionTest(cache.http_cache(), transaction);
- EXPECT_EQ(3, cache.network_layer()->transaction_count());
- EXPECT_EQ(2, cache.disk_cache()->open_count());
- EXPECT_EQ(2, cache.disk_cache()->create_count());
+ EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n"));
+ EXPECT_EQ(2, cache.network_layer()->transaction_count());
+ EXPECT_EQ(1, cache.disk_cache()->open_count());
+ EXPECT_EQ(2, cache.disk_cache()->create_count());
+
+ // The last response was saved.
+ // ### before commit: figure out the way to actually test that.
+ // The open results in a useless entry since
+ // &RangeTransactionServer::RangeHandler returns headers without an ETag
+ // or a non-range request (not the things we set above).
+ RunTransactionTest(cache.http_cache(), transaction);
+ EXPECT_EQ(3, cache.network_layer()->transaction_count());
+ if (use_memory_entry_data) {
+ EXPECT_EQ(1, cache.disk_cache()->open_count());
+ EXPECT_EQ(3, cache.disk_cache()->create_count());
+ } else {
+ EXPECT_EQ(2, cache.disk_cache()->open_count());
+ EXPECT_EQ(2, cache.disk_cache()->create_count());
+ }
+ }
}
// Verifies that conditionalization failures when asking for a range that would
@@ -7886,28 +7920,36 @@ TEST(HttpCache, CachedRedirect) {
// Verify that no-cache resources are stored in cache, but are not fetched from
// cache during normal loads.
TEST(HttpCache, CacheControlNoCacheNormalLoad) {
- MockHttpCache cache;
+ for (bool use_memory_entry_data : {false, true}) {
+ MockHttpCache cache;
+ cache.disk_cache()->set_support_in_memory_entry_data(use_memory_entry_data);
- ScopedMockTransaction transaction(kSimpleGET_Transaction);
- transaction.response_headers = "cache-control: no-cache\n";
+ ScopedMockTransaction transaction(kSimpleGET_Transaction);
+ transaction.response_headers = "cache-control: no-cache\n";
- // Initial load.
- RunTransactionTest(cache.http_cache(), transaction);
+ // Initial load.
+ RunTransactionTest(cache.http_cache(), transaction);
- EXPECT_EQ(1, cache.network_layer()->transaction_count());
- EXPECT_EQ(0, cache.disk_cache()->open_count());
- EXPECT_EQ(1, cache.disk_cache()->create_count());
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
- // Try loading again; it should result in a network fetch.
- RunTransactionTest(cache.http_cache(), transaction);
+ // Try loading again; it should result in a network fetch.
+ RunTransactionTest(cache.http_cache(), transaction);
- EXPECT_EQ(2, cache.network_layer()->transaction_count());
- EXPECT_EQ(1, cache.disk_cache()->open_count());
- EXPECT_EQ(1, cache.disk_cache()->create_count());
+ EXPECT_EQ(2, cache.network_layer()->transaction_count());
+ if (use_memory_entry_data) {
+ EXPECT_EQ(0, cache.disk_cache()->open_count());
+ EXPECT_EQ(2, cache.disk_cache()->create_count());
+ } else {
+ EXPECT_EQ(1, cache.disk_cache()->open_count());
+ EXPECT_EQ(1, cache.disk_cache()->create_count());
+ }
- disk_cache::Entry* entry;
- EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry));
- entry->Close();
+ disk_cache::Entry* entry;
+ EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry));
+ entry->Close();
+ }
}
// Verify that no-cache resources are stored in cache and fetched from cache
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698