OLD | NEW |
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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 num_network_delegate_actions)); | 148 num_network_delegate_actions)); |
149 } | 149 } |
150 scoped_ptr<net::HttpTransaction> trans; | 150 scoped_ptr<net::HttpTransaction> trans; |
151 int rv = cache->CreateTransaction(&trans, delegate.get()); | 151 int rv = cache->CreateTransaction(&trans, delegate.get()); |
152 EXPECT_EQ(net::OK, rv); | 152 EXPECT_EQ(net::OK, rv); |
153 ASSERT_TRUE(trans.get()); | 153 ASSERT_TRUE(trans.get()); |
154 | 154 |
155 rv = trans->Start(&request, callback.callback(), net_log); | 155 rv = trans->Start(&request, callback.callback(), net_log); |
156 if (rv == net::ERR_IO_PENDING) | 156 if (rv == net::ERR_IO_PENDING) |
157 rv = callback.WaitForResult(); | 157 rv = callback.WaitForResult(); |
158 ASSERT_EQ(net::OK, rv); | 158 ASSERT_EQ(trans_info.return_code, rv); |
| 159 |
| 160 if (net::OK != rv) |
| 161 return; |
159 | 162 |
160 const net::HttpResponseInfo* response = trans->GetResponseInfo(); | 163 const net::HttpResponseInfo* response = trans->GetResponseInfo(); |
161 ASSERT_TRUE(response); | 164 ASSERT_TRUE(response); |
162 | 165 |
163 if (response_info) | 166 if (response_info) |
164 *response_info = *response; | 167 *response_info = *response; |
165 | 168 |
166 ReadAndVerifyTransaction(trans.get(), trans_info); | 169 ReadAndVerifyTransaction(trans.get(), trans_info); |
167 } | 170 } |
168 | 171 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 "GET", | 245 "GET", |
243 base::Time(), | 246 base::Time(), |
244 "", | 247 "", |
245 net::LOAD_VALIDATE_CACHE, | 248 net::LOAD_VALIDATE_CACHE, |
246 "HTTP/1.1 200 OK", | 249 "HTTP/1.1 200 OK", |
247 "Cache-Control: max-age=10000\n", | 250 "Cache-Control: max-age=10000\n", |
248 base::Time(), | 251 base::Time(), |
249 "<html><body>Google Blah Blah</body></html>", | 252 "<html><body>Google Blah Blah</body></html>", |
250 TEST_MODE_SYNC_NET_START, | 253 TEST_MODE_SYNC_NET_START, |
251 &FastTransactionServer::FastNoStoreHandler, | 254 &FastTransactionServer::FastNoStoreHandler, |
252 0 | 255 0, |
| 256 net::OK |
253 }; | 257 }; |
254 | 258 |
255 // This class provides a handler for kRangeGET_TransactionOK so that the range | 259 // This class provides a handler for kRangeGET_TransactionOK so that the range |
256 // request can be served on demand. | 260 // request can be served on demand. |
257 class RangeTransactionServer { | 261 class RangeTransactionServer { |
258 public: | 262 public: |
259 RangeTransactionServer() { | 263 RangeTransactionServer() { |
260 not_modified_ = false; | 264 not_modified_ = false; |
261 modified_ = false; | 265 modified_ = false; |
262 bad_200_ = false; | 266 bad_200_ = false; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 net::LOAD_NORMAL, | 387 net::LOAD_NORMAL, |
384 "HTTP/1.1 206 Partial Content", | 388 "HTTP/1.1 206 Partial Content", |
385 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 389 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
386 "ETag: \"foo\"\n" | 390 "ETag: \"foo\"\n" |
387 "Accept-Ranges: bytes\n" | 391 "Accept-Ranges: bytes\n" |
388 "Content-Length: 10\n", | 392 "Content-Length: 10\n", |
389 base::Time(), | 393 base::Time(), |
390 "rg: 40-49 ", | 394 "rg: 40-49 ", |
391 TEST_MODE_NORMAL, | 395 TEST_MODE_NORMAL, |
392 &RangeTransactionServer::RangeHandler, | 396 &RangeTransactionServer::RangeHandler, |
393 0 | 397 0, |
| 398 net::OK |
394 }; | 399 }; |
395 | 400 |
396 // Verifies the response headers (|response|) match a partial content | 401 // Verifies the response headers (|response|) match a partial content |
397 // response for the range starting at |start| and ending at |end|. | 402 // response for the range starting at |start| and ending at |end|. |
398 void Verify206Response(std::string response, int start, int end) { | 403 void Verify206Response(std::string response, int start, int end) { |
399 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | 404 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), |
400 response.size())); | 405 response.size())); |
401 scoped_refptr<net::HttpResponseHeaders> headers( | 406 scoped_refptr<net::HttpResponseHeaders> headers( |
402 new net::HttpResponseHeaders(raw_headers)); | 407 new net::HttpResponseHeaders(raw_headers)); |
403 | 408 |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 transaction.load_flags |= net::LOAD_PREFERRING_CACHE; | 839 transaction.load_flags |= net::LOAD_PREFERRING_CACHE; |
835 transaction.request_headers = "Foo: none\n"; | 840 transaction.request_headers = "Foo: none\n"; |
836 RunTransactionTest(cache.http_cache(), transaction); | 841 RunTransactionTest(cache.http_cache(), transaction); |
837 | 842 |
838 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 843 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
839 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 844 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
840 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 845 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
841 RemoveMockTransaction(&transaction); | 846 RemoveMockTransaction(&transaction); |
842 } | 847 } |
843 | 848 |
| 849 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on |
| 850 // network success |
| 851 TEST(HttpCache, SimpleGET_CacheOverride_Network) { |
| 852 MockHttpCache cache; |
| 853 |
| 854 // Prime cache. |
| 855 MockTransaction transaction(kSimpleGET_Transaction); |
| 856 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; |
| 857 transaction.response_headers = "Cache-Control: no-cache\n"; |
| 858 |
| 859 AddMockTransaction(&transaction); |
| 860 RunTransactionTest(cache.http_cache(), transaction); |
| 861 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 862 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 863 RemoveMockTransaction(&transaction); |
| 864 |
| 865 // Re-run transaction; make sure the result came from the network, |
| 866 // not the cache. |
| 867 transaction.data = "Changed data."; |
| 868 AddMockTransaction(&transaction); |
| 869 net::HttpResponseInfo response_info; |
| 870 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 871 &response_info); |
| 872 |
| 873 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 874 EXPECT_FALSE(response_info.server_data_unavailable); |
| 875 |
| 876 RemoveMockTransaction(&transaction); |
| 877 } |
| 878 |
| 879 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on |
| 880 // offline failure |
| 881 TEST(HttpCache, SimpleGET_CacheOverride_Offline) { |
| 882 MockHttpCache cache; |
| 883 |
| 884 // Prime cache. |
| 885 MockTransaction transaction(kSimpleGET_Transaction); |
| 886 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; |
| 887 transaction.response_headers = "Cache-Control: no-cache\n"; |
| 888 |
| 889 AddMockTransaction(&transaction); |
| 890 RunTransactionTest(cache.http_cache(), transaction); |
| 891 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 892 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 893 RemoveMockTransaction(&transaction); |
| 894 |
| 895 // Network failure with offline error; should return cache entry above + |
| 896 // flag signalling stale data. |
| 897 transaction.return_code = net::ERR_NAME_NOT_RESOLVED; |
| 898 AddMockTransaction(&transaction); |
| 899 |
| 900 MockHttpRequest request(transaction); |
| 901 net::TestCompletionCallback callback; |
| 902 scoped_ptr<net::HttpTransaction> trans; |
| 903 int rv = cache.http_cache()->CreateTransaction(&trans, NULL); |
| 904 EXPECT_EQ(net::OK, rv); |
| 905 ASSERT_TRUE(trans.get()); |
| 906 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
| 907 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
| 908 |
| 909 const net::HttpResponseInfo* response_info = trans->GetResponseInfo(); |
| 910 ASSERT_TRUE(response_info); |
| 911 EXPECT_TRUE(response_info->server_data_unavailable); |
| 912 EXPECT_TRUE(response_info->was_cached); |
| 913 ReadAndVerifyTransaction(trans.get(), transaction); |
| 914 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 915 |
| 916 RemoveMockTransaction(&transaction); |
| 917 } |
| 918 |
| 919 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on |
| 920 // non-offline failure failure |
| 921 TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) { |
| 922 MockHttpCache cache; |
| 923 |
| 924 // Prime cache. |
| 925 MockTransaction transaction(kSimpleGET_Transaction); |
| 926 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; |
| 927 transaction.response_headers = "Cache-Control: no-cache\n"; |
| 928 |
| 929 AddMockTransaction(&transaction); |
| 930 RunTransactionTest(cache.http_cache(), transaction); |
| 931 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 932 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 933 RemoveMockTransaction(&transaction); |
| 934 |
| 935 // Network failure with non-offline error; should fail with that error. |
| 936 transaction.return_code = net::ERR_PROXY_CONNECTION_FAILED; |
| 937 AddMockTransaction(&transaction); |
| 938 |
| 939 net::HttpResponseInfo response_info2; |
| 940 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 941 &response_info2); |
| 942 |
| 943 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 944 EXPECT_FALSE(response_info2.server_data_unavailable); |
| 945 |
| 946 RemoveMockTransaction(&transaction); |
| 947 } |
| 948 |
844 TEST(HttpCache, SimpleGET_LoadBypassCache) { | 949 TEST(HttpCache, SimpleGET_LoadBypassCache) { |
845 MockHttpCache cache; | 950 MockHttpCache cache; |
846 | 951 |
847 // Write to the cache. | 952 // Write to the cache. |
848 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 953 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
849 | 954 |
850 // Force this transaction to write to the cache again. | 955 // Force this transaction to write to the cache again. |
851 MockTransaction transaction(kSimpleGET_Transaction); | 956 MockTransaction transaction(kSimpleGET_Transaction); |
852 transaction.load_flags |= net::LOAD_BYPASS_CACHE; | 957 transaction.load_flags |= net::LOAD_BYPASS_CACHE; |
853 | 958 |
(...skipping 4659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5513 | 5618 |
5514 // Force this transaction to read from the cache. | 5619 // Force this transaction to read from the cache. |
5515 MockTransaction transaction(kSimpleGET_Transaction); | 5620 MockTransaction transaction(kSimpleGET_Transaction); |
5516 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 5621 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
5517 | 5622 |
5518 RunTransactionTestWithDelegate(cache.http_cache(), | 5623 RunTransactionTestWithDelegate(cache.http_cache(), |
5519 kSimpleGET_Transaction, | 5624 kSimpleGET_Transaction, |
5520 5, | 5625 5, |
5521 0); | 5626 0); |
5522 } | 5627 } |
OLD | NEW |