| 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 7926 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7937 } | 7937 } |
| 7938 | 7938 |
| 7939 TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) { | 7939 TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) { |
| 7940 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); | 7940 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
| 7941 AdvanceTime(kRequireValidationSecs); | 7941 AdvanceTime(kRequireValidationSecs); |
| 7942 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); | 7942 EXPECT_TRUE(TransactionRequiredNetwork(LOAD_PREFETCH)); |
| 7943 AdvanceTime(kRequireValidationSecs); | 7943 AdvanceTime(kRequireValidationSecs); |
| 7944 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); | 7944 EXPECT_FALSE(TransactionRequiredNetwork(LOAD_NORMAL)); |
| 7945 } | 7945 } |
| 7946 | 7946 |
| 7947 static void CheckResourceFreshnessHeader(const HttpRequestInfo* request, | |
| 7948 std::string* response_status, | |
| 7949 std::string* response_headers, | |
| 7950 std::string* response_data) { | |
| 7951 std::string value; | |
| 7952 EXPECT_TRUE(request->extra_headers.GetHeader("Resource-Freshness", &value)); | |
| 7953 EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value); | |
| 7954 } | |
| 7955 | |
| 7956 // Verify that the Resource-Freshness header is sent on a revalidation if the | |
| 7957 // stale-while-revalidate directive was on the response. | |
| 7958 TEST(HttpCache, ResourceFreshnessHeaderSent) { | |
| 7959 MockHttpCache cache; | |
| 7960 | |
| 7961 ScopedMockTransaction stale_while_revalidate_transaction( | |
| 7962 kSimpleGET_Transaction); | |
| 7963 stale_while_revalidate_transaction.response_headers = | |
| 7964 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
| 7965 "Age: 10801\n" | |
| 7966 "Cache-Control: max-age=3600,stale-while-revalidate=7200\n"; | |
| 7967 | |
| 7968 // Write to the cache. | |
| 7969 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
| 7970 | |
| 7971 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
| 7972 | |
| 7973 // Send the request again and check that Resource-Freshness header is added. | |
| 7974 stale_while_revalidate_transaction.handler = CheckResourceFreshnessHeader; | |
| 7975 | |
| 7976 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
| 7977 | |
| 7978 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
| 7979 } | |
| 7980 | |
| 7981 static void CheckResourceFreshnessAbsent(const HttpRequestInfo* request, | |
| 7982 std::string* response_status, | |
| 7983 std::string* response_headers, | |
| 7984 std::string* response_data) { | |
| 7985 EXPECT_FALSE(request->extra_headers.HasHeader("Resource-Freshness")); | |
| 7986 } | |
| 7987 | |
| 7988 // Verify that the Resource-Freshness header is not sent when | |
| 7989 // stale-while-revalidate is 0. | |
| 7990 TEST(HttpCache, ResourceFreshnessHeaderNotSent) { | |
| 7991 MockHttpCache cache; | |
| 7992 | |
| 7993 ScopedMockTransaction stale_while_revalidate_transaction( | |
| 7994 kSimpleGET_Transaction); | |
| 7995 stale_while_revalidate_transaction.response_headers = | |
| 7996 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
| 7997 "Age: 10801\n" | |
| 7998 "Cache-Control: max-age=3600,stale-while-revalidate=0\n"; | |
| 7999 | |
| 8000 // Write to the cache. | |
| 8001 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
| 8002 | |
| 8003 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
| 8004 | |
| 8005 // Send the request again and check that Resource-Freshness header is absent. | |
| 8006 stale_while_revalidate_transaction.handler = CheckResourceFreshnessAbsent; | |
| 8007 | |
| 8008 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
| 8009 | |
| 8010 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
| 8011 } | |
| 8012 | |
| 8013 TEST(HttpCache, StaleContentNotUsedWhenLoadFlagNotSet) { | |
| 8014 MockHttpCache cache; | |
| 8015 | |
| 8016 ScopedMockTransaction stale_while_revalidate_transaction( | |
| 8017 kSimpleGET_Transaction); | |
| 8018 | |
| 8019 stale_while_revalidate_transaction.response_headers = | |
| 8020 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
| 8021 "Age: 10801\n" | |
| 8022 "Cache-Control: max-age=0,stale-while-revalidate=86400\n"; | |
| 8023 | |
| 8024 // Write to the cache. | |
| 8025 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
| 8026 | |
| 8027 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
| 8028 | |
| 8029 // Send the request again and check that it is sent to the network again. | |
| 8030 HttpResponseInfo response_info; | |
| 8031 RunTransactionTestWithResponseInfo( | |
| 8032 cache.http_cache(), stale_while_revalidate_transaction, &response_info); | |
| 8033 | |
| 8034 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
| 8035 EXPECT_FALSE(response_info.async_revalidation_required); | |
| 8036 } | |
| 8037 | |
| 8038 TEST(HttpCache, StaleContentUsedWhenLoadFlagSetAndUsable) { | |
| 8039 MockHttpCache cache; | |
| 8040 | |
| 8041 ScopedMockTransaction stale_while_revalidate_transaction( | |
| 8042 kSimpleGET_Transaction); | |
| 8043 stale_while_revalidate_transaction.load_flags |= | |
| 8044 LOAD_SUPPORT_ASYNC_REVALIDATION; | |
| 8045 stale_while_revalidate_transaction.response_headers = | |
| 8046 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
| 8047 "Age: 10801\n" | |
| 8048 "Cache-Control: max-age=0,stale-while-revalidate=86400\n"; | |
| 8049 | |
| 8050 // Write to the cache. | |
| 8051 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
| 8052 | |
| 8053 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
| 8054 | |
| 8055 // Send the request again and check that it is not sent to the network again. | |
| 8056 HttpResponseInfo response_info; | |
| 8057 RunTransactionTestWithResponseInfo( | |
| 8058 cache.http_cache(), stale_while_revalidate_transaction, &response_info); | |
| 8059 | |
| 8060 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
| 8061 EXPECT_TRUE(response_info.async_revalidation_required); | |
| 8062 } | |
| 8063 | |
| 8064 TEST(HttpCache, StaleContentNotUsedWhenUnusable) { | |
| 8065 MockHttpCache cache; | |
| 8066 | |
| 8067 ScopedMockTransaction stale_while_revalidate_transaction( | |
| 8068 kSimpleGET_Transaction); | |
| 8069 stale_while_revalidate_transaction.load_flags |= | |
| 8070 LOAD_SUPPORT_ASYNC_REVALIDATION; | |
| 8071 stale_while_revalidate_transaction.response_headers = | |
| 8072 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
| 8073 "Age: 10801\n" | |
| 8074 "Cache-Control: max-age=0,stale-while-revalidate=1800\n"; | |
| 8075 | |
| 8076 // Write to the cache. | |
| 8077 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | |
| 8078 | |
| 8079 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
| 8080 | |
| 8081 // Send the request again and check that it is sent to the network again. | |
| 8082 HttpResponseInfo response_info; | |
| 8083 RunTransactionTestWithResponseInfo( | |
| 8084 cache.http_cache(), stale_while_revalidate_transaction, &response_info); | |
| 8085 | |
| 8086 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
| 8087 EXPECT_FALSE(response_info.async_revalidation_required); | |
| 8088 } | |
| 8089 | |
| 8090 // Tests that we allow multiple simultaneous, non-overlapping transactions to | 7947 // Tests that we allow multiple simultaneous, non-overlapping transactions to |
| 8091 // take place on a sparse entry. | 7948 // take place on a sparse entry. |
| 8092 TEST(HttpCache, RangeGET_MultipleRequests) { | 7949 TEST(HttpCache, RangeGET_MultipleRequests) { |
| 8093 MockHttpCache cache; | 7950 MockHttpCache cache; |
| 8094 | 7951 |
| 8095 // Create a transaction for bytes 0-9. | 7952 // Create a transaction for bytes 0-9. |
| 8096 MockHttpRequest request(kRangeGET_TransactionOK); | 7953 MockHttpRequest request(kRangeGET_TransactionOK); |
| 8097 MockTransaction transaction(kRangeGET_TransactionOK); | 7954 MockTransaction transaction(kRangeGET_TransactionOK); |
| 8098 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; | 7955 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 8099 transaction.data = "rg: 00-09 "; | 7956 transaction.data = "rg: 00-09 "; |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8402 ASSERT_TRUE(attrs->GetDictionary( | 8259 ASSERT_TRUE(attrs->GetDictionary( |
| 8403 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 8260 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
| 8404 std::string size; | 8261 std::string size; |
| 8405 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 8262 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
| 8406 int actual_size = 0; | 8263 int actual_size = 0; |
| 8407 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 8264 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
| 8408 ASSERT_LT(0, actual_size); | 8265 ASSERT_LT(0, actual_size); |
| 8409 } | 8266 } |
| 8410 | 8267 |
| 8411 } // namespace net | 8268 } // namespace net |
| OLD | NEW |