| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 #include "testing/gmock/include/gmock/gmock.h" | 56 #include "testing/gmock/include/gmock/gmock.h" |
| 57 #include "testing/gtest/include/gtest/gtest.h" | 57 #include "testing/gtest/include/gtest/gtest.h" |
| 58 | 58 |
| 59 using net::test::IsError; | 59 using net::test::IsError; |
| 60 using net::test::IsOk; | 60 using net::test::IsOk; |
| 61 | 61 |
| 62 using base::Time; | 62 using base::Time; |
| 63 | 63 |
| 64 namespace net { | 64 namespace net { |
| 65 | 65 |
| 66 using CacheEntryStatus = HttpResponseInfo::CacheEntryStatus; |
| 67 |
| 66 namespace { | 68 namespace { |
| 67 | 69 |
| 68 // Tests the load timing values of a request that goes through a | 70 // Tests the load timing values of a request that goes through a |
| 69 // MockNetworkTransaction. | 71 // MockNetworkTransaction. |
| 70 void TestLoadTimingNetworkRequest(const LoadTimingInfo& load_timing_info) { | 72 void TestLoadTimingNetworkRequest(const LoadTimingInfo& load_timing_info) { |
| 71 EXPECT_FALSE(load_timing_info.socket_reused); | 73 EXPECT_FALSE(load_timing_info.socket_reused); |
| 72 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | 74 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 73 | 75 |
| 74 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); | 76 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 75 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); | 77 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| (...skipping 977 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1053 | 1055 |
| 1054 // write to the cache | 1056 // write to the cache |
| 1055 HttpResponseInfo response_info; | 1057 HttpResponseInfo response_info; |
| 1056 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | 1058 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 1057 &response_info); | 1059 &response_info); |
| 1058 | 1060 |
| 1059 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1061 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1060 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1062 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1061 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1063 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1062 EXPECT_TRUE(response_info.network_accessed); | 1064 EXPECT_TRUE(response_info.network_accessed); |
| 1065 EXPECT_EQ(CacheEntryStatus::ENTRY_NOT_IN_CACHE, |
| 1066 response_info.cache_entry_status); |
| 1063 } | 1067 } |
| 1064 | 1068 |
| 1065 // Confirm if we have a fresh entry in cache, it isn't marked as | 1069 // Confirm if we have a fresh entry in cache, it isn't marked as |
| 1066 // network verified. | 1070 // network verified. |
| 1067 TEST(HttpCache, SimpleGET_NetworkAccessed_Cache) { | 1071 TEST(HttpCache, SimpleGET_NetworkAccessed_Cache) { |
| 1068 MockHttpCache cache; | 1072 MockHttpCache cache; |
| 1069 | 1073 |
| 1070 // Prime cache. | 1074 // Prime cache. |
| 1071 MockTransaction transaction(kSimpleGET_Transaction); | 1075 MockTransaction transaction(kSimpleGET_Transaction); |
| 1072 | 1076 |
| 1073 RunTransactionTest(cache.http_cache(), transaction); | 1077 RunTransactionTest(cache.http_cache(), transaction); |
| 1074 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1078 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1075 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1079 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1076 | 1080 |
| 1077 // Re-run transaction; make sure we don't mark the network as accessed. | 1081 // Re-run transaction; make sure we don't mark the network as accessed. |
| 1078 HttpResponseInfo response_info; | 1082 HttpResponseInfo response_info; |
| 1079 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | 1083 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
| 1080 &response_info); | 1084 &response_info); |
| 1081 | 1085 |
| 1082 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1086 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1083 EXPECT_FALSE(response_info.server_data_unavailable); | 1087 EXPECT_FALSE(response_info.server_data_unavailable); |
| 1084 EXPECT_FALSE(response_info.network_accessed); | 1088 EXPECT_FALSE(response_info.network_accessed); |
| 1089 EXPECT_EQ(CacheEntryStatus::ENTRY_USED, response_info.cache_entry_status); |
| 1085 } | 1090 } |
| 1086 | 1091 |
| 1087 TEST(HttpCache, SimpleGET_LoadBypassCache) { | 1092 TEST(HttpCache, SimpleGET_LoadBypassCache) { |
| 1088 MockHttpCache cache; | 1093 MockHttpCache cache; |
| 1089 | 1094 |
| 1090 // Write to the cache. | 1095 // Write to the cache. |
| 1091 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1096 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1092 | 1097 |
| 1093 // Force this transaction to write to the cache again. | 1098 // Force this transaction to write to the cache again. |
| 1094 MockTransaction transaction(kSimpleGET_Transaction); | 1099 MockTransaction transaction(kSimpleGET_Transaction); |
| (...skipping 6908 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8003 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 8008 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 8004 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 8009 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 8005 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 8010 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 8006 EXPECT_TRUE(response_info.was_cached); | 8011 EXPECT_TRUE(response_info.was_cached); |
| 8007 | 8012 |
| 8008 // The new SSL state is reported. | 8013 // The new SSL state is reported. |
| 8009 EXPECT_EQ(status2, response_info.ssl_info.connection_status); | 8014 EXPECT_EQ(status2, response_info.ssl_info.connection_status); |
| 8010 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); | 8015 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); |
| 8011 } | 8016 } |
| 8012 | 8017 |
| 8018 TEST(HttpCache, CacheEntryStatusOther) { |
| 8019 MockHttpCache cache; |
| 8020 |
| 8021 HttpResponseInfo response_info; |
| 8022 RunTransactionTestWithResponseInfo(cache.http_cache(), kRangeGET_Transaction, |
| 8023 &response_info); |
| 8024 |
| 8025 EXPECT_FALSE(response_info.was_cached); |
| 8026 EXPECT_TRUE(response_info.network_accessed); |
| 8027 EXPECT_EQ(CacheEntryStatus::ENTRY_OTHER, response_info.cache_entry_status); |
| 8028 } |
| 8029 |
| 8030 TEST(HttpCache, CacheEntryStatusNotInCache) { |
| 8031 MockHttpCache cache; |
| 8032 |
| 8033 HttpResponseInfo response_info; |
| 8034 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 8035 &response_info); |
| 8036 |
| 8037 EXPECT_FALSE(response_info.was_cached); |
| 8038 EXPECT_TRUE(response_info.network_accessed); |
| 8039 EXPECT_EQ(CacheEntryStatus::ENTRY_NOT_IN_CACHE, |
| 8040 response_info.cache_entry_status); |
| 8041 } |
| 8042 |
| 8043 TEST(HttpCache, CacheEntryStatusUsed) { |
| 8044 MockHttpCache cache; |
| 8045 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 8046 |
| 8047 HttpResponseInfo response_info; |
| 8048 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, |
| 8049 &response_info); |
| 8050 |
| 8051 EXPECT_TRUE(response_info.was_cached); |
| 8052 EXPECT_FALSE(response_info.network_accessed); |
| 8053 EXPECT_EQ(CacheEntryStatus::ENTRY_USED, response_info.cache_entry_status); |
| 8054 } |
| 8055 |
| 8056 TEST(HttpCache, CacheEntryStatusValidated) { |
| 8057 MockHttpCache cache; |
| 8058 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 8059 |
| 8060 ScopedMockTransaction still_valid(kETagGET_Transaction); |
| 8061 still_valid.load_flags = LOAD_VALIDATE_CACHE; // Force a validation. |
| 8062 still_valid.handler = ETagGet_ConditionalRequest_Handler; |
| 8063 |
| 8064 HttpResponseInfo response_info; |
| 8065 RunTransactionTestWithResponseInfo(cache.http_cache(), still_valid, |
| 8066 &response_info); |
| 8067 |
| 8068 EXPECT_TRUE(response_info.was_cached); |
| 8069 EXPECT_TRUE(response_info.network_accessed); |
| 8070 EXPECT_EQ(CacheEntryStatus::ENTRY_VALIDATED, |
| 8071 response_info.cache_entry_status); |
| 8072 } |
| 8073 |
| 8074 TEST(HttpCache, CacheEntryStatusUpdated) { |
| 8075 MockHttpCache cache; |
| 8076 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); |
| 8077 |
| 8078 ScopedMockTransaction update(kETagGET_Transaction); |
| 8079 update.load_flags = LOAD_VALIDATE_CACHE; // Force a validation. |
| 8080 |
| 8081 HttpResponseInfo response_info; |
| 8082 RunTransactionTestWithResponseInfo(cache.http_cache(), update, |
| 8083 &response_info); |
| 8084 |
| 8085 EXPECT_FALSE(response_info.was_cached); |
| 8086 EXPECT_TRUE(response_info.network_accessed); |
| 8087 EXPECT_EQ(CacheEntryStatus::ENTRY_UPDATED, response_info.cache_entry_status); |
| 8088 } |
| 8089 |
| 8090 TEST(HttpCache, CacheEntryStatusCantConditionalize) { |
| 8091 MockHttpCache cache; |
| 8092 cache.FailConditionalizations(); |
| 8093 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); |
| 8094 |
| 8095 HttpResponseInfo response_info; |
| 8096 RunTransactionTestWithResponseInfo(cache.http_cache(), |
| 8097 kTypicalGET_Transaction, &response_info); |
| 8098 |
| 8099 EXPECT_FALSE(response_info.was_cached); |
| 8100 EXPECT_TRUE(response_info.network_accessed); |
| 8101 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, |
| 8102 response_info.cache_entry_status); |
| 8103 } |
| 8104 |
| 8013 } // namespace net | 8105 } // namespace net |
| OLD | NEW |