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 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
834 transaction.load_flags |= net::LOAD_PREFERRING_CACHE; | 837 transaction.load_flags |= net::LOAD_PREFERRING_CACHE; |
835 transaction.request_headers = "Foo: none\n"; | 838 transaction.request_headers = "Foo: none\n"; |
836 RunTransactionTest(cache.http_cache(), transaction); | 839 RunTransactionTest(cache.http_cache(), transaction); |
837 | 840 |
838 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 841 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
839 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 842 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
840 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 843 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
841 RemoveMockTransaction(&transaction); | 844 RemoveMockTransaction(&transaction); |
842 } | 845 } |
843 | 846 |
847 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | |
848 // network success | |
849 TEST(HttpCache, SimpleGET_CacheOverride_Network) { | |
850 MockHttpCache cache; | |
851 | |
852 MockTransaction transaction(kSimpleGET_Transaction); | |
853 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; | |
854 transaction.response_headers = "Cache-Control: no-cache\n"; | |
855 | |
856 AddMockTransaction(&transaction); | |
857 net::HttpResponseInfo response_info; | |
858 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | |
859 &response_info); | |
860 | |
861 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
862 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.
| |
863 | |
864 RemoveMockTransaction(&transaction); | |
865 } | |
866 | |
867 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | |
868 // offline failure | |
869 TEST(HttpCache, SimpleGET_CacheOverride_Offline) { | |
870 MockHttpCache cache; | |
871 | |
872 // 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.
| |
873 MockTransaction transaction(kSimpleGET_Transaction); | |
874 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; | |
875 transaction.response_headers = "Cache-Control: no-cache\n"; | |
876 | |
877 AddMockTransaction(&transaction); | |
878 RunTransactionTest(cache.http_cache(), transaction); | |
879 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
880 | |
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.
| |
881 // Network failure with offline error; should return cache entry above + | |
882 // flag signalling stale data. | |
883 transaction.return_code = net::ERR_NAME_NOT_RESOLVED; | |
884 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.
| |
885 | |
886 MockHttpRequest request(transaction); | |
887 net::TestCompletionCallback callback; | |
888 scoped_ptr<net::HttpTransaction> trans; | |
889 int rv = cache.http_cache()->CreateTransaction(&trans, NULL); | |
890 EXPECT_EQ(net::OK, rv); | |
891 ASSERT_TRUE(trans.get()); | |
892 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
893 rv = callback.GetResult(rv); | |
894 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.
| |
895 | |
896 const net::HttpResponseInfo* response_info1 = trans->GetResponseInfo(); | |
897 ASSERT_TRUE(response_info1); | |
898 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.
| |
899 ReadAndVerifyTransaction(trans.get(), transaction); | |
900 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
901 | |
902 RemoveMockTransaction(&transaction); | |
903 } | |
904 | |
905 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | |
906 // non-offline failure failure | |
907 TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) { | |
908 MockHttpCache cache; | |
909 | |
910 // Prime cache | |
911 MockTransaction transaction(kSimpleGET_Transaction); | |
912 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; | |
913 transaction.response_headers = "Cache-Control: no-cache\n"; | |
914 | |
915 AddMockTransaction(&transaction); | |
916 RunTransactionTest(cache.http_cache(), transaction); | |
917 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
918 | |
919 // Network failure with non-offline error; should fail with that error. | |
920 transaction.return_code = net::ERR_PROXY_CONNECTION_FAILED; | |
921 AddMockTransaction(&transaction); | |
922 | |
923 net::HttpResponseInfo response_info2; | |
924 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | |
925 &response_info2); | |
926 | |
927 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
928 EXPECT_FALSE(response_info2.was_cache_override); | |
929 | |
930 RemoveMockTransaction(&transaction); | |
931 } | |
932 | |
844 TEST(HttpCache, SimpleGET_LoadBypassCache) { | 933 TEST(HttpCache, SimpleGET_LoadBypassCache) { |
845 MockHttpCache cache; | 934 MockHttpCache cache; |
846 | 935 |
847 // Write to the cache. | 936 // Write to the cache. |
848 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 937 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
849 | 938 |
850 // Force this transaction to write to the cache again. | 939 // Force this transaction to write to the cache again. |
851 MockTransaction transaction(kSimpleGET_Transaction); | 940 MockTransaction transaction(kSimpleGET_Transaction); |
852 transaction.load_flags |= net::LOAD_BYPASS_CACHE; | 941 transaction.load_flags |= net::LOAD_BYPASS_CACHE; |
853 | 942 |
(...skipping 4659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5513 | 5602 |
5514 // Force this transaction to read from the cache. | 5603 // Force this transaction to read from the cache. |
5515 MockTransaction transaction(kSimpleGET_Transaction); | 5604 MockTransaction transaction(kSimpleGET_Transaction); |
5516 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 5605 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
5517 | 5606 |
5518 RunTransactionTestWithDelegate(cache.http_cache(), | 5607 RunTransactionTestWithDelegate(cache.http_cache(), |
5519 kSimpleGET_Transaction, | 5608 kSimpleGET_Transaction, |
5520 5, | 5609 5, |
5521 0); | 5610 0); |
5522 } | 5611 } |
OLD | NEW |