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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 6712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6723 EXPECT_EQ(range_response_size * 2, received_bytes); | 6723 EXPECT_EQ(range_response_size * 2, received_bytes); |
6724 | 6724 |
6725 RemoveMockTransaction(&kRangeGET_TransactionOK); | 6725 RemoveMockTransaction(&kRangeGET_TransactionOK); |
6726 } | 6726 } |
6727 | 6727 |
6728 static void CheckResourceFreshnessHeader(const net::HttpRequestInfo* request, | 6728 static void CheckResourceFreshnessHeader(const net::HttpRequestInfo* request, |
6729 std::string* response_status, | 6729 std::string* response_status, |
6730 std::string* response_headers, | 6730 std::string* response_headers, |
6731 std::string* response_data) { | 6731 std::string* response_data) { |
6732 std::string value; | 6732 std::string value; |
6733 EXPECT_TRUE( | 6733 EXPECT_TRUE(request->extra_headers.GetHeader("Resource-Freshness", &value)); |
6734 request->extra_headers.GetHeader("Chromium-Resource-Freshness", &value)); | |
6735 EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value); | 6734 EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value); |
6736 } | 6735 } |
6737 | 6736 |
6738 // Verify that the Chromium-Resource-Freshness header is sent on a revalidation | 6737 // Verify that the Resource-Freshness header is sent on a revalidation if the |
6739 // if the stale-while-revalidate directive was on the response. | 6738 // stale-while-revalidate directive was on the response. |
6740 // TODO(ricea): Rename this test when a final name for the header is decided. | |
6741 TEST(HttpCache, ResourceFreshnessHeaderSent) { | 6739 TEST(HttpCache, ResourceFreshnessHeaderSent) { |
6742 MockHttpCache cache; | 6740 MockHttpCache cache; |
6743 | 6741 |
6744 ScopedMockTransaction stale_while_revalidate_transaction( | 6742 ScopedMockTransaction stale_while_revalidate_transaction( |
6745 kSimpleGET_Transaction); | 6743 kSimpleGET_Transaction); |
6746 stale_while_revalidate_transaction.response_headers = | 6744 stale_while_revalidate_transaction.response_headers = |
6747 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 6745 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
6748 "Age: 10801\n" | 6746 "Age: 10801\n" |
6749 "Cache-Control: max-age=3600,stale-while-revalidate=7200\n"; | 6747 "Cache-Control: max-age=3600,stale-while-revalidate=7200\n"; |
6750 | 6748 |
6751 // Write to the cache. | 6749 // Write to the cache. |
6752 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | 6750 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
6753 | 6751 |
6754 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6752 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6755 | 6753 |
6756 // Send the request again and check that Chromium-Resource-Freshness header is | 6754 // Send the request again and check that Resource-Freshness header is added. |
6757 // added. | |
6758 stale_while_revalidate_transaction.handler = CheckResourceFreshnessHeader; | 6755 stale_while_revalidate_transaction.handler = CheckResourceFreshnessHeader; |
6759 | 6756 |
6760 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | 6757 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
6761 | 6758 |
6762 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 6759 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
6763 } | 6760 } |
6764 | 6761 |
6765 static void CheckResourceFreshnessAbsent(const net::HttpRequestInfo* request, | 6762 static void CheckResourceFreshnessAbsent(const net::HttpRequestInfo* request, |
6766 std::string* response_status, | 6763 std::string* response_status, |
6767 std::string* response_headers, | 6764 std::string* response_headers, |
6768 std::string* response_data) { | 6765 std::string* response_data) { |
6769 EXPECT_FALSE(request->extra_headers.HasHeader("Chromium-Resource-Freshness")); | 6766 EXPECT_FALSE(request->extra_headers.HasHeader("Resource-Freshness")); |
6770 } | 6767 } |
6771 | 6768 |
6772 // Verify that the Chromium-Resource-Freshness header is not sent when | 6769 // Verify that the Resource-Freshness header is not sent when |
6773 // stale-while-revalidate is 0. | 6770 // stale-while-revalidate is 0. |
6774 TEST(HttpCache, ResourceFreshnessHeaderNotSent) { | 6771 TEST(HttpCache, ResourceFreshnessHeaderNotSent) { |
6775 MockHttpCache cache; | 6772 MockHttpCache cache; |
6776 | 6773 |
6777 ScopedMockTransaction stale_while_revalidate_transaction( | 6774 ScopedMockTransaction stale_while_revalidate_transaction( |
6778 kSimpleGET_Transaction); | 6775 kSimpleGET_Transaction); |
6779 stale_while_revalidate_transaction.response_headers = | 6776 stale_while_revalidate_transaction.response_headers = |
6780 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 6777 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
6781 "Age: 10801\n" | 6778 "Age: 10801\n" |
6782 "Cache-Control: max-age=3600,stale-while-revalidate=0\n"; | 6779 "Cache-Control: max-age=3600,stale-while-revalidate=0\n"; |
6783 | 6780 |
6784 // Write to the cache. | 6781 // Write to the cache. |
6785 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | 6782 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
6786 | 6783 |
6787 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6784 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6788 | 6785 |
6789 // Send the request again and check that Chromium-Resource-Freshness header is | 6786 // Send the request again and check that Resource-Freshness header is absent. |
6790 // absent. | |
6791 stale_while_revalidate_transaction.handler = CheckResourceFreshnessAbsent; | 6787 stale_while_revalidate_transaction.handler = CheckResourceFreshnessAbsent; |
6792 | 6788 |
6793 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); | 6789 RunTransactionTest(cache.http_cache(), stale_while_revalidate_transaction); |
6794 | 6790 |
6795 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 6791 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
6796 } | 6792 } |
OLD | NEW |