| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/hash_tables.h" | 7 #include "base/hash_tables.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 //----------------------------------------------------------------------------- | 26 //----------------------------------------------------------------------------- |
| 27 // mock disk cache (a very basic memory cache implementation) | 27 // mock disk cache (a very basic memory cache implementation) |
| 28 | 28 |
| 29 class MockDiskEntry : public disk_cache::Entry, | 29 class MockDiskEntry : public disk_cache::Entry, |
| 30 public base::RefCounted<MockDiskEntry> { | 30 public base::RefCounted<MockDiskEntry> { |
| 31 public: | 31 public: |
| 32 MockDiskEntry() | 32 MockDiskEntry() |
| 33 : test_mode_(0), doomed_(false), sparse_(false) { | 33 : test_mode_(0), doomed_(false), sparse_(false) { |
| 34 } | 34 } |
| 35 | 35 |
| 36 MockDiskEntry(const std::string& key) | 36 explicit MockDiskEntry(const std::string& key) |
| 37 : key_(key), doomed_(false), sparse_(false) { | 37 : key_(key), doomed_(false), sparse_(false) { |
| 38 // | 38 // |
| 39 // 'key' is prefixed with an identifier if it corresponds to a cached POST. | 39 // 'key' is prefixed with an identifier if it corresponds to a cached POST. |
| 40 // Skip past that to locate the actual URL. | 40 // Skip past that to locate the actual URL. |
| 41 // | 41 // |
| 42 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an | 42 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an |
| 43 // URL corresponding to a registered MockTransaction. It would be good to | 43 // URL corresponding to a registered MockTransaction. It would be good to |
| 44 // have another way to access the test_mode. | 44 // have another way to access the test_mode. |
| 45 // | 45 // |
| 46 GURL url; | 46 GURL url; |
| (...skipping 1121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1168 | 1168 |
| 1169 std::string response_headers; | 1169 std::string response_headers; |
| 1170 RunTransactionTestWithResponse( | 1170 RunTransactionTestWithResponse( |
| 1171 cache.http_cache(), request, &response_headers); | 1171 cache.http_cache(), request, &response_headers); |
| 1172 | 1172 |
| 1173 EXPECT_EQ(kNetResponse1.status_and_headers(), response_headers); | 1173 EXPECT_EQ(kNetResponse1.status_and_headers(), response_headers); |
| 1174 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1174 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1175 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1175 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1176 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1176 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1177 | 1177 |
| 1178 // Request |kUrl| a second first time. Now |kNetResponse1| it is in the HTTP | 1178 // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP |
| 1179 // cache, so we don't hit the network. | 1179 // cache, so we don't hit the network. |
| 1180 | 1180 |
| 1181 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. | 1181 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. |
| 1182 kNetResponse1.AssignTo(&request); // Expected result. | 1182 kNetResponse1.AssignTo(&request); // Expected result. |
| 1183 | 1183 |
| 1184 RunTransactionTestWithResponse( | 1184 RunTransactionTestWithResponse( |
| 1185 cache.http_cache(), request, &response_headers); | 1185 cache.http_cache(), request, &response_headers); |
| 1186 | 1186 |
| 1187 EXPECT_EQ(kNetResponse1.status_and_headers(), response_headers); | 1187 EXPECT_EQ(kNetResponse1.status_and_headers(), response_headers); |
| 1188 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1188 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1222 cache.http_cache(), request, &response_headers); | 1222 cache.http_cache(), request, &response_headers); |
| 1223 | 1223 |
| 1224 EXPECT_EQ(kNetResponse2.status_and_headers(), response_headers); | 1224 EXPECT_EQ(kNetResponse2.status_and_headers(), response_headers); |
| 1225 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1225 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1226 EXPECT_EQ(2, cache.disk_cache()->open_count()); | 1226 EXPECT_EQ(2, cache.disk_cache()->open_count()); |
| 1227 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1227 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1228 | 1228 |
| 1229 RemoveMockTransaction(&mock_network_response); | 1229 RemoveMockTransaction(&mock_network_response); |
| 1230 } | 1230 } |
| 1231 | 1231 |
| 1232 TEST(HttpCache, UrlContainingHash) { |
| 1233 MockHttpCache cache; |
| 1234 |
| 1235 // Do a typical GET request -- should write an entry into our cache. |
| 1236 MockTransaction trans(kTypicalGET_Transaction); |
| 1237 RunTransactionTest(cache.http_cache(), trans); |
| 1238 |
| 1239 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1240 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1241 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1242 |
| 1243 // Request the same URL, but this time with a reference section (hash). |
| 1244 // Since the cache key strips the hash sections, this should be a cache hit. |
| 1245 std::string url_with_hash = std::string(trans.url) + "#multiple#hashes"; |
| 1246 trans.url = url_with_hash.c_str(); |
| 1247 trans.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 1248 |
| 1249 RunTransactionTest(cache.http_cache(), trans); |
| 1250 |
| 1251 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1252 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1253 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1254 } |
| 1255 |
| 1232 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { | 1256 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { |
| 1233 MockHttpCache cache; | 1257 MockHttpCache cache; |
| 1234 | 1258 |
| 1235 // Test that we skip the cache for POST requests. Eventually, we will want | 1259 // Test that we skip the cache for POST requests. Eventually, we will want |
| 1236 // to cache these, but we'll still have cases where skipping the cache makes | 1260 // to cache these, but we'll still have cases where skipping the cache makes |
| 1237 // sense, so we want to make sure that it works properly. | 1261 // sense, so we want to make sure that it works properly. |
| 1238 | 1262 |
| 1239 MockTransaction transaction(kSimplePOST_Transaction); | 1263 MockTransaction transaction(kSimplePOST_Transaction); |
| 1240 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 1264 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 1241 | 1265 |
| (...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1723 | 1747 |
| 1724 // force this transaction to write to the cache again | 1748 // force this transaction to write to the cache again |
| 1725 MockTransaction transaction(kSimpleGET_Transaction); | 1749 MockTransaction transaction(kSimpleGET_Transaction); |
| 1726 | 1750 |
| 1727 RunTransactionTest(cache.http_cache(), transaction); | 1751 RunTransactionTest(cache.http_cache(), transaction); |
| 1728 | 1752 |
| 1729 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1753 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1730 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1754 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1731 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1755 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1732 } | 1756 } |
| OLD | NEW |