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

Unified Diff: net/http/http_cache_unittest.cc

Issue 12310075: Cache failover to LOAD_PREFERRING_CACHE if network response suggests offline. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Incorporated most comments; responded with questions to a few. Created 7 years, 10 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
Index: net/http/http_cache_unittest.cc
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc
index 22255760841199705d355c0e4112f129a481b9af..a49d11ddc908affd6a7c941cc868bf48637849d4 100644
--- a/net/http/http_cache_unittest.cc
+++ b/net/http/http_cache_unittest.cc
@@ -155,7 +155,10 @@ void RunTransactionTestWithRequestAndLogAndDelegate(
rv = trans->Start(&request, callback.callback(), net_log);
if (rv == net::ERR_IO_PENDING)
rv = callback.WaitForResult();
- ASSERT_EQ(net::OK, rv);
+ ASSERT_EQ(trans_info.return_code, rv);
+
+ if (net::OK != rv)
+ return;
const net::HttpResponseInfo* response = trans->GetResponseInfo();
ASSERT_TRUE(response);
@@ -841,6 +844,92 @@ TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMismatch) {
RemoveMockTransaction(&transaction);
}
+// Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
+// network success
+TEST(HttpCache, SimpleGET_CacheOverride_Network) {
+ MockHttpCache cache;
+
+ MockTransaction transaction(kSimpleGET_Transaction);
+ transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE;
+ transaction.response_headers = "Cache-Control: no-cache\n";
+
+ AddMockTransaction(&transaction);
+ net::HttpResponseInfo response_info;
+ RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
+ &response_info);
+
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+ EXPECT_FALSE(response_info.was_cache_override);
rvargas (doing something else) 2013/03/06 03:11:48 It would be better to prime the cache so that at l
Randy Smith (Not in Mondays) 2013/03/06 22:55:55 Good point. Done.
+
+ RemoveMockTransaction(&transaction);
+}
+
+// Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
+// offline failure
+TEST(HttpCache, SimpleGET_CacheOverride_Offline) {
+ MockHttpCache cache;
+
+ // Prime cache
rvargas (doing something else) 2013/03/06 03:11:48 nit: period at the end
Randy Smith (Not in Mondays) 2013/03/06 22:55:55 Done.
+ MockTransaction transaction(kSimpleGET_Transaction);
+ transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE;
+ transaction.response_headers = "Cache-Control: no-cache\n";
+
+ AddMockTransaction(&transaction);
+ RunTransactionTest(cache.http_cache(), transaction);
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+
rvargas (doing something else) 2013/03/06 03:11:48 EXPECT_EQ(1, cache.disk_cache()->create_count());
Randy Smith (Not in Mondays) 2013/03/06 22:55:55 Done.
+ // Network failure with offline error; should return cache entry above +
+ // flag signalling stale data.
+ transaction.return_code = net::ERR_NAME_NOT_RESOLVED;
+ AddMockTransaction(&transaction);
rvargas (doing something else) 2013/03/06 03:11:48 We should be removing the previous mock before add
Randy Smith (Not in Mondays) 2013/03/06 22:55:55 Done.
+
+ MockHttpRequest request(transaction);
+ net::TestCompletionCallback callback;
+ scoped_ptr<net::HttpTransaction> trans;
+ int rv = cache.http_cache()->CreateTransaction(&trans, NULL);
+ EXPECT_EQ(net::OK, rv);
+ ASSERT_TRUE(trans.get());
+ rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
+ rv = callback.GetResult(rv);
+ EXPECT_EQ(net::OK, rv);
rvargas (doing something else) 2013/03/06 03:11:48 nit: merge this line with the previous one.
Randy Smith (Not in Mondays) 2013/03/06 22:55:55 Done.
+
+ const net::HttpResponseInfo* response_info1 = trans->GetResponseInfo();
+ ASSERT_TRUE(response_info1);
+ EXPECT_TRUE(response_info1->was_cache_override);
rvargas (doing something else) 2013/03/06 03:11:48 + EXPECT_TRUE(response_info1->was_cached);
Randy Smith (Not in Mondays) 2013/03/06 22:55:55 Done.
+ ReadAndVerifyTransaction(trans.get(), transaction);
+ EXPECT_EQ(2, cache.network_layer()->transaction_count());
+
+ RemoveMockTransaction(&transaction);
+}
+
+// Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
+// non-offline failure failure
+TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) {
+ MockHttpCache cache;
+
+ // Prime cache
+ MockTransaction transaction(kSimpleGET_Transaction);
+ transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE;
+ transaction.response_headers = "Cache-Control: no-cache\n";
+
+ AddMockTransaction(&transaction);
+ RunTransactionTest(cache.http_cache(), transaction);
+ EXPECT_EQ(1, cache.network_layer()->transaction_count());
+
+ // Network failure with non-offline error; should fail with that error.
+ transaction.return_code = net::ERR_PROXY_CONNECTION_FAILED;
+ AddMockTransaction(&transaction);
+
+ net::HttpResponseInfo response_info2;
+ RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
+ &response_info2);
+
+ EXPECT_EQ(2, cache.network_layer()->transaction_count());
+ EXPECT_FALSE(response_info2.was_cache_override);
+
+ RemoveMockTransaction(&transaction);
+}
+
TEST(HttpCache, SimpleGET_LoadBypassCache) {
MockHttpCache cache;

Powered by Google App Engine
This is Rietveld 408576698