Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(540)

Side by Side Diff: net/http/http_response_headers_unittest.cc

Issue 455623003: stale-while-revalidate experimental implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Stop setting Cache-Control: max-age=0 on async revalidations. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <algorithm> 5 #include <algorithm>
6 #include <limits> 6 #include <limits>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/pickle.h" 10 #include "base/pickle.h"
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 "Content-type: */*\n", 770 "Content-type: */*\n",
771 "", false, 771 "", false,
772 "", false, 772 "", false,
773 "*/*" }, 773 "*/*" },
774 }; 774 };
775 775
776 INSTANTIATE_TEST_CASE_P(HttpResponseHeaders, 776 INSTANTIATE_TEST_CASE_P(HttpResponseHeaders,
777 ContentTypeTest, 777 ContentTypeTest,
778 testing::ValuesIn(mimetype_tests)); 778 testing::ValuesIn(mimetype_tests));
779 779
780 typedef net::HttpResponseHeaders::ValidationType ValidationType;
781 const ValidationType VALIDATION_NONE =
782 net::HttpResponseHeaders::VALIDATION_NONE;
783 const ValidationType VALIDATION_SYNCHRONOUS =
784 net::HttpResponseHeaders::VALIDATION_SYNCHRONOUS;
785 const ValidationType VALIDATION_ASYNCHRONOUS =
786 net::HttpResponseHeaders::VALIDATION_ASYNCHRONOUS;
787
780 struct RequiresValidationTestData { 788 struct RequiresValidationTestData {
781 const char* headers; 789 const char* headers;
782 bool requires_validation; 790 ValidationType validation_type;
783 }; 791 };
784 792
785 class RequiresValidationTest 793 class RequiresValidationTest
786 : public HttpResponseHeadersTest, 794 : public HttpResponseHeadersTest,
787 public ::testing::WithParamInterface<RequiresValidationTestData> { 795 public ::testing::WithParamInterface<RequiresValidationTestData> {
788 }; 796 };
789 797
790 TEST_P(RequiresValidationTest, RequiresValidation) { 798 TEST_P(RequiresValidationTest, RequiresValidation) {
791 const RequiresValidationTestData test = GetParam(); 799 const RequiresValidationTestData test = GetParam();
792 800
793 base::Time request_time, response_time, current_time; 801 base::Time request_time, response_time, current_time;
794 base::Time::FromString("Wed, 28 Nov 2007 00:40:09 GMT", &request_time); 802 base::Time::FromString("Wed, 28 Nov 2007 00:40:09 GMT", &request_time);
795 base::Time::FromString("Wed, 28 Nov 2007 00:40:12 GMT", &response_time); 803 base::Time::FromString("Wed, 28 Nov 2007 00:40:12 GMT", &response_time);
796 base::Time::FromString("Wed, 28 Nov 2007 00:45:20 GMT", &current_time); 804 base::Time::FromString("Wed, 28 Nov 2007 00:45:20 GMT", &current_time);
797 805
798 std::string headers(test.headers); 806 std::string headers(test.headers);
799 HeadersToRaw(&headers); 807 HeadersToRaw(&headers);
800 scoped_refptr<net::HttpResponseHeaders> parsed( 808 scoped_refptr<net::HttpResponseHeaders> parsed(
801 new net::HttpResponseHeaders(headers)); 809 new net::HttpResponseHeaders(headers));
802 810
803 bool requires_validation = 811 ValidationType validation_type =
804 parsed->RequiresValidation(request_time, response_time, current_time); 812 parsed->RequiresValidation(request_time, response_time, current_time);
805 EXPECT_EQ(test.requires_validation, requires_validation); 813 EXPECT_EQ(test.validation_type, validation_type);
806 } 814 }
807 815
808 const struct RequiresValidationTestData requires_validation_tests[] = { 816 const struct RequiresValidationTestData requires_validation_tests[] = {
809 // No expiry info: expires immediately. 817 // No expiry info: expires immediately.
810 { "HTTP/1.1 200 OK\n" 818 { "HTTP/1.1 200 OK\n"
811 "\n", 819 "\n",
812 true 820 VALIDATION_SYNCHRONOUS
813 }, 821 },
814 // No expiry info: expires immediately. 822 // No expiry info: expires immediately.
815 { "HTTP/1.1 200 OK\n" 823 { "HTTP/1.1 200 OK\n"
816 "\n", 824 "\n",
817 true 825 VALIDATION_SYNCHRONOUS
818 }, 826 },
819 // Valid for a little while. 827 // Valid for a little while.
820 { "HTTP/1.1 200 OK\n" 828 { "HTTP/1.1 200 OK\n"
821 "cache-control: max-age=10000\n" 829 "cache-control: max-age=10000\n"
822 "\n", 830 "\n",
823 false 831 VALIDATION_NONE
824 }, 832 },
825 // Expires in the future. 833 // Expires in the future.
826 { "HTTP/1.1 200 OK\n" 834 { "HTTP/1.1 200 OK\n"
827 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 835 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
828 "expires: Wed, 28 Nov 2007 01:00:00 GMT\n" 836 "expires: Wed, 28 Nov 2007 01:00:00 GMT\n"
829 "\n", 837 "\n",
830 false 838 VALIDATION_NONE
831 }, 839 },
832 // Already expired. 840 // Already expired.
833 { "HTTP/1.1 200 OK\n" 841 { "HTTP/1.1 200 OK\n"
834 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 842 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
835 "expires: Wed, 28 Nov 2007 00:00:00 GMT\n" 843 "expires: Wed, 28 Nov 2007 00:00:00 GMT\n"
836 "\n", 844 "\n",
837 true 845 VALIDATION_SYNCHRONOUS
838 }, 846 },
839 // Max-age trumps expires. 847 // Max-age trumps expires.
840 { "HTTP/1.1 200 OK\n" 848 { "HTTP/1.1 200 OK\n"
841 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 849 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
842 "expires: Wed, 28 Nov 2007 00:00:00 GMT\n" 850 "expires: Wed, 28 Nov 2007 00:00:00 GMT\n"
843 "cache-control: max-age=10000\n" 851 "cache-control: max-age=10000\n"
844 "\n", 852 "\n",
845 false 853 VALIDATION_NONE
846 }, 854 },
847 // Last-modified heuristic: modified a while ago. 855 // Last-modified heuristic: modified a while ago.
848 { "HTTP/1.1 200 OK\n" 856 { "HTTP/1.1 200 OK\n"
849 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 857 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
850 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" 858 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n"
851 "\n", 859 "\n",
852 false 860 VALIDATION_NONE
853 }, 861 },
854 { "HTTP/1.1 203 Non-Authoritative Information\n" 862 { "HTTP/1.1 203 Non-Authoritative Information\n"
855 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 863 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
856 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" 864 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n"
857 "\n", 865 "\n",
858 false 866 VALIDATION_NONE
859 }, 867 },
860 { "HTTP/1.1 206 Partial Content\n" 868 { "HTTP/1.1 206 Partial Content\n"
861 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 869 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
862 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" 870 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n"
863 "\n", 871 "\n",
864 false 872 VALIDATION_NONE
865 }, 873 },
866 // Last-modified heuristic: modified recently. 874 // Last-modified heuristic: modified recently.
867 { "HTTP/1.1 200 OK\n" 875 { "HTTP/1.1 200 OK\n"
868 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 876 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
869 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n" 877 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n"
870 "\n", 878 "\n",
871 true 879 VALIDATION_SYNCHRONOUS
872 }, 880 },
873 { "HTTP/1.1 203 Non-Authoritative Information\n" 881 { "HTTP/1.1 203 Non-Authoritative Information\n"
874 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 882 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
875 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n" 883 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n"
876 "\n", 884 "\n",
877 true 885 VALIDATION_SYNCHRONOUS
878 }, 886 },
879 { "HTTP/1.1 206 Partial Content\n" 887 { "HTTP/1.1 206 Partial Content\n"
880 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 888 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
881 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n" 889 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n"
882 "\n", 890 "\n",
883 true 891 VALIDATION_SYNCHRONOUS
884 }, 892 },
885 // Cached permanent redirect. 893 // Cached permanent redirect.
886 { "HTTP/1.1 301 Moved Permanently\n" 894 { "HTTP/1.1 301 Moved Permanently\n"
887 "\n", 895 "\n",
888 false 896 VALIDATION_NONE
889 }, 897 },
890 // Another cached permanent redirect. 898 // Another cached permanent redirect.
891 { "HTTP/1.1 308 Permanent Redirect\n" 899 { "HTTP/1.1 308 Permanent Redirect\n"
892 "\n", 900 "\n",
893 false 901 VALIDATION_NONE
894 }, 902 },
895 // Cached redirect: not reusable even though by default it would be. 903 // Cached redirect: not reusable even though by default it would be.
896 { "HTTP/1.1 300 Multiple Choices\n" 904 { "HTTP/1.1 300 Multiple Choices\n"
897 "Cache-Control: no-cache\n" 905 "Cache-Control: no-cache\n"
898 "\n", 906 "\n",
899 true 907 VALIDATION_SYNCHRONOUS
900 }, 908 },
901 // Cached forever by default. 909 // Cached forever by default.
902 { "HTTP/1.1 410 Gone\n" 910 { "HTTP/1.1 410 Gone\n"
903 "\n", 911 "\n",
904 false 912 VALIDATION_NONE
905 }, 913 },
906 // Cached temporary redirect: not reusable. 914 // Cached temporary redirect: not reusable.
907 { "HTTP/1.1 302 Found\n" 915 { "HTTP/1.1 302 Found\n"
908 "\n", 916 "\n",
909 true 917 VALIDATION_SYNCHRONOUS
910 }, 918 },
911 // Cached temporary redirect: reusable. 919 // Cached temporary redirect: reusable.
912 { "HTTP/1.1 302 Found\n" 920 { "HTTP/1.1 302 Found\n"
913 "cache-control: max-age=10000\n" 921 "cache-control: max-age=10000\n"
914 "\n", 922 "\n",
915 false 923 VALIDATION_NONE
916 }, 924 },
917 // Cache-control: max-age=N overrides expires: date in the past. 925 // Cache-control: max-age=N overrides expires: date in the past.
918 { "HTTP/1.1 200 OK\n" 926 { "HTTP/1.1 200 OK\n"
919 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 927 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
920 "expires: Wed, 28 Nov 2007 00:20:11 GMT\n" 928 "expires: Wed, 28 Nov 2007 00:20:11 GMT\n"
921 "cache-control: max-age=10000\n" 929 "cache-control: max-age=10000\n"
922 "\n", 930 "\n",
923 false 931 VALIDATION_NONE
924 }, 932 },
925 // Cache-control: no-store overrides expires: in the future. 933 // Cache-control: no-store overrides expires: in the future.
926 { "HTTP/1.1 200 OK\n" 934 { "HTTP/1.1 200 OK\n"
927 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 935 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
928 "expires: Wed, 29 Nov 2007 00:40:11 GMT\n" 936 "expires: Wed, 29 Nov 2007 00:40:11 GMT\n"
929 "cache-control: no-store,private,no-cache=\"foo\"\n" 937 "cache-control: no-store,private,no-cache=\"foo\"\n"
930 "\n", 938 "\n",
931 true 939 VALIDATION_SYNCHRONOUS
932 }, 940 },
933 // Pragma: no-cache overrides last-modified heuristic. 941 // Pragma: no-cache overrides last-modified heuristic.
934 { "HTTP/1.1 200 OK\n" 942 { "HTTP/1.1 200 OK\n"
935 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" 943 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
936 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" 944 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n"
937 "pragma: no-cache\n" 945 "pragma: no-cache\n"
938 "\n", 946 "\n",
939 true 947 VALIDATION_SYNCHRONOUS
948 },
949 // max-age has expired, needs synchronous revalidation
950 { "HTTP/1.1 200 OK\n"
951 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
952 "cache-control: max-age=300\n"
953 "\n",
954 VALIDATION_SYNCHRONOUS
955 },
956 // max-age has expired, stale-while-revalidate has not, eligible for
957 // asynchronous revalidation
958 { "HTTP/1.1 200 OK\n"
959 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
960 "cache-control: max-age=300, stale-while-revalidate=3600\n"
961 "\n",
962 VALIDATION_ASYNCHRONOUS
963 },
964 // max-age and stale-while-revalidate have expired, needs synchronous
965 // revalidation
966 { "HTTP/1.1 200 OK\n"
967 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
968 "cache-control: max-age=300, stale-while-revalidate=5\n"
969 "\n",
970 VALIDATION_SYNCHRONOUS
971 },
972 // max-age is 0, stale-while-revalidate is large enough to permit
973 // asynchronous revalidation
974 { "HTTP/1.1 200 OK\n"
975 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
976 "cache-control: max-age=0, stale-while-revalidate=360\n"
977 "\n",
978 VALIDATION_ASYNCHRONOUS
979 },
980 // stale-while-revalidate must not override no-cache or similar directives.
981 { "HTTP/1.1 200 OK\n"
982 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
983 "cache-control: no-cache, stale-while-revalidate=360\n"
984 "\n",
985 VALIDATION_SYNCHRONOUS
986 },
987 // max-age has not expired, so no revalidation is needed.
988 { "HTTP/1.1 200 OK\n"
989 "date: Wed, 28 Nov 2007 00:40:11 GMT\n"
990 "cache-control: max-age=3600, stale-while-revalidate=3600\n"
991 "\n",
992 VALIDATION_NONE
940 }, 993 },
941 994
942 // TODO(darin): Add many many more tests here. 995 // TODO(darin): Add many many more tests here.
943 }; 996 };
944 997
945 INSTANTIATE_TEST_CASE_P(HttpResponseHeaders, 998 INSTANTIATE_TEST_CASE_P(HttpResponseHeaders,
946 RequiresValidationTest, 999 RequiresValidationTest,
947 testing::ValuesIn(requires_validation_tests)); 1000 testing::ValuesIn(requires_validation_tests));
948 1001
949 struct UpdateTestData { 1002 struct UpdateTestData {
(...skipping 1297 matching lines...) Expand 10 before | Expand all | Expand 10 after
2247 } 2300 }
2248 2301
2249 TEST_F(HttpResponseHeadersCacheControlTest, 2302 TEST_F(HttpResponseHeadersCacheControlTest,
2250 FirstStaleWhileRevalidateValueUsed) { 2303 FirstStaleWhileRevalidateValueUsed) {
2251 InitializeHeadersWithCacheControl( 2304 InitializeHeadersWithCacheControl(
2252 "stale-while-revalidate=1,stale-while-revalidate=7200"); 2305 "stale-while-revalidate=1,stale-while-revalidate=7200");
2253 EXPECT_EQ(TimeDelta::FromSeconds(1), GetStaleWhileRevalidateValue()); 2306 EXPECT_EQ(TimeDelta::FromSeconds(1), GetStaleWhileRevalidateValue());
2254 } 2307 }
2255 2308
2256 } // end namespace 2309 } // end namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698