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_response_headers.h" | 5 #include "net/http/http_response_headers.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <iostream> | 10 #include <iostream> |
(...skipping 747 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 "Content-type: */*\n", | 758 "Content-type: */*\n", |
759 "", false, | 759 "", false, |
760 "", false, | 760 "", false, |
761 "*/*" }, | 761 "*/*" }, |
762 }; | 762 }; |
763 | 763 |
764 INSTANTIATE_TEST_CASE_P(HttpResponseHeaders, | 764 INSTANTIATE_TEST_CASE_P(HttpResponseHeaders, |
765 ContentTypeTest, | 765 ContentTypeTest, |
766 testing::ValuesIn(mimetype_tests)); | 766 testing::ValuesIn(mimetype_tests)); |
767 | 767 |
768 struct RequiresValidationTestData { | 768 enum TestValidationType { |
769 const char* headers; | 769 TEST_VALIDATION_NONE, |
770 ValidationType validation_type; | 770 TEST_VALIDATION_ASYNCHRONOUS, |
| 771 TEST_VALIDATION_SYNCHRONOUS, |
771 }; | 772 }; |
772 | 773 |
773 class RequiresValidationTest | 774 struct ExpirationTimesTestData { |
774 : public HttpResponseHeadersTest, | 775 const char* headers; |
775 public ::testing::WithParamInterface<RequiresValidationTestData> { | 776 TestValidationType validation_type; |
776 }; | 777 }; |
777 | 778 |
778 TEST_P(RequiresValidationTest, RequiresValidation) { | 779 class ExpirationTimesTest |
779 const RequiresValidationTestData test = GetParam(); | 780 : public HttpResponseHeadersTest, |
| 781 public ::testing::WithParamInterface<ExpirationTimesTestData> {}; |
| 782 |
| 783 TEST_P(ExpirationTimesTest, ExpirationTimes) { |
| 784 const ExpirationTimesTestData test = GetParam(); |
780 | 785 |
781 base::Time request_time, response_time, current_time; | 786 base::Time request_time, response_time, current_time; |
782 base::Time::FromString("Wed, 28 Nov 2007 00:40:09 GMT", &request_time); | 787 base::Time::FromString("Wed, 28 Nov 2007 00:40:09 GMT", &request_time); |
783 base::Time::FromString("Wed, 28 Nov 2007 00:40:12 GMT", &response_time); | 788 base::Time::FromString("Wed, 28 Nov 2007 00:40:12 GMT", &response_time); |
784 base::Time::FromString("Wed, 28 Nov 2007 00:45:20 GMT", ¤t_time); | 789 base::Time::FromString("Wed, 28 Nov 2007 00:45:20 GMT", ¤t_time); |
785 | 790 |
786 std::string headers(test.headers); | 791 std::string headers(test.headers); |
787 HeadersToRaw(&headers); | 792 HeadersToRaw(&headers); |
788 scoped_refptr<HttpResponseHeaders> parsed(new HttpResponseHeaders(headers)); | 793 scoped_refptr<HttpResponseHeaders> parsed(new HttpResponseHeaders(headers)); |
789 | 794 |
790 ValidationType validation_type = | 795 HttpResponseHeaders::ExpirationTimes expiration_times = |
791 parsed->RequiresValidation(request_time, response_time, current_time); | 796 parsed->GetExpirationTimes(request_time, response_time); |
| 797 TestValidationType validation_type; |
| 798 if (current_time < expiration_times.GetExpirationTime()) |
| 799 validation_type = TEST_VALIDATION_NONE; |
| 800 else if (current_time < expiration_times.GetAsyncExpirationTime()) |
| 801 validation_type = TEST_VALIDATION_ASYNCHRONOUS; |
| 802 else |
| 803 validation_type = TEST_VALIDATION_SYNCHRONOUS; |
792 EXPECT_EQ(test.validation_type, validation_type); | 804 EXPECT_EQ(test.validation_type, validation_type); |
793 } | 805 } |
794 | 806 |
795 const struct RequiresValidationTestData requires_validation_tests[] = { | 807 const struct ExpirationTimesTestData expiration_times_tests[] = { |
796 // No expiry info: expires immediately. | 808 // No expiry info: expires immediately. |
797 { "HTTP/1.1 200 OK\n" | 809 {"HTTP/1.1 200 OK\n" |
798 "\n", | 810 "\n", |
799 VALIDATION_SYNCHRONOUS | 811 TEST_VALIDATION_SYNCHRONOUS}, |
800 }, | 812 // No expiry info: expires immediately. |
801 // No expiry info: expires immediately. | 813 {"HTTP/1.1 200 OK\n" |
802 { "HTTP/1.1 200 OK\n" | 814 "\n", |
803 "\n", | 815 TEST_VALIDATION_SYNCHRONOUS}, |
804 VALIDATION_SYNCHRONOUS | 816 // Valid for a little while. |
805 }, | 817 {"HTTP/1.1 200 OK\n" |
806 // Valid for a little while. | 818 "cache-control: max-age=10000\n" |
807 { "HTTP/1.1 200 OK\n" | 819 "\n", |
808 "cache-control: max-age=10000\n" | 820 TEST_VALIDATION_NONE}, |
809 "\n", | 821 // Expires in the future. |
810 VALIDATION_NONE | 822 {"HTTP/1.1 200 OK\n" |
811 }, | 823 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
812 // Expires in the future. | 824 "expires: Wed, 28 Nov 2007 01:00:00 GMT\n" |
813 { "HTTP/1.1 200 OK\n" | 825 "\n", |
814 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 826 TEST_VALIDATION_NONE}, |
815 "expires: Wed, 28 Nov 2007 01:00:00 GMT\n" | 827 // Already expired. |
816 "\n", | 828 {"HTTP/1.1 200 OK\n" |
817 VALIDATION_NONE | 829 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
818 }, | 830 "expires: Wed, 28 Nov 2007 00:00:00 GMT\n" |
819 // Already expired. | 831 "\n", |
820 { "HTTP/1.1 200 OK\n" | 832 TEST_VALIDATION_SYNCHRONOUS}, |
821 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 833 // Max-age trumps expires. |
822 "expires: Wed, 28 Nov 2007 00:00:00 GMT\n" | 834 {"HTTP/1.1 200 OK\n" |
823 "\n", | 835 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
824 VALIDATION_SYNCHRONOUS | 836 "expires: Wed, 28 Nov 2007 00:00:00 GMT\n" |
825 }, | 837 "cache-control: max-age=10000\n" |
826 // Max-age trumps expires. | 838 "\n", |
827 { "HTTP/1.1 200 OK\n" | 839 TEST_VALIDATION_NONE}, |
828 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 840 // Last-modified heuristic: modified a while ago. |
829 "expires: Wed, 28 Nov 2007 00:00:00 GMT\n" | 841 {"HTTP/1.1 200 OK\n" |
830 "cache-control: max-age=10000\n" | 842 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
831 "\n", | 843 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" |
832 VALIDATION_NONE | 844 "\n", |
833 }, | 845 TEST_VALIDATION_NONE}, |
834 // Last-modified heuristic: modified a while ago. | 846 {"HTTP/1.1 203 Non-Authoritative Information\n" |
835 { "HTTP/1.1 200 OK\n" | 847 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
836 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 848 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" |
837 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" | 849 "\n", |
838 "\n", | 850 TEST_VALIDATION_NONE}, |
839 VALIDATION_NONE | 851 {"HTTP/1.1 206 Partial Content\n" |
840 }, | 852 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
841 { "HTTP/1.1 203 Non-Authoritative Information\n" | 853 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" |
842 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 854 "\n", |
843 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" | 855 TEST_VALIDATION_NONE}, |
844 "\n", | 856 // Last-modified heuristic: modified recently. |
845 VALIDATION_NONE | 857 {"HTTP/1.1 200 OK\n" |
846 }, | 858 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
847 { "HTTP/1.1 206 Partial Content\n" | 859 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n" |
848 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 860 "\n", |
849 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" | 861 TEST_VALIDATION_SYNCHRONOUS}, |
850 "\n", | 862 {"HTTP/1.1 203 Non-Authoritative Information\n" |
851 VALIDATION_NONE | 863 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
852 }, | 864 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n" |
853 // Last-modified heuristic: modified recently. | 865 "\n", |
854 { "HTTP/1.1 200 OK\n" | 866 TEST_VALIDATION_SYNCHRONOUS}, |
855 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 867 {"HTTP/1.1 206 Partial Content\n" |
856 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n" | 868 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
857 "\n", | 869 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n" |
858 VALIDATION_SYNCHRONOUS | 870 "\n", |
859 }, | 871 TEST_VALIDATION_SYNCHRONOUS}, |
860 { "HTTP/1.1 203 Non-Authoritative Information\n" | 872 // Cached permanent redirect. |
861 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 873 {"HTTP/1.1 301 Moved Permanently\n" |
862 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n" | 874 "\n", |
863 "\n", | 875 TEST_VALIDATION_NONE}, |
864 VALIDATION_SYNCHRONOUS | 876 // Another cached permanent redirect. |
865 }, | 877 {"HTTP/1.1 308 Permanent Redirect\n" |
866 { "HTTP/1.1 206 Partial Content\n" | 878 "\n", |
867 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 879 TEST_VALIDATION_NONE}, |
868 "last-modified: Wed, 28 Nov 2007 00:40:10 GMT\n" | 880 // Cached redirect: not reusable even though by default it would be. |
869 "\n", | 881 {"HTTP/1.1 300 Multiple Choices\n" |
870 VALIDATION_SYNCHRONOUS | 882 "Cache-Control: no-cache\n" |
871 }, | 883 "\n", |
872 // Cached permanent redirect. | 884 TEST_VALIDATION_SYNCHRONOUS}, |
873 { "HTTP/1.1 301 Moved Permanently\n" | 885 // Cached forever by default. |
874 "\n", | 886 {"HTTP/1.1 410 Gone\n" |
875 VALIDATION_NONE | 887 "\n", |
876 }, | 888 TEST_VALIDATION_NONE}, |
877 // Another cached permanent redirect. | 889 // Cached temporary redirect: not reusable. |
878 { "HTTP/1.1 308 Permanent Redirect\n" | 890 {"HTTP/1.1 302 Found\n" |
879 "\n", | 891 "\n", |
880 VALIDATION_NONE | 892 TEST_VALIDATION_SYNCHRONOUS}, |
881 }, | 893 // Cached temporary redirect: reusable. |
882 // Cached redirect: not reusable even though by default it would be. | 894 {"HTTP/1.1 302 Found\n" |
883 { "HTTP/1.1 300 Multiple Choices\n" | 895 "cache-control: max-age=10000\n" |
884 "Cache-Control: no-cache\n" | 896 "\n", |
885 "\n", | 897 TEST_VALIDATION_NONE}, |
886 VALIDATION_SYNCHRONOUS | 898 // Cache-control: max-age=N overrides expires: date in the past. |
887 }, | 899 {"HTTP/1.1 200 OK\n" |
888 // Cached forever by default. | 900 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
889 { "HTTP/1.1 410 Gone\n" | 901 "expires: Wed, 28 Nov 2007 00:20:11 GMT\n" |
890 "\n", | 902 "cache-control: max-age=10000\n" |
891 VALIDATION_NONE | 903 "\n", |
892 }, | 904 TEST_VALIDATION_NONE}, |
893 // Cached temporary redirect: not reusable. | 905 // Cache-control: no-store overrides expires: in the future. |
894 { "HTTP/1.1 302 Found\n" | 906 {"HTTP/1.1 200 OK\n" |
895 "\n", | 907 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
896 VALIDATION_SYNCHRONOUS | 908 "expires: Wed, 29 Nov 2007 00:40:11 GMT\n" |
897 }, | 909 "cache-control: no-store,private,no-cache=\"foo\"\n" |
898 // Cached temporary redirect: reusable. | 910 "\n", |
899 { "HTTP/1.1 302 Found\n" | 911 TEST_VALIDATION_SYNCHRONOUS}, |
900 "cache-control: max-age=10000\n" | 912 // Pragma: no-cache overrides last-modified heuristic. |
901 "\n", | 913 {"HTTP/1.1 200 OK\n" |
902 VALIDATION_NONE | 914 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
903 }, | 915 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" |
904 // Cache-control: max-age=N overrides expires: date in the past. | 916 "pragma: no-cache\n" |
905 { "HTTP/1.1 200 OK\n" | 917 "\n", |
906 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 918 TEST_VALIDATION_SYNCHRONOUS}, |
907 "expires: Wed, 28 Nov 2007 00:20:11 GMT\n" | 919 // max-age has expired, needs synchronous revalidation |
908 "cache-control: max-age=10000\n" | 920 {"HTTP/1.1 200 OK\n" |
909 "\n", | 921 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
910 VALIDATION_NONE | 922 "cache-control: max-age=300\n" |
911 }, | 923 "\n", |
912 // Cache-control: no-store overrides expires: in the future. | 924 TEST_VALIDATION_SYNCHRONOUS}, |
913 { "HTTP/1.1 200 OK\n" | 925 // max-age has expired, stale-while-revalidate has not, eligible for |
914 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 926 // asynchronous revalidation |
915 "expires: Wed, 29 Nov 2007 00:40:11 GMT\n" | 927 {"HTTP/1.1 200 OK\n" |
916 "cache-control: no-store,private,no-cache=\"foo\"\n" | 928 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
917 "\n", | 929 "cache-control: max-age=300, stale-while-revalidate=3600\n" |
918 VALIDATION_SYNCHRONOUS | 930 "\n", |
919 }, | 931 TEST_VALIDATION_ASYNCHRONOUS}, |
920 // Pragma: no-cache overrides last-modified heuristic. | 932 // max-age and stale-while-revalidate have expired, needs synchronous |
921 { "HTTP/1.1 200 OK\n" | 933 // revalidation |
922 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 934 {"HTTP/1.1 200 OK\n" |
923 "last-modified: Wed, 27 Nov 2007 08:00:00 GMT\n" | 935 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
924 "pragma: no-cache\n" | 936 "cache-control: max-age=300, stale-while-revalidate=5\n" |
925 "\n", | 937 "\n", |
926 VALIDATION_SYNCHRONOUS | 938 TEST_VALIDATION_SYNCHRONOUS}, |
927 }, | 939 // max-age is 0, stale-while-revalidate is large enough to permit |
928 // max-age has expired, needs synchronous revalidation | 940 // asynchronous revalidation |
929 { "HTTP/1.1 200 OK\n" | 941 {"HTTP/1.1 200 OK\n" |
930 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 942 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
931 "cache-control: max-age=300\n" | 943 "cache-control: max-age=0, stale-while-revalidate=360\n" |
932 "\n", | 944 "\n", |
933 VALIDATION_SYNCHRONOUS | 945 TEST_VALIDATION_ASYNCHRONOUS}, |
934 }, | 946 // stale-while-revalidate must not override no-cache or similar directives. |
935 // max-age has expired, stale-while-revalidate has not, eligible for | 947 {"HTTP/1.1 200 OK\n" |
936 // asynchronous revalidation | 948 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
937 { "HTTP/1.1 200 OK\n" | 949 "cache-control: no-cache, stale-while-revalidate=360\n" |
938 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 950 "\n", |
939 "cache-control: max-age=300, stale-while-revalidate=3600\n" | 951 TEST_VALIDATION_SYNCHRONOUS}, |
940 "\n", | 952 // max-age has not expired, so no revalidation is needed. |
941 VALIDATION_ASYNCHRONOUS | 953 {"HTTP/1.1 200 OK\n" |
942 }, | 954 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
943 // max-age and stale-while-revalidate have expired, needs synchronous | 955 "cache-control: max-age=3600, stale-while-revalidate=3600\n" |
944 // revalidation | 956 "\n", |
945 { "HTTP/1.1 200 OK\n" | 957 TEST_VALIDATION_NONE}, |
946 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 958 // must-revalidate overrides stale-while-revalidate, so synchronous |
947 "cache-control: max-age=300, stale-while-revalidate=5\n" | 959 // validation |
948 "\n", | 960 // is needed. |
949 VALIDATION_SYNCHRONOUS | 961 {"HTTP/1.1 200 OK\n" |
950 }, | 962 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" |
951 // max-age is 0, stale-while-revalidate is large enough to permit | 963 "cache-control: must-revalidate, max-age=300, " |
952 // asynchronous revalidation | 964 "stale-while-revalidate=3600\n" |
953 { "HTTP/1.1 200 OK\n" | 965 "\n", |
954 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | 966 TEST_VALIDATION_SYNCHRONOUS}, |
955 "cache-control: max-age=0, stale-while-revalidate=360\n" | |
956 "\n", | |
957 VALIDATION_ASYNCHRONOUS | |
958 }, | |
959 // stale-while-revalidate must not override no-cache or similar directives. | |
960 { "HTTP/1.1 200 OK\n" | |
961 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | |
962 "cache-control: no-cache, stale-while-revalidate=360\n" | |
963 "\n", | |
964 VALIDATION_SYNCHRONOUS | |
965 }, | |
966 // max-age has not expired, so no revalidation is needed. | |
967 { "HTTP/1.1 200 OK\n" | |
968 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | |
969 "cache-control: max-age=3600, stale-while-revalidate=3600\n" | |
970 "\n", | |
971 VALIDATION_NONE | |
972 }, | |
973 // must-revalidate overrides stale-while-revalidate, so synchronous validation | |
974 // is needed. | |
975 { "HTTP/1.1 200 OK\n" | |
976 "date: Wed, 28 Nov 2007 00:40:11 GMT\n" | |
977 "cache-control: must-revalidate, max-age=300, stale-while-revalidate=3600\n" | |
978 "\n", | |
979 VALIDATION_SYNCHRONOUS | |
980 }, | |
981 | 967 |
982 // TODO(darin): Add many many more tests here. | 968 // TODO(darin): Add many many more tests here. |
983 }; | 969 }; |
984 | 970 |
985 INSTANTIATE_TEST_CASE_P(HttpResponseHeaders, | 971 INSTANTIATE_TEST_CASE_P(HttpResponseHeaders, |
986 RequiresValidationTest, | 972 ExpirationTimesTest, |
987 testing::ValuesIn(requires_validation_tests)); | 973 testing::ValuesIn(expiration_times_tests)); |
988 | 974 |
989 struct UpdateTestData { | 975 struct UpdateTestData { |
990 const char* orig_headers; | 976 const char* orig_headers; |
991 const char* new_headers; | 977 const char* new_headers; |
992 const char* expected_headers; | 978 const char* expected_headers; |
993 }; | 979 }; |
994 | 980 |
995 class UpdateTest | 981 class UpdateTest |
996 : public HttpResponseHeadersTest, | 982 : public HttpResponseHeadersTest, |
997 public ::testing::WithParamInterface<UpdateTestData> { | 983 public ::testing::WithParamInterface<UpdateTestData> { |
(...skipping 1216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2214 TEST_F(HttpResponseHeadersCacheControlTest, | 2200 TEST_F(HttpResponseHeadersCacheControlTest, |
2215 FirstStaleWhileRevalidateValueUsed) { | 2201 FirstStaleWhileRevalidateValueUsed) { |
2216 InitializeHeadersWithCacheControl( | 2202 InitializeHeadersWithCacheControl( |
2217 "stale-while-revalidate=1,stale-while-revalidate=7200"); | 2203 "stale-while-revalidate=1,stale-while-revalidate=7200"); |
2218 EXPECT_EQ(TimeDelta::FromSeconds(1), GetStaleWhileRevalidateValue()); | 2204 EXPECT_EQ(TimeDelta::FromSeconds(1), GetStaleWhileRevalidateValue()); |
2219 } | 2205 } |
2220 | 2206 |
2221 } // namespace | 2207 } // namespace |
2222 | 2208 |
2223 } // namespace net | 2209 } // namespace net |
OLD | NEW |