| 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 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 387 |
| 388 void RunTransactionTestWithRequestAndLog(net::HttpCache* cache, | 388 void RunTransactionTestWithRequestAndLog(net::HttpCache* cache, |
| 389 const MockTransaction& trans_info, | 389 const MockTransaction& trans_info, |
| 390 const MockHttpRequest& request, | 390 const MockHttpRequest& request, |
| 391 net::HttpResponseInfo* response_info, | 391 net::HttpResponseInfo* response_info, |
| 392 net::LoadLog* load_log) { | 392 net::LoadLog* load_log) { |
| 393 TestCompletionCallback callback; | 393 TestCompletionCallback callback; |
| 394 | 394 |
| 395 // write to the cache | 395 // write to the cache |
| 396 | 396 |
| 397 scoped_ptr<net::HttpTransaction> trans(cache->CreateTransaction()); | 397 scoped_ptr<net::HttpTransaction> trans; |
| 398 int rv = cache->CreateTransaction(&trans); |
| 399 EXPECT_EQ(net::OK, rv); |
| 398 ASSERT_TRUE(trans.get()); | 400 ASSERT_TRUE(trans.get()); |
| 399 | 401 |
| 400 int rv = trans->Start(&request, &callback, load_log); | 402 rv = trans->Start(&request, &callback, load_log); |
| 401 if (rv == net::ERR_IO_PENDING) | 403 if (rv == net::ERR_IO_PENDING) |
| 402 rv = callback.WaitForResult(); | 404 rv = callback.WaitForResult(); |
| 403 ASSERT_EQ(net::OK, rv); | 405 ASSERT_EQ(net::OK, rv); |
| 404 | 406 |
| 405 const net::HttpResponseInfo* response = trans->GetResponseInfo(); | 407 const net::HttpResponseInfo* response = trans->GetResponseInfo(); |
| 406 ASSERT_TRUE(response); | 408 ASSERT_TRUE(response); |
| 407 | 409 |
| 408 if (response_info) | 410 if (response_info) |
| 409 *response_info = *response; | 411 *response_info = *response; |
| 410 | 412 |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 } // namespace | 646 } // namespace |
| 645 | 647 |
| 646 | 648 |
| 647 //----------------------------------------------------------------------------- | 649 //----------------------------------------------------------------------------- |
| 648 // tests | 650 // tests |
| 649 | 651 |
| 650 | 652 |
| 651 TEST(HttpCache, CreateThenDestroy) { | 653 TEST(HttpCache, CreateThenDestroy) { |
| 652 MockHttpCache cache; | 654 MockHttpCache cache; |
| 653 | 655 |
| 654 scoped_ptr<net::HttpTransaction> trans( | 656 scoped_ptr<net::HttpTransaction> trans; |
| 655 cache.http_cache()->CreateTransaction()); | 657 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 658 EXPECT_EQ(net::OK, rv); |
| 656 ASSERT_TRUE(trans.get()); | 659 ASSERT_TRUE(trans.get()); |
| 657 } | 660 } |
| 658 | 661 |
| 659 TEST(HttpCache, SimpleGET) { | 662 TEST(HttpCache, SimpleGET) { |
| 660 MockHttpCache cache; | 663 MockHttpCache cache; |
| 661 | 664 |
| 662 // write to the cache | 665 // write to the cache |
| 663 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 666 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 664 | 667 |
| 665 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 668 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { | 771 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { |
| 769 MockHttpCache cache; | 772 MockHttpCache cache; |
| 770 | 773 |
| 771 // force this transaction to read from the cache | 774 // force this transaction to read from the cache |
| 772 MockTransaction transaction(kSimpleGET_Transaction); | 775 MockTransaction transaction(kSimpleGET_Transaction); |
| 773 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 776 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 774 | 777 |
| 775 MockHttpRequest request(transaction); | 778 MockHttpRequest request(transaction); |
| 776 TestCompletionCallback callback; | 779 TestCompletionCallback callback; |
| 777 | 780 |
| 778 scoped_ptr<net::HttpTransaction> trans( | 781 scoped_ptr<net::HttpTransaction> trans; |
| 779 cache.http_cache()->CreateTransaction()); | 782 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 783 EXPECT_EQ(net::OK, rv); |
| 780 ASSERT_TRUE(trans.get()); | 784 ASSERT_TRUE(trans.get()); |
| 781 | 785 |
| 782 int rv = trans->Start(&request, &callback, NULL); | 786 rv = trans->Start(&request, &callback, NULL); |
| 783 if (rv == net::ERR_IO_PENDING) | 787 if (rv == net::ERR_IO_PENDING) |
| 784 rv = callback.WaitForResult(); | 788 rv = callback.WaitForResult(); |
| 785 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | 789 ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 786 | 790 |
| 787 trans.reset(); | 791 trans.reset(); |
| 788 | 792 |
| 789 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | 793 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 790 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 794 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 791 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 795 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 792 } | 796 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 911 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 915 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 912 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 916 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 913 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 917 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 914 } | 918 } |
| 915 | 919 |
| 916 struct Context { | 920 struct Context { |
| 917 int result; | 921 int result; |
| 918 TestCompletionCallback callback; | 922 TestCompletionCallback callback; |
| 919 scoped_ptr<net::HttpTransaction> trans; | 923 scoped_ptr<net::HttpTransaction> trans; |
| 920 | 924 |
| 921 explicit Context(net::HttpTransaction* t) | 925 Context() : result(net::ERR_IO_PENDING) { |
| 922 : result(net::ERR_IO_PENDING), trans(t) { | |
| 923 } | 926 } |
| 924 }; | 927 }; |
| 925 | 928 |
| 926 TEST(HttpCache, SimpleGET_ManyReaders) { | 929 TEST(HttpCache, SimpleGET_ManyReaders) { |
| 927 MockHttpCache cache; | 930 MockHttpCache cache; |
| 928 | 931 |
| 929 MockHttpRequest request(kSimpleGET_Transaction); | 932 MockHttpRequest request(kSimpleGET_Transaction); |
| 930 | 933 |
| 931 std::vector<Context*> context_list; | 934 std::vector<Context*> context_list; |
| 932 const int kNumTransactions = 5; | 935 const int kNumTransactions = 5; |
| 933 | 936 |
| 934 for (int i = 0; i < kNumTransactions; ++i) { | 937 for (int i = 0; i < kNumTransactions; ++i) { |
| 935 context_list.push_back( | 938 context_list.push_back(new Context()); |
| 936 new Context(cache.http_cache()->CreateTransaction())); | 939 Context* c = context_list[i]; |
| 937 | 940 |
| 938 Context* c = context_list[i]; | 941 c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 939 int rv = c->trans->Start(&request, &c->callback, NULL); | 942 EXPECT_EQ(net::OK, c->result); |
| 940 if (rv != net::ERR_IO_PENDING) | 943 |
| 941 c->result = rv; | 944 c->result = c->trans->Start(&request, &c->callback, NULL); |
| 942 } | 945 } |
| 943 | 946 |
| 944 // the first request should be a writer at this point, and the subsequent | 947 // the first request should be a writer at this point, and the subsequent |
| 945 // requests should be pending. | 948 // requests should be pending. |
| 946 | 949 |
| 947 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 950 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 948 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 951 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 949 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 952 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 950 | 953 |
| 951 for (int i = 0; i < kNumTransactions; ++i) { | 954 for (int i = 0; i < kNumTransactions; ++i) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 975 MockHttpCache cache; | 978 MockHttpCache cache; |
| 976 | 979 |
| 977 MockHttpRequest request(kSimpleGET_Transaction); | 980 MockHttpRequest request(kSimpleGET_Transaction); |
| 978 MockHttpRequest reader_request(kSimpleGET_Transaction); | 981 MockHttpRequest reader_request(kSimpleGET_Transaction); |
| 979 reader_request.load_flags = net::LOAD_ONLY_FROM_CACHE; | 982 reader_request.load_flags = net::LOAD_ONLY_FROM_CACHE; |
| 980 | 983 |
| 981 std::vector<Context*> context_list; | 984 std::vector<Context*> context_list; |
| 982 const int kNumTransactions = 5; | 985 const int kNumTransactions = 5; |
| 983 | 986 |
| 984 for (int i = 0; i < kNumTransactions; ++i) { | 987 for (int i = 0; i < kNumTransactions; ++i) { |
| 985 context_list.push_back( | 988 context_list.push_back(new Context()); |
| 986 new Context(cache.http_cache()->CreateTransaction())); | 989 Context* c = context_list[i]; |
| 987 | 990 |
| 988 Context* c = context_list[i]; | 991 c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 992 EXPECT_EQ(net::OK, c->result); |
| 993 |
| 989 MockHttpRequest* this_request = &request; | 994 MockHttpRequest* this_request = &request; |
| 990 if (i == 1 || i == 2) | 995 if (i == 1 || i == 2) |
| 991 this_request = &reader_request; | 996 this_request = &reader_request; |
| 992 | 997 |
| 993 int rv = c->trans->Start(this_request, &c->callback, NULL); | 998 c->result = c->trans->Start(this_request, &c->callback, NULL); |
| 994 if (rv != net::ERR_IO_PENDING) | |
| 995 c->result = rv; | |
| 996 } | 999 } |
| 997 | 1000 |
| 998 // The first request should be a writer at this point, and the subsequent | 1001 // The first request should be a writer at this point, and the subsequent |
| 999 // requests should be pending. | 1002 // requests should be pending. |
| 1000 | 1003 |
| 1001 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1004 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1002 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1005 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1003 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1006 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1004 | 1007 |
| 1005 Context* c = context_list[0]; | 1008 Context* c = context_list[0]; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1051 | 1054 |
| 1052 // The headers will be served right from the call to Start() the request. | 1055 // The headers will be served right from the call to Start() the request. |
| 1053 MockHttpRequest request(kFastNoStoreGET_Transaction); | 1056 MockHttpRequest request(kFastNoStoreGET_Transaction); |
| 1054 FastTransactionServer request_handler; | 1057 FastTransactionServer request_handler; |
| 1055 AddMockTransaction(&kFastNoStoreGET_Transaction); | 1058 AddMockTransaction(&kFastNoStoreGET_Transaction); |
| 1056 | 1059 |
| 1057 std::vector<Context*> context_list; | 1060 std::vector<Context*> context_list; |
| 1058 const int kNumTransactions = 3; | 1061 const int kNumTransactions = 3; |
| 1059 | 1062 |
| 1060 for (int i = 0; i < kNumTransactions; ++i) { | 1063 for (int i = 0; i < kNumTransactions; ++i) { |
| 1061 context_list.push_back( | 1064 context_list.push_back(new Context()); |
| 1062 new Context(cache.http_cache()->CreateTransaction())); | 1065 Context* c = context_list[i]; |
| 1063 | 1066 |
| 1064 Context* c = context_list[i]; | 1067 c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1065 int rv = c->trans->Start(&request, &c->callback, NULL); | 1068 EXPECT_EQ(net::OK, c->result); |
| 1066 if (rv != net::ERR_IO_PENDING) | 1069 |
| 1067 c->result = rv; | 1070 c->result = c->trans->Start(&request, &c->callback, NULL); |
| 1068 } | 1071 } |
| 1069 | 1072 |
| 1070 // The first request should be a writer at this point, and the subsequent | 1073 // The first request should be a writer at this point, and the subsequent |
| 1071 // requests should be pending. | 1074 // requests should be pending. |
| 1072 | 1075 |
| 1073 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1076 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1074 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1077 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1075 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1078 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1076 | 1079 |
| 1077 // Now, make sure that the second request asks for the entry not to be stored. | 1080 // Now, make sure that the second request asks for the entry not to be stored. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1094 | 1097 |
| 1095 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { | 1098 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
| 1096 MockHttpCache cache; | 1099 MockHttpCache cache; |
| 1097 | 1100 |
| 1098 MockHttpRequest request(kSimpleGET_Transaction); | 1101 MockHttpRequest request(kSimpleGET_Transaction); |
| 1099 | 1102 |
| 1100 std::vector<Context*> context_list; | 1103 std::vector<Context*> context_list; |
| 1101 const int kNumTransactions = 2; | 1104 const int kNumTransactions = 2; |
| 1102 | 1105 |
| 1103 for (int i = 0; i < kNumTransactions; ++i) { | 1106 for (int i = 0; i < kNumTransactions; ++i) { |
| 1104 context_list.push_back( | 1107 context_list.push_back(new Context()); |
| 1105 new Context(cache.http_cache()->CreateTransaction())); | 1108 Context* c = context_list[i]; |
| 1106 | 1109 |
| 1107 Context* c = context_list[i]; | 1110 c->result = cache.http_cache()->CreateTransaction(&c->trans); |
| 1108 int rv = c->trans->Start(&request, &c->callback, NULL); | 1111 EXPECT_EQ(net::OK, c->result); |
| 1109 if (rv != net::ERR_IO_PENDING) | 1112 |
| 1110 c->result = rv; | 1113 c->result = c->trans->Start(&request, &c->callback, NULL); |
| 1111 } | 1114 } |
| 1112 | 1115 |
| 1113 // the first request should be a writer at this point, and the subsequent | 1116 // the first request should be a writer at this point, and the subsequent |
| 1114 // requests should be pending. | 1117 // requests should be pending. |
| 1115 | 1118 |
| 1116 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1119 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1117 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1120 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1118 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1121 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1119 | 1122 |
| 1120 for (int i = 0; i < kNumTransactions; ++i) { | 1123 for (int i = 0; i < kNumTransactions; ++i) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1148 | 1151 |
| 1149 TEST(HttpCache, SimpleGET_AbandonedCacheRead) { | 1152 TEST(HttpCache, SimpleGET_AbandonedCacheRead) { |
| 1150 MockHttpCache cache; | 1153 MockHttpCache cache; |
| 1151 | 1154 |
| 1152 // write to the cache | 1155 // write to the cache |
| 1153 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1156 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 1154 | 1157 |
| 1155 MockHttpRequest request(kSimpleGET_Transaction); | 1158 MockHttpRequest request(kSimpleGET_Transaction); |
| 1156 TestCompletionCallback callback; | 1159 TestCompletionCallback callback; |
| 1157 | 1160 |
| 1158 scoped_ptr<net::HttpTransaction> trans( | 1161 scoped_ptr<net::HttpTransaction> trans; |
| 1159 cache.http_cache()->CreateTransaction()); | 1162 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 1160 int rv = trans->Start(&request, &callback, NULL); | 1163 EXPECT_EQ(net::OK, rv); |
| 1164 rv = trans->Start(&request, &callback, NULL); |
| 1161 if (rv == net::ERR_IO_PENDING) | 1165 if (rv == net::ERR_IO_PENDING) |
| 1162 rv = callback.WaitForResult(); | 1166 rv = callback.WaitForResult(); |
| 1163 ASSERT_EQ(net::OK, rv); | 1167 ASSERT_EQ(net::OK, rv); |
| 1164 | 1168 |
| 1165 scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(256); | 1169 scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(256); |
| 1166 rv = trans->Read(buf, 256, &callback); | 1170 rv = trans->Read(buf, 256, &callback); |
| 1167 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 1171 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 1168 | 1172 |
| 1169 // Test that destroying the transaction while it is reading from the cache | 1173 // Test that destroying the transaction while it is reading from the cache |
| 1170 // works properly. | 1174 // works properly. |
| (...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1661 // Test that we skip the cache for POST requests. Eventually, we will want | 1665 // Test that we skip the cache for POST requests. Eventually, we will want |
| 1662 // to cache these, but we'll still have cases where skipping the cache makes | 1666 // to cache these, but we'll still have cases where skipping the cache makes |
| 1663 // sense, so we want to make sure that it works properly. | 1667 // sense, so we want to make sure that it works properly. |
| 1664 | 1668 |
| 1665 MockTransaction transaction(kSimplePOST_Transaction); | 1669 MockTransaction transaction(kSimplePOST_Transaction); |
| 1666 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 1670 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 1667 | 1671 |
| 1668 MockHttpRequest request(transaction); | 1672 MockHttpRequest request(transaction); |
| 1669 TestCompletionCallback callback; | 1673 TestCompletionCallback callback; |
| 1670 | 1674 |
| 1671 scoped_ptr<net::HttpTransaction> trans( | 1675 scoped_ptr<net::HttpTransaction> trans; |
| 1672 cache.http_cache()->CreateTransaction()); | 1676 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 1677 EXPECT_EQ(net::OK, rv); |
| 1673 ASSERT_TRUE(trans.get()); | 1678 ASSERT_TRUE(trans.get()); |
| 1674 | 1679 |
| 1675 int rv = trans->Start(&request, &callback, NULL); | 1680 rv = trans->Start(&request, &callback, NULL); |
| 1676 if (rv == net::ERR_IO_PENDING) | 1681 if (rv == net::ERR_IO_PENDING) |
| 1677 rv = callback.WaitForResult(); | 1682 rv = callback.WaitForResult(); |
| 1678 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | 1683 ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 1679 | 1684 |
| 1680 trans.reset(); | 1685 trans.reset(); |
| 1681 | 1686 |
| 1682 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | 1687 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 1683 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1688 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1684 EXPECT_EQ(0, cache.disk_cache()->create_count()); | 1689 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 1685 } | 1690 } |
| (...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2299 } | 2304 } |
| 2300 | 2305 |
| 2301 // Tests that we don't delete a sparse entry when we cancel a request. | 2306 // Tests that we don't delete a sparse entry when we cancel a request. |
| 2302 TEST(HttpCache, RangeGET_Cancel) { | 2307 TEST(HttpCache, RangeGET_Cancel) { |
| 2303 MockHttpCache cache; | 2308 MockHttpCache cache; |
| 2304 cache.http_cache()->set_enable_range_support(true); | 2309 cache.http_cache()->set_enable_range_support(true); |
| 2305 AddMockTransaction(&kRangeGET_TransactionOK); | 2310 AddMockTransaction(&kRangeGET_TransactionOK); |
| 2306 | 2311 |
| 2307 MockHttpRequest request(kRangeGET_TransactionOK); | 2312 MockHttpRequest request(kRangeGET_TransactionOK); |
| 2308 | 2313 |
| 2309 Context* c = new Context(cache.http_cache()->CreateTransaction()); | 2314 Context* c = new Context(); |
| 2315 int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 2316 EXPECT_EQ(net::OK, rv); |
| 2310 | 2317 |
| 2311 int rv = c->trans->Start(&request, &c->callback, NULL); | 2318 rv = c->trans->Start(&request, &c->callback, NULL); |
| 2312 if (rv == net::ERR_IO_PENDING) | 2319 if (rv == net::ERR_IO_PENDING) |
| 2313 rv = c->callback.WaitForResult(); | 2320 rv = c->callback.WaitForResult(); |
| 2314 | 2321 |
| 2315 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2322 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2316 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2323 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2317 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2324 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2318 | 2325 |
| 2319 // Make sure that the entry has some data stored. | 2326 // Make sure that the entry has some data stored. |
| 2320 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); | 2327 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); |
| 2321 rv = c->trans->Read(buf, buf->size(), &c->callback); | 2328 rv = c->trans->Read(buf, buf->size(), &c->callback); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 2347 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2354 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2348 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2355 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2349 | 2356 |
| 2350 // Force this transaction to read from the cache. | 2357 // Force this transaction to read from the cache. |
| 2351 MockTransaction transaction(kRangeGET_TransactionOK); | 2358 MockTransaction transaction(kRangeGET_TransactionOK); |
| 2352 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 2359 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 2353 | 2360 |
| 2354 MockHttpRequest request(transaction); | 2361 MockHttpRequest request(transaction); |
| 2355 TestCompletionCallback callback; | 2362 TestCompletionCallback callback; |
| 2356 | 2363 |
| 2357 scoped_ptr<net::HttpTransaction> trans( | 2364 scoped_ptr<net::HttpTransaction> trans; |
| 2358 cache.http_cache()->CreateTransaction()); | 2365 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 2366 EXPECT_EQ(net::OK, rv); |
| 2359 ASSERT_TRUE(trans.get()); | 2367 ASSERT_TRUE(trans.get()); |
| 2360 | 2368 |
| 2361 int rv = trans->Start(&request, &callback, NULL); | 2369 int rv = trans->Start(&request, &callback, NULL); |
| 2362 if (rv == net::ERR_IO_PENDING) | 2370 if (rv == net::ERR_IO_PENDING) |
| 2363 rv = callback.WaitForResult(); | 2371 rv = callback.WaitForResult(); |
| 2364 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | 2372 ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 2365 | 2373 |
| 2366 trans.reset(); | 2374 trans.reset(); |
| 2367 | 2375 |
| 2368 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2376 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 2399 } | 2407 } |
| 2400 | 2408 |
| 2401 // Tests that we delete an entry when the request is cancelled before starting | 2409 // Tests that we delete an entry when the request is cancelled before starting |
| 2402 // to read from the network. | 2410 // to read from the network. |
| 2403 TEST(HttpCache, DoomOnDestruction) { | 2411 TEST(HttpCache, DoomOnDestruction) { |
| 2404 MockHttpCache cache; | 2412 MockHttpCache cache; |
| 2405 cache.http_cache()->set_enable_range_support(true); | 2413 cache.http_cache()->set_enable_range_support(true); |
| 2406 | 2414 |
| 2407 MockHttpRequest request(kSimpleGET_Transaction); | 2415 MockHttpRequest request(kSimpleGET_Transaction); |
| 2408 | 2416 |
| 2409 Context* c = new Context(cache.http_cache()->CreateTransaction()); | 2417 Context* c = new Context(); |
| 2418 int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 2419 EXPECT_EQ(net::OK, rv); |
| 2410 | 2420 |
| 2411 int rv = c->trans->Start(&request, &c->callback, NULL); | 2421 rv = c->trans->Start(&request, &c->callback, NULL); |
| 2412 if (rv == net::ERR_IO_PENDING) | 2422 if (rv == net::ERR_IO_PENDING) |
| 2413 c->result = c->callback.WaitForResult(); | 2423 c->result = c->callback.WaitForResult(); |
| 2414 | 2424 |
| 2415 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2425 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2416 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2426 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2417 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2427 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2418 | 2428 |
| 2419 // Destroy the transaction. We only have the headers so we should delete this | 2429 // Destroy the transaction. We only have the headers so we should delete this |
| 2420 // entry. | 2430 // entry. |
| 2421 delete c; | 2431 delete c; |
| 2422 | 2432 |
| 2423 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 2433 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 2424 | 2434 |
| 2425 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2435 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2426 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2436 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2427 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2437 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 2428 } | 2438 } |
| 2429 | 2439 |
| 2430 // Tests that we mark an entry as incomplete when the request is cancelled. | 2440 // Tests that we mark an entry as incomplete when the request is cancelled. |
| 2431 TEST(HttpCache, Set_Truncated_Flag) { | 2441 TEST(HttpCache, Set_Truncated_Flag) { |
| 2432 MockHttpCache cache; | 2442 MockHttpCache cache; |
| 2433 cache.http_cache()->set_enable_range_support(true); | 2443 cache.http_cache()->set_enable_range_support(true); |
| 2434 | 2444 |
| 2435 MockHttpRequest request(kSimpleGET_Transaction); | 2445 MockHttpRequest request(kSimpleGET_Transaction); |
| 2436 | 2446 |
| 2437 Context* c = new Context(cache.http_cache()->CreateTransaction()); | 2447 Context* c = new Context(); |
| 2448 int rv = cache.http_cache()->CreateTransaction(&c->trans); |
| 2449 EXPECT_EQ(net::OK, rv); |
| 2438 | 2450 |
| 2439 int rv = c->trans->Start(&request, &c->callback, NULL); | 2451 rv = c->trans->Start(&request, &c->callback, NULL); |
| 2440 if (rv == net::ERR_IO_PENDING) | 2452 if (rv == net::ERR_IO_PENDING) |
| 2441 rv = c->callback.WaitForResult(); | 2453 rv = c->callback.WaitForResult(); |
| 2442 | 2454 |
| 2443 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2455 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2444 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2456 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2445 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2457 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2446 | 2458 |
| 2447 // Make sure that the entry has some data stored. | 2459 // Make sure that the entry has some data stored. |
| 2448 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); | 2460 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); |
| 2449 rv = c->trans->Read(buf, buf->size(), &c->callback); | 2461 rv = c->trans->Read(buf, buf->size(), &c->callback); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2586 | 2598 |
| 2587 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); | 2599 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); |
| 2588 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; | 2600 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; |
| 2589 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; | 2601 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; |
| 2590 | 2602 |
| 2591 MockHttpRequest request(kTestTransaction); | 2603 MockHttpRequest request(kTestTransaction); |
| 2592 TestCompletionCallback callback; | 2604 TestCompletionCallback callback; |
| 2593 | 2605 |
| 2594 // write to the cache | 2606 // write to the cache |
| 2595 { | 2607 { |
| 2596 scoped_ptr<net::HttpTransaction> trans( | 2608 scoped_ptr<net::HttpTransaction> trans; |
| 2597 cache.http_cache()->CreateTransaction()); | 2609 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 2610 EXPECT_EQ(net::OK, rv); |
| 2598 ASSERT_TRUE(trans.get()); | 2611 ASSERT_TRUE(trans.get()); |
| 2599 | 2612 |
| 2600 int rv = trans->Start(&request, &callback, NULL); | 2613 rv = trans->Start(&request, &callback, NULL); |
| 2601 if (rv == net::ERR_IO_PENDING) | 2614 if (rv == net::ERR_IO_PENDING) |
| 2602 rv = callback.WaitForResult(); | 2615 rv = callback.WaitForResult(); |
| 2603 ASSERT_EQ(net::OK, rv); | 2616 ASSERT_EQ(net::OK, rv); |
| 2604 | 2617 |
| 2605 const net::HttpResponseInfo* info = trans->GetResponseInfo(); | 2618 const net::HttpResponseInfo* info = trans->GetResponseInfo(); |
| 2606 ASSERT_TRUE(info); | 2619 ASSERT_TRUE(info); |
| 2607 | 2620 |
| 2608 EXPECT_EQ(info->headers->response_code(), 301); | 2621 EXPECT_EQ(info->headers->response_code(), 301); |
| 2609 | 2622 |
| 2610 std::string location; | 2623 std::string location; |
| 2611 info->headers->EnumerateHeader(NULL, "Location", &location); | 2624 info->headers->EnumerateHeader(NULL, "Location", &location); |
| 2612 EXPECT_EQ(location, "http://www.bar.com/"); | 2625 EXPECT_EQ(location, "http://www.bar.com/"); |
| 2613 | 2626 |
| 2614 // Destroy transaction when going out of scope. We have not actually | 2627 // Destroy transaction when going out of scope. We have not actually |
| 2615 // read the response body -- want to test that it is still getting cached. | 2628 // read the response body -- want to test that it is still getting cached. |
| 2616 } | 2629 } |
| 2617 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 2630 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 2618 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2631 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2619 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2632 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2620 | 2633 |
| 2621 // read from the cache | 2634 // read from the cache |
| 2622 { | 2635 { |
| 2623 scoped_ptr<net::HttpTransaction> trans( | 2636 scoped_ptr<net::HttpTransaction> trans; |
| 2624 cache.http_cache()->CreateTransaction()); | 2637 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 2638 EXPECT_EQ(net::OK, rv); |
| 2625 ASSERT_TRUE(trans.get()); | 2639 ASSERT_TRUE(trans.get()); |
| 2626 | 2640 |
| 2627 int rv = trans->Start(&request, &callback, NULL); | 2641 rv = trans->Start(&request, &callback, NULL); |
| 2628 if (rv == net::ERR_IO_PENDING) | 2642 if (rv == net::ERR_IO_PENDING) |
| 2629 rv = callback.WaitForResult(); | 2643 rv = callback.WaitForResult(); |
| 2630 ASSERT_EQ(net::OK, rv); | 2644 ASSERT_EQ(net::OK, rv); |
| 2631 | 2645 |
| 2632 const net::HttpResponseInfo* info = trans->GetResponseInfo(); | 2646 const net::HttpResponseInfo* info = trans->GetResponseInfo(); |
| 2633 ASSERT_TRUE(info); | 2647 ASSERT_TRUE(info); |
| 2634 | 2648 |
| 2635 EXPECT_EQ(info->headers->response_code(), 301); | 2649 EXPECT_EQ(info->headers->response_code(), 301); |
| 2636 | 2650 |
| 2637 std::string location; | 2651 std::string location; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2740 | 2754 |
| 2741 // write to the cache | 2755 // write to the cache |
| 2742 RunTransactionTest(cache.http_cache(), transaction); | 2756 RunTransactionTest(cache.http_cache(), transaction); |
| 2743 | 2757 |
| 2744 // Test that it was not cached. | 2758 // Test that it was not cached. |
| 2745 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 2759 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
| 2746 | 2760 |
| 2747 MockHttpRequest request(transaction); | 2761 MockHttpRequest request(transaction); |
| 2748 TestCompletionCallback callback; | 2762 TestCompletionCallback callback; |
| 2749 | 2763 |
| 2750 scoped_ptr<net::HttpTransaction> trans( | 2764 scoped_ptr<net::HttpTransaction> trans; |
| 2751 cache.http_cache()->CreateTransaction()); | 2765 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 2766 EXPECT_EQ(net::OK, rv); |
| 2752 ASSERT_TRUE(trans.get()); | 2767 ASSERT_TRUE(trans.get()); |
| 2753 | 2768 |
| 2754 int rv = trans->Start(&request, &callback, NULL); | 2769 rv = trans->Start(&request, &callback, NULL); |
| 2755 if (rv == net::ERR_IO_PENDING) | 2770 if (rv == net::ERR_IO_PENDING) |
| 2756 rv = callback.WaitForResult(); | 2771 rv = callback.WaitForResult(); |
| 2757 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | 2772 ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 2758 } | 2773 } |
| 2759 | 2774 |
| 2760 // Ensure that we don't crash by if left-behind transactions. | 2775 // Ensure that we don't crash by if left-behind transactions. |
| 2761 TEST(HttpCache, OutlivedTransactions) { | 2776 TEST(HttpCache, OutlivedTransactions) { |
| 2762 MockHttpCache* cache = new MockHttpCache; | 2777 MockHttpCache* cache = new MockHttpCache; |
| 2763 | 2778 |
| 2764 net::HttpTransaction* trans = cache->http_cache()->CreateTransaction(); | 2779 scoped_ptr<net::HttpTransaction> trans; |
| 2780 int rv = cache->http_cache()->CreateTransaction(&trans); |
| 2781 EXPECT_EQ(net::OK, rv); |
| 2782 |
| 2765 delete cache; | 2783 delete cache; |
| 2766 delete trans; | 2784 trans.reset(); |
| 2767 } | 2785 } |
| 2768 | 2786 |
| 2769 // Test that the disabled mode works. | 2787 // Test that the disabled mode works. |
| 2770 TEST(HttpCache, CacheDisabledMode) { | 2788 TEST(HttpCache, CacheDisabledMode) { |
| 2771 MockHttpCache cache; | 2789 MockHttpCache cache; |
| 2772 | 2790 |
| 2773 // write to the cache | 2791 // write to the cache |
| 2774 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 2792 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 2775 | 2793 |
| 2776 // go into disabled mode | 2794 // go into disabled mode |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2852 std::string headers; | 2870 std::string headers; |
| 2853 response.headers->GetNormalizedHeaders(&headers); | 2871 response.headers->GetNormalizedHeaders(&headers); |
| 2854 | 2872 |
| 2855 EXPECT_EQ("HTTP/1.1 200 OK\n" | 2873 EXPECT_EQ("HTTP/1.1 200 OK\n" |
| 2856 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | 2874 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
| 2857 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | 2875 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
| 2858 headers); | 2876 headers); |
| 2859 | 2877 |
| 2860 RemoveMockTransaction(&mock_network_response); | 2878 RemoveMockTransaction(&mock_network_response); |
| 2861 } | 2879 } |
| OLD | NEW |