| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/platform_file.h" | 9 #include "base/platform_file.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 class MockDiskEntry : public disk_cache::Entry, | 27 class MockDiskEntry : public disk_cache::Entry, |
| 28 public base::RefCounted<MockDiskEntry> { | 28 public base::RefCounted<MockDiskEntry> { |
| 29 public: | 29 public: |
| 30 MockDiskEntry() | 30 MockDiskEntry() |
| 31 : test_mode_(0), doomed_(false), platform_file_(global_platform_file_) { | 31 : test_mode_(0), doomed_(false), platform_file_(global_platform_file_) { |
| 32 } | 32 } |
| 33 | 33 |
| 34 MockDiskEntry(const std::string& key) | 34 MockDiskEntry(const std::string& key) |
| 35 : key_(key), doomed_(false), platform_file_(global_platform_file_) { | 35 : key_(key), doomed_(false), platform_file_(global_platform_file_) { |
| 36 const MockTransaction* t = FindMockTransaction(GURL(key)); | 36 // |
| 37 // 'key' is prefixed with an identifier if it corresponds to a cached POST. |
| 38 // Skip past that to locate the actual URL. |
| 39 // |
| 40 // TODO(darin): It breaks the abstraction a bit that we assume 'key' is an |
| 41 // URL corresponding to a registered MockTransaction. It would be good to |
| 42 // have another way to access the test_mode. |
| 43 // |
| 44 GURL url; |
| 45 if (isdigit(key[0])) { |
| 46 size_t slash = key.find('/'); |
| 47 DCHECK(slash != std::string::npos); |
| 48 url = GURL(key.substr(slash + 1)); |
| 49 } else { |
| 50 url = GURL(key); |
| 51 } |
| 52 const MockTransaction* t = FindMockTransaction(url); |
| 37 DCHECK(t); | 53 DCHECK(t); |
| 38 test_mode_ = t->test_mode; | 54 test_mode_ = t->test_mode; |
| 39 } | 55 } |
| 40 | 56 |
| 41 ~MockDiskEntry() { | 57 ~MockDiskEntry() { |
| 42 } | 58 } |
| 43 | 59 |
| 44 bool is_doomed() const { return doomed_; } | 60 bool is_doomed() const { return doomed_; } |
| 45 | 61 |
| 46 virtual void Doom() { | 62 virtual void Doom() { |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 void ReadAndVerifyTransaction(net::HttpTransaction* trans, | 283 void ReadAndVerifyTransaction(net::HttpTransaction* trans, |
| 268 const MockTransaction& trans_info) { | 284 const MockTransaction& trans_info) { |
| 269 std::string content; | 285 std::string content; |
| 270 int rv = ReadTransaction(trans, &content); | 286 int rv = ReadTransaction(trans, &content); |
| 271 | 287 |
| 272 EXPECT_EQ(net::OK, rv); | 288 EXPECT_EQ(net::OK, rv); |
| 273 EXPECT_EQ(strlen(trans_info.data), content.size()); | 289 EXPECT_EQ(strlen(trans_info.data), content.size()); |
| 274 EXPECT_EQ(0, memcmp(trans_info.data, content.data(), content.size())); | 290 EXPECT_EQ(0, memcmp(trans_info.data, content.data(), content.size())); |
| 275 } | 291 } |
| 276 | 292 |
| 277 void RunTransactionTest(net::HttpCache* cache, | 293 void RunTransactionTestWithRequest(net::HttpCache* cache, |
| 278 const MockTransaction& trans_info) { | 294 const MockTransaction& trans_info, |
| 279 MockHttpRequest request(trans_info); | 295 const MockHttpRequest& request) { |
| 280 TestCompletionCallback callback; | 296 TestCompletionCallback callback; |
| 281 | 297 |
| 282 // write to the cache | 298 // write to the cache |
| 283 | 299 |
| 284 scoped_ptr<net::HttpTransaction> trans(cache->CreateTransaction()); | 300 scoped_ptr<net::HttpTransaction> trans(cache->CreateTransaction()); |
| 285 ASSERT_TRUE(trans.get()); | 301 ASSERT_TRUE(trans.get()); |
| 286 | 302 |
| 287 int rv = trans->Start(&request, &callback); | 303 int rv = trans->Start(&request, &callback); |
| 288 if (rv == net::ERR_IO_PENDING) | 304 if (rv == net::ERR_IO_PENDING) |
| 289 rv = callback.WaitForResult(); | 305 rv = callback.WaitForResult(); |
| 290 ASSERT_EQ(net::OK, rv); | 306 ASSERT_EQ(net::OK, rv); |
| 291 | 307 |
| 292 const net::HttpResponseInfo* response = trans->GetResponseInfo(); | 308 const net::HttpResponseInfo* response = trans->GetResponseInfo(); |
| 293 ASSERT_TRUE(response); | 309 ASSERT_TRUE(response); |
| 294 | 310 |
| 295 ReadAndVerifyTransaction(trans.get(), trans_info); | 311 ReadAndVerifyTransaction(trans.get(), trans_info); |
| 296 } | 312 } |
| 297 | 313 |
| 314 void RunTransactionTest(net::HttpCache* cache, |
| 315 const MockTransaction& trans_info) { |
| 316 return RunTransactionTestWithRequest( |
| 317 cache, trans_info, MockHttpRequest(trans_info)); |
| 318 } |
| 319 |
| 298 // This class provides a handler for kFastNoStoreGET_Transaction so that the | 320 // This class provides a handler for kFastNoStoreGET_Transaction so that the |
| 299 // no-store header can be included on demand. | 321 // no-store header can be included on demand. |
| 300 class FastTransactionServer { | 322 class FastTransactionServer { |
| 301 public: | 323 public: |
| 302 FastTransactionServer() { | 324 FastTransactionServer() { |
| 303 no_store = false; | 325 no_store = false; |
| 304 } | 326 } |
| 305 ~FastTransactionServer() {} | 327 ~FastTransactionServer() {} |
| 306 | 328 |
| 307 void set_no_store(bool value) { no_store = value; } | 329 void set_no_store(bool value) { no_store = value; } |
| (...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 890 RunTransactionTest(cache.http_cache(), transaction2); | 912 RunTransactionTest(cache.http_cache(), transaction2); |
| 891 | 913 |
| 892 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 914 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 893 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 915 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 894 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 916 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 895 } | 917 } |
| 896 | 918 |
| 897 TEST(HttpCache, SimplePOST_SkipsCache) { | 919 TEST(HttpCache, SimplePOST_SkipsCache) { |
| 898 MockHttpCache cache; | 920 MockHttpCache cache; |
| 899 | 921 |
| 900 // Test that we skip the cache for POST requests. Eventually, we will want | 922 // Test that we skip the cache for POST requests that do not have an upload |
| 901 // to cache these, but we'll still have cases where skipping the cache makes | 923 // identifier. |
| 902 // sense, so we want to make sure that it works properly. | |
| 903 | 924 |
| 904 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); | 925 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); |
| 905 | 926 |
| 906 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 927 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 907 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 928 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 908 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 929 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 909 } | 930 } |
| 910 | 931 |
| 911 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { | 932 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { |
| 912 MockHttpCache cache; | 933 MockHttpCache cache; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 930 rv = callback.WaitForResult(); | 951 rv = callback.WaitForResult(); |
| 931 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | 952 ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 932 | 953 |
| 933 trans.reset(); | 954 trans.reset(); |
| 934 | 955 |
| 935 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | 956 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 936 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 957 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 937 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 958 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 938 } | 959 } |
| 939 | 960 |
| 961 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) { |
| 962 MockHttpCache cache; |
| 963 |
| 964 // Test that we hit the cache for POST requests. |
| 965 |
| 966 MockTransaction transaction(kSimplePOST_Transaction); |
| 967 |
| 968 const int64 kUploadId = 1; // Just a dummy value. |
| 969 |
| 970 MockHttpRequest request(transaction); |
| 971 request.upload_data = new net::UploadData(); |
| 972 request.upload_data->set_identifier(kUploadId); |
| 973 request.upload_data->AppendBytes("hello", 5); |
| 974 |
| 975 // Populate the cache. |
| 976 RunTransactionTestWithRequest(cache.http_cache(), transaction, request); |
| 977 |
| 978 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 979 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 980 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 981 |
| 982 // Load from cache. |
| 983 request.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 984 RunTransactionTestWithRequest(cache.http_cache(), transaction, request); |
| 985 |
| 986 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 987 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 988 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 989 } |
| 990 |
| 940 TEST(HttpCache, RangeGET_SkipsCache) { | 991 TEST(HttpCache, RangeGET_SkipsCache) { |
| 941 MockHttpCache cache; | 992 MockHttpCache cache; |
| 942 | 993 |
| 943 // Test that we skip the cache for POST requests. Eventually, we will want | 994 // Test that we skip the cache for POST requests. Eventually, we will want |
| 944 // to cache these, but we'll still have cases where skipping the cache makes | 995 // to cache these, but we'll still have cases where skipping the cache makes |
| 945 // sense, so we want to make sure that it works properly. | 996 // sense, so we want to make sure that it works properly. |
| 946 | 997 |
| 947 RunTransactionTest(cache.http_cache(), kRangeGET_Transaction); | 998 RunTransactionTest(cache.http_cache(), kRangeGET_Transaction); |
| 948 | 999 |
| 949 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1000 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1275 | 1326 |
| 1276 const net::HttpResponseInfo* response = trans->GetResponseInfo(); | 1327 const net::HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1277 ASSERT_TRUE(response); | 1328 ASSERT_TRUE(response); |
| 1278 | 1329 |
| 1279 // Make sure we get the same file handle as in the first request. | 1330 // Make sure we get the same file handle as in the first request. |
| 1280 ASSERT_EQ(kFakePlatformFile1, response->response_data_file); | 1331 ASSERT_EQ(kFakePlatformFile1, response->response_data_file); |
| 1281 | 1332 |
| 1282 ReadAndVerifyTransaction(trans.get(), trans_info); | 1333 ReadAndVerifyTransaction(trans.get(), trans_info); |
| 1283 } | 1334 } |
| 1284 } | 1335 } |
| OLD | NEW |