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