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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
495 net::TestCompletionCallback cb; | 495 net::TestCompletionCallback cb; |
496 // This will lazily initialize the backend. | 496 // This will lazily initialize the backend. |
497 int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); | 497 int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); |
498 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 498 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
499 } | 499 } |
500 | 500 |
501 TEST(HttpCache, SimpleGET) { | 501 TEST(HttpCache, SimpleGET) { |
502 MockHttpCache cache; | 502 MockHttpCache cache; |
503 | 503 |
504 // write to the cache | 504 // write to the cache |
505 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 505 net::HttpResponseInfo response_info; |
506 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | |
rvargas (doing something else)
2013/04/08 19:11:52
SimpleGET_NetworkAccessed_Cache is a superset of w
Randy Smith (Not in Mondays)
2013/04/08 21:03:11
SimpleGET_NetworkAccessed_Cache doesn't test to ma
| |
507 &response_info); | |
506 | 508 |
507 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 509 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
508 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 510 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
509 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 511 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
512 EXPECT_TRUE(response_info.network_accessed); | |
510 } | 513 } |
511 | 514 |
512 TEST(HttpCache, SimpleGETNoDiskCache) { | 515 TEST(HttpCache, SimpleGETNoDiskCache) { |
513 MockHttpCache cache; | 516 MockHttpCache cache; |
514 | 517 |
515 cache.disk_cache()->set_fail_requests(); | 518 cache.disk_cache()->set_fail_requests(); |
516 | 519 |
517 net::CapturingBoundNetLog log; | 520 net::CapturingBoundNetLog log; |
518 log.SetLogLevel(net::NetLog::LOG_BASIC); | 521 log.SetLogLevel(net::NetLog::LOG_BASIC); |
519 | 522 |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
871 // Re-run transaction; make sure the result came from the network, | 874 // Re-run transaction; make sure the result came from the network, |
872 // not the cache. | 875 // not the cache. |
873 transaction.data = "Changed data."; | 876 transaction.data = "Changed data."; |
874 AddMockTransaction(&transaction); | 877 AddMockTransaction(&transaction); |
875 net::HttpResponseInfo response_info; | 878 net::HttpResponseInfo response_info; |
876 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | 879 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
877 &response_info); | 880 &response_info); |
878 | 881 |
879 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 882 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
880 EXPECT_FALSE(response_info.server_data_unavailable); | 883 EXPECT_FALSE(response_info.server_data_unavailable); |
884 EXPECT_TRUE(response_info.network_accessed); | |
881 | 885 |
882 RemoveMockTransaction(&transaction); | 886 RemoveMockTransaction(&transaction); |
883 } | 887 } |
884 | 888 |
885 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | 889 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on |
886 // offline failure | 890 // offline failure |
887 TEST(HttpCache, SimpleGET_CacheOverride_Offline) { | 891 TEST(HttpCache, SimpleGET_CacheOverride_Offline) { |
888 MockHttpCache cache; | 892 MockHttpCache cache; |
889 | 893 |
890 // Prime cache. | 894 // Prime cache. |
(...skipping 19 matching lines...) Expand all Loading... | |
910 net::DEFAULT_PRIORITY, &trans, NULL); | 914 net::DEFAULT_PRIORITY, &trans, NULL); |
911 EXPECT_EQ(net::OK, rv); | 915 EXPECT_EQ(net::OK, rv); |
912 ASSERT_TRUE(trans.get()); | 916 ASSERT_TRUE(trans.get()); |
913 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | 917 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); |
914 EXPECT_EQ(net::OK, callback.GetResult(rv)); | 918 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
915 | 919 |
916 const net::HttpResponseInfo* response_info = trans->GetResponseInfo(); | 920 const net::HttpResponseInfo* response_info = trans->GetResponseInfo(); |
917 ASSERT_TRUE(response_info); | 921 ASSERT_TRUE(response_info); |
918 EXPECT_TRUE(response_info->server_data_unavailable); | 922 EXPECT_TRUE(response_info->server_data_unavailable); |
919 EXPECT_TRUE(response_info->was_cached); | 923 EXPECT_TRUE(response_info->was_cached); |
924 EXPECT_FALSE(response_info->network_accessed); | |
920 ReadAndVerifyTransaction(trans.get(), transaction); | 925 ReadAndVerifyTransaction(trans.get(), transaction); |
921 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 926 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
922 | 927 |
923 RemoveMockTransaction(&transaction); | 928 RemoveMockTransaction(&transaction); |
924 } | 929 } |
925 | 930 |
926 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | 931 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on |
927 // non-offline failure failure | 932 // non-offline failure. |
928 TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) { | 933 TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) { |
929 MockHttpCache cache; | 934 MockHttpCache cache; |
930 | 935 |
931 // Prime cache. | 936 // Prime cache. |
932 MockTransaction transaction(kSimpleGET_Transaction); | 937 MockTransaction transaction(kSimpleGET_Transaction); |
933 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; | 938 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; |
934 transaction.response_headers = "Cache-Control: no-cache\n"; | 939 transaction.response_headers = "Cache-Control: no-cache\n"; |
935 | 940 |
936 AddMockTransaction(&transaction); | 941 AddMockTransaction(&transaction); |
937 RunTransactionTest(cache.http_cache(), transaction); | 942 RunTransactionTest(cache.http_cache(), transaction); |
938 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 943 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
939 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 944 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
940 RemoveMockTransaction(&transaction); | 945 RemoveMockTransaction(&transaction); |
941 | 946 |
942 // Network failure with non-offline error; should fail with that error. | 947 // Network failure with non-offline error; should fail with that error. |
943 transaction.return_code = net::ERR_PROXY_CONNECTION_FAILED; | 948 transaction.return_code = net::ERR_PROXY_CONNECTION_FAILED; |
944 AddMockTransaction(&transaction); | 949 AddMockTransaction(&transaction); |
945 | 950 |
946 net::HttpResponseInfo response_info2; | 951 net::HttpResponseInfo response_info2; |
947 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | 952 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, |
948 &response_info2); | 953 &response_info2); |
949 | 954 |
950 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 955 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
951 EXPECT_FALSE(response_info2.server_data_unavailable); | 956 EXPECT_FALSE(response_info2.server_data_unavailable); |
952 | 957 |
953 RemoveMockTransaction(&transaction); | 958 RemoveMockTransaction(&transaction); |
954 } | 959 } |
955 | 960 |
961 // Confirm if we have a fresh entry in cache, it isn't marked as | |
962 // network verified. | |
963 TEST(HttpCache, SimpleGET_NetworkAccessed_Cache) { | |
964 MockHttpCache cache; | |
965 | |
966 // Prime cache. | |
967 MockTransaction transaction(kSimpleGET_Transaction); | |
968 | |
969 AddMockTransaction(&transaction); | |
rvargas (doing something else)
2013/04/08 19:11:52
The predefined transactions are already registered
Randy Smith (Not in Mondays)
2013/04/08 21:03:11
Done.
| |
970 RunTransactionTest(cache.http_cache(), transaction); | |
971 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
972 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
973 RemoveMockTransaction(&transaction); | |
974 | |
975 // Re-run transaction; make sure we don't mark the network as accessed. | |
976 AddMockTransaction(&transaction); | |
977 net::HttpResponseInfo response_info; | |
978 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | |
979 &response_info); | |
980 | |
981 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
982 EXPECT_FALSE(response_info.server_data_unavailable); | |
983 EXPECT_FALSE(response_info.network_accessed); | |
984 | |
985 RemoveMockTransaction(&transaction); | |
986 } | |
987 | |
956 TEST(HttpCache, SimpleGET_LoadBypassCache) { | 988 TEST(HttpCache, SimpleGET_LoadBypassCache) { |
957 MockHttpCache cache; | 989 MockHttpCache cache; |
958 | 990 |
959 // Write to the cache. | 991 // Write to the cache. |
960 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 992 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
961 | 993 |
962 // Force this transaction to write to the cache again. | 994 // Force this transaction to write to the cache again. |
963 MockTransaction transaction(kSimpleGET_Transaction); | 995 MockTransaction transaction(kSimpleGET_Transaction); |
964 transaction.load_flags |= net::LOAD_BYPASS_CACHE; | 996 transaction.load_flags |= net::LOAD_BYPASS_CACHE; |
965 | 997 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1037 // write to the cache | 1069 // write to the cache |
1038 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1070 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
1039 | 1071 |
1040 // read from the cache | 1072 // read from the cache |
1041 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1073 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
1042 | 1074 |
1043 // force this transaction to validate the cache | 1075 // force this transaction to validate the cache |
1044 MockTransaction transaction(kSimpleGET_Transaction); | 1076 MockTransaction transaction(kSimpleGET_Transaction); |
1045 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; | 1077 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; |
1046 | 1078 |
1047 RunTransactionTest(cache.http_cache(), transaction); | 1079 net::HttpResponseInfo response_info; |
1080 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | |
1081 &response_info); | |
1048 | 1082 |
1049 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1083 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
1050 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 1084 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
1051 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1085 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1086 EXPECT_TRUE(response_info.network_accessed); | |
1052 } | 1087 } |
1053 | 1088 |
1054 TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) { | 1089 TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) { |
1055 MockHttpCache cache; | 1090 MockHttpCache cache; |
1056 | 1091 |
1057 // write to the cache | 1092 // write to the cache |
1058 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1093 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
1059 | 1094 |
1060 // read from the cache | 1095 // read from the cache |
1061 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 1096 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
(...skipping 4689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5751 trans->SetPriority(net::HIGHEST); | 5786 trans->SetPriority(net::HIGHEST); |
5752 // Should trigger a new network transaction and pick up the new | 5787 // Should trigger a new network transaction and pick up the new |
5753 // priority. | 5788 // priority. |
5754 ReadAndVerifyTransaction(trans.get(), transaction); | 5789 ReadAndVerifyTransaction(trans.get(), transaction); |
5755 | 5790 |
5756 EXPECT_EQ(net::HIGHEST, | 5791 EXPECT_EQ(net::HIGHEST, |
5757 cache.network_layer()->last_create_transaction_priority()); | 5792 cache.network_layer()->last_create_transaction_priority()); |
5758 | 5793 |
5759 RemoveMockTransaction(&kRangeGET_TransactionOK); | 5794 RemoveMockTransaction(&kRangeGET_TransactionOK); |
5760 } | 5795 } |
OLD | NEW |