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

Side by Side 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 comments. Created 7 years, 9 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | net/http/http_response_info.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/http/http_cache.h" 5 #include "net/http/http_cache.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 } 149 }
150 scoped_ptr<net::HttpTransaction> trans; 150 scoped_ptr<net::HttpTransaction> trans;
151 int rv = cache->CreateTransaction( 151 int rv = cache->CreateTransaction(
152 net::DEFAULT_PRIORITY, &trans, delegate.get()); 152 net::DEFAULT_PRIORITY, &trans, delegate.get());
153 EXPECT_EQ(net::OK, rv); 153 EXPECT_EQ(net::OK, rv);
154 ASSERT_TRUE(trans.get()); 154 ASSERT_TRUE(trans.get());
155 155
156 rv = trans->Start(&request, callback.callback(), net_log); 156 rv = trans->Start(&request, callback.callback(), net_log);
157 if (rv == net::ERR_IO_PENDING) 157 if (rv == net::ERR_IO_PENDING)
158 rv = callback.WaitForResult(); 158 rv = callback.WaitForResult();
159 ASSERT_EQ(net::OK, rv); 159 ASSERT_EQ(trans_info.return_code, rv);
160
161 if (net::OK != rv)
162 return;
160 163
161 const net::HttpResponseInfo* response = trans->GetResponseInfo(); 164 const net::HttpResponseInfo* response = trans->GetResponseInfo();
162 ASSERT_TRUE(response); 165 ASSERT_TRUE(response);
163 166
164 if (response_info) 167 if (response_info)
165 *response_info = *response; 168 *response_info = *response;
166 169
167 ReadAndVerifyTransaction(trans.get(), trans_info); 170 ReadAndVerifyTransaction(trans.get(), trans_info);
168 } 171 }
169 172
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 "GET", 246 "GET",
244 base::Time(), 247 base::Time(),
245 "", 248 "",
246 net::LOAD_VALIDATE_CACHE, 249 net::LOAD_VALIDATE_CACHE,
247 "HTTP/1.1 200 OK", 250 "HTTP/1.1 200 OK",
248 "Cache-Control: max-age=10000\n", 251 "Cache-Control: max-age=10000\n",
249 base::Time(), 252 base::Time(),
250 "<html><body>Google Blah Blah</body></html>", 253 "<html><body>Google Blah Blah</body></html>",
251 TEST_MODE_SYNC_NET_START, 254 TEST_MODE_SYNC_NET_START,
252 &FastTransactionServer::FastNoStoreHandler, 255 &FastTransactionServer::FastNoStoreHandler,
253 0 256 0,
257 net::OK
254 }; 258 };
255 259
256 // This class provides a handler for kRangeGET_TransactionOK so that the range 260 // This class provides a handler for kRangeGET_TransactionOK so that the range
257 // request can be served on demand. 261 // request can be served on demand.
258 class RangeTransactionServer { 262 class RangeTransactionServer {
259 public: 263 public:
260 RangeTransactionServer() { 264 RangeTransactionServer() {
261 not_modified_ = false; 265 not_modified_ = false;
262 modified_ = false; 266 modified_ = false;
263 bad_200_ = false; 267 bad_200_ = false;
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 net::LOAD_NORMAL, 388 net::LOAD_NORMAL,
385 "HTTP/1.1 206 Partial Content", 389 "HTTP/1.1 206 Partial Content",
386 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" 390 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n"
387 "ETag: \"foo\"\n" 391 "ETag: \"foo\"\n"
388 "Accept-Ranges: bytes\n" 392 "Accept-Ranges: bytes\n"
389 "Content-Length: 10\n", 393 "Content-Length: 10\n",
390 base::Time(), 394 base::Time(),
391 "rg: 40-49 ", 395 "rg: 40-49 ",
392 TEST_MODE_NORMAL, 396 TEST_MODE_NORMAL,
393 &RangeTransactionServer::RangeHandler, 397 &RangeTransactionServer::RangeHandler,
394 0 398 0,
399 net::OK
395 }; 400 };
396 401
397 // Verifies the response headers (|response|) match a partial content 402 // Verifies the response headers (|response|) match a partial content
398 // response for the range starting at |start| and ending at |end|. 403 // response for the range starting at |start| and ending at |end|.
399 void Verify206Response(std::string response, int start, int end) { 404 void Verify206Response(std::string response, int start, int end) {
400 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), 405 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(),
401 response.size())); 406 response.size()));
402 scoped_refptr<net::HttpResponseHeaders> headers( 407 scoped_refptr<net::HttpResponseHeaders> headers(
403 new net::HttpResponseHeaders(raw_headers)); 408 new net::HttpResponseHeaders(raw_headers));
404 409
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after
840 transaction.load_flags |= net::LOAD_PREFERRING_CACHE; 845 transaction.load_flags |= net::LOAD_PREFERRING_CACHE;
841 transaction.request_headers = "Foo: none\n"; 846 transaction.request_headers = "Foo: none\n";
842 RunTransactionTest(cache.http_cache(), transaction); 847 RunTransactionTest(cache.http_cache(), transaction);
843 848
844 EXPECT_EQ(2, cache.network_layer()->transaction_count()); 849 EXPECT_EQ(2, cache.network_layer()->transaction_count());
845 EXPECT_EQ(1, cache.disk_cache()->open_count()); 850 EXPECT_EQ(1, cache.disk_cache()->open_count());
846 EXPECT_EQ(1, cache.disk_cache()->create_count()); 851 EXPECT_EQ(1, cache.disk_cache()->create_count());
847 RemoveMockTransaction(&transaction); 852 RemoveMockTransaction(&transaction);
848 } 853 }
849 854
855 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
856 // network success
857 TEST(HttpCache, SimpleGET_CacheOverride_Network) {
858 MockHttpCache cache;
859
860 // Prime cache.
861 MockTransaction transaction(kSimpleGET_Transaction);
862 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE;
863 transaction.response_headers = "Cache-Control: no-cache\n";
864
865 AddMockTransaction(&transaction);
866 RunTransactionTest(cache.http_cache(), transaction);
867 EXPECT_EQ(1, cache.network_layer()->transaction_count());
868 EXPECT_EQ(1, cache.disk_cache()->create_count());
869 RemoveMockTransaction(&transaction);
870
871 // Re-run transaction; make sure the result came from the network,
872 // not the cache.
873 transaction.data = "Changed data.";
874 AddMockTransaction(&transaction);
875 net::HttpResponseInfo response_info;
876 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
877 &response_info);
878
879 EXPECT_EQ(2, cache.network_layer()->transaction_count());
880 EXPECT_FALSE(response_info.server_data_unavailable);
881
882 RemoveMockTransaction(&transaction);
883 }
884
885 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
886 // offline failure
887 TEST(HttpCache, SimpleGET_CacheOverride_Offline) {
888 MockHttpCache cache;
889
890 // Prime cache.
891 MockTransaction transaction(kSimpleGET_Transaction);
892 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE;
893 transaction.response_headers = "Cache-Control: no-cache\n";
894
895 AddMockTransaction(&transaction);
896 RunTransactionTest(cache.http_cache(), transaction);
897 EXPECT_EQ(1, cache.network_layer()->transaction_count());
898 EXPECT_EQ(1, cache.disk_cache()->create_count());
899 RemoveMockTransaction(&transaction);
900
901 // Network failure with offline error; should return cache entry above +
902 // flag signalling stale data.
903 transaction.return_code = net::ERR_NAME_NOT_RESOLVED;
904 AddMockTransaction(&transaction);
905
906 MockHttpRequest request(transaction);
907 net::TestCompletionCallback callback;
908 scoped_ptr<net::HttpTransaction> trans;
909 int rv = cache.http_cache()->CreateTransaction(
910 net::DEFAULT_PRIORITY, &trans, NULL);
911 EXPECT_EQ(net::OK, rv);
912 ASSERT_TRUE(trans.get());
913 rv = trans->Start(&request, callback.callback(), net::BoundNetLog());
914 EXPECT_EQ(net::OK, callback.GetResult(rv));
915
916 const net::HttpResponseInfo* response_info = trans->GetResponseInfo();
917 ASSERT_TRUE(response_info);
918 EXPECT_TRUE(response_info->server_data_unavailable);
919 EXPECT_TRUE(response_info->was_cached);
920 ReadAndVerifyTransaction(trans.get(), transaction);
921 EXPECT_EQ(2, cache.network_layer()->transaction_count());
922
923 RemoveMockTransaction(&transaction);
924 }
925
926 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on
927 // non-offline failure failure
928 TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) {
929 MockHttpCache cache;
930
931 // Prime cache.
932 MockTransaction transaction(kSimpleGET_Transaction);
933 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE;
934 transaction.response_headers = "Cache-Control: no-cache\n";
935
936 AddMockTransaction(&transaction);
937 RunTransactionTest(cache.http_cache(), transaction);
938 EXPECT_EQ(1, cache.network_layer()->transaction_count());
939 EXPECT_EQ(1, cache.disk_cache()->create_count());
940 RemoveMockTransaction(&transaction);
941
942 // Network failure with non-offline error; should fail with that error.
943 transaction.return_code = net::ERR_PROXY_CONNECTION_FAILED;
944 AddMockTransaction(&transaction);
945
946 net::HttpResponseInfo response_info2;
947 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction,
948 &response_info2);
949
950 EXPECT_EQ(2, cache.network_layer()->transaction_count());
951 EXPECT_FALSE(response_info2.server_data_unavailable);
952
953 RemoveMockTransaction(&transaction);
954 }
955
850 TEST(HttpCache, SimpleGET_LoadBypassCache) { 956 TEST(HttpCache, SimpleGET_LoadBypassCache) {
851 MockHttpCache cache; 957 MockHttpCache cache;
852 958
853 // Write to the cache. 959 // Write to the cache.
854 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); 960 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
855 961
856 // Force this transaction to write to the cache again. 962 // Force this transaction to write to the cache again.
857 MockTransaction transaction(kSimpleGET_Transaction); 963 MockTransaction transaction(kSimpleGET_Transaction);
858 transaction.load_flags |= net::LOAD_BYPASS_CACHE; 964 transaction.load_flags |= net::LOAD_BYPASS_CACHE;
859 965
(...skipping 4700 matching lines...) Expand 10 before | Expand all | Expand 10 after
5560 5666
5561 // Force this transaction to read from the cache. 5667 // Force this transaction to read from the cache.
5562 MockTransaction transaction(kSimpleGET_Transaction); 5668 MockTransaction transaction(kSimpleGET_Transaction);
5563 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; 5669 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE;
5564 5670
5565 RunTransactionTestWithDelegate(cache.http_cache(), 5671 RunTransactionTestWithDelegate(cache.http_cache(),
5566 kSimpleGET_Transaction, 5672 kSimpleGET_Transaction,
5567 5, 5673 5,
5568 0); 5674 0);
5569 } 5675 }
OLDNEW
« no previous file with comments | « net/http/http_cache_transaction.cc ('k') | net/http/http_response_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698