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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 934 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | 945 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
946 &load_timing_info); | 946 &load_timing_info); |
947 | 947 |
948 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 948 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
949 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 949 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
950 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 950 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
951 TestLoadTimingNetworkRequest(load_timing_info); | 951 TestLoadTimingNetworkRequest(load_timing_info); |
952 RemoveMockTransaction(&transaction); | 952 RemoveMockTransaction(&transaction); |
953 } | 953 } |
954 | 954 |
955 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | |
956 // network success | |
957 TEST(HttpCache, SimpleGET_CacheOverride_Network) { | |
958 MockHttpCache cache; | |
959 | |
960 // Prime cache. | |
961 MockTransaction transaction(kSimpleGET_Transaction); | |
962 transaction.load_flags |= LOAD_FROM_CACHE_IF_OFFLINE; | |
963 transaction.response_headers = "Cache-Control: no-cache\n"; | |
964 | |
965 AddMockTransaction(&transaction); | |
966 RunTransactionTest(cache.http_cache(), transaction); | |
967 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
968 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
969 RemoveMockTransaction(&transaction); | |
970 | |
971 // Re-run transaction; make sure the result came from the network, | |
972 // not the cache. | |
973 transaction.data = "Changed data."; | |
974 AddMockTransaction(&transaction); | |
975 HttpResponseInfo response_info; | |
976 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | |
977 &response_info); | |
978 | |
979 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
980 EXPECT_FALSE(response_info.server_data_unavailable); | |
981 EXPECT_TRUE(response_info.network_accessed); | |
982 | |
983 RemoveMockTransaction(&transaction); | |
984 } | |
985 | |
986 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | |
987 // offline failure | |
988 TEST(HttpCache, SimpleGET_CacheOverride_Offline) { | |
989 MockHttpCache cache; | |
990 | |
991 // Prime cache. | |
992 MockTransaction transaction(kSimpleGET_Transaction); | |
993 transaction.load_flags |= LOAD_FROM_CACHE_IF_OFFLINE; | |
994 transaction.response_headers = "Cache-Control: no-cache\n"; | |
995 | |
996 AddMockTransaction(&transaction); | |
997 RunTransactionTest(cache.http_cache(), transaction); | |
998 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
999 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1000 RemoveMockTransaction(&transaction); | |
1001 | |
1002 // Network failure with offline error; should return cache entry above + | |
1003 // flag signalling stale data. | |
1004 transaction.return_code = ERR_NAME_NOT_RESOLVED; | |
1005 AddMockTransaction(&transaction); | |
1006 | |
1007 MockHttpRequest request(transaction); | |
1008 TestCompletionCallback callback; | |
1009 scoped_ptr<HttpTransaction> trans; | |
1010 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); | |
1011 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1012 EXPECT_EQ(OK, callback.GetResult(rv)); | |
1013 | |
1014 const HttpResponseInfo* response_info = trans->GetResponseInfo(); | |
1015 ASSERT_TRUE(response_info); | |
1016 EXPECT_TRUE(response_info->server_data_unavailable); | |
1017 EXPECT_TRUE(response_info->was_cached); | |
1018 EXPECT_FALSE(response_info->network_accessed); | |
1019 ReadAndVerifyTransaction(trans.get(), transaction); | |
1020 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1021 | |
1022 RemoveMockTransaction(&transaction); | |
1023 } | |
1024 | |
1025 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | |
1026 // non-offline failure. | |
1027 TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) { | |
1028 MockHttpCache cache; | |
1029 | |
1030 // Prime cache. | |
1031 MockTransaction transaction(kSimpleGET_Transaction); | |
1032 transaction.load_flags |= LOAD_FROM_CACHE_IF_OFFLINE; | |
1033 transaction.response_headers = "Cache-Control: no-cache\n"; | |
1034 | |
1035 AddMockTransaction(&transaction); | |
1036 RunTransactionTest(cache.http_cache(), transaction); | |
1037 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1038 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1039 RemoveMockTransaction(&transaction); | |
1040 | |
1041 // Network failure with non-offline error; should fail with that error. | |
1042 transaction.return_code = ERR_PROXY_CONNECTION_FAILED; | |
1043 AddMockTransaction(&transaction); | |
1044 | |
1045 HttpResponseInfo response_info2; | |
1046 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | |
1047 &response_info2); | |
1048 | |
1049 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1050 EXPECT_FALSE(response_info2.server_data_unavailable); | |
1051 | |
1052 RemoveMockTransaction(&transaction); | |
1053 } | |
1054 | |
1055 // Tests that was_cached was set properly on a failure, even if the cached | 955 // Tests that was_cached was set properly on a failure, even if the cached |
1056 // response wasn't returned. | 956 // response wasn't returned. |
1057 TEST(HttpCache, SimpleGET_CacheSignal_Failure) { | 957 TEST(HttpCache, SimpleGET_CacheSignal_Failure) { |
1058 MockHttpCache cache; | 958 MockHttpCache cache; |
1059 | 959 |
1060 // Prime cache. | 960 // Prime cache. |
1061 MockTransaction transaction(kSimpleGET_Transaction); | 961 MockTransaction transaction(kSimpleGET_Transaction); |
1062 transaction.response_headers = "Cache-Control: no-cache\n"; | 962 transaction.response_headers = "Cache-Control: no-cache\n"; |
1063 | 963 |
1064 AddMockTransaction(&transaction); | 964 AddMockTransaction(&transaction); |
(...skipping 6836 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7901 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); | 7801 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); |
7902 base::MessageLoop::current()->RunUntilIdle(); | 7802 base::MessageLoop::current()->RunUntilIdle(); |
7903 EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState()); | 7803 EXPECT_EQ(LOAD_STATE_IDLE, second->trans->GetLoadState()); |
7904 ASSERT_TRUE(second->trans->GetResponseInfo()); | 7804 ASSERT_TRUE(second->trans->GetResponseInfo()); |
7905 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( | 7805 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( |
7906 "Cache-Control", "no-store")); | 7806 "Cache-Control", "no-store")); |
7907 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); | 7807 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); |
7908 } | 7808 } |
7909 | 7809 |
7910 } // namespace net | 7810 } // namespace net |
OLD | NEW |