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 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "net/http/http_util.h" | 9 #include "net/http/http_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
865 ranges[j].first_byte_position()); | 865 ranges[j].first_byte_position()); |
866 EXPECT_EQ(tests[i].expected_ranges[j].expected_last_byte_position, | 866 EXPECT_EQ(tests[i].expected_ranges[j].expected_last_byte_position, |
867 ranges[j].last_byte_position()); | 867 ranges[j].last_byte_position()); |
868 EXPECT_EQ(tests[i].expected_ranges[j].expected_suffix_length, | 868 EXPECT_EQ(tests[i].expected_ranges[j].expected_suffix_length, |
869 ranges[j].suffix_length()); | 869 ranges[j].suffix_length()); |
870 } | 870 } |
871 } | 871 } |
872 } | 872 } |
873 } | 873 } |
874 | 874 |
| 875 TEST(HttpUtilTest, ParseRetryAfterHeader) { |
| 876 base::Time::Exploded now_exploded = { 2014, 11, -1, 5, 22, 39, 30, 0 }; |
| 877 base::Time now = base::Time::FromUTCExploded(now_exploded); |
| 878 |
| 879 base::Time::Exploded later_exploded = { 2015, 1, -1, 1, 12, 34, 56, 0 }; |
| 880 base::Time later = base::Time::FromUTCExploded(later_exploded); |
| 881 |
| 882 const struct { |
| 883 const char* retry_after_string; |
| 884 bool expected_return_value; |
| 885 base::TimeDelta expected_retry_after; |
| 886 } tests[] = { |
| 887 { "", false, base::TimeDelta() }, |
| 888 { "-3", false, base::TimeDelta() }, |
| 889 { "-2", false, base::TimeDelta() }, |
| 890 { "-1", false, base::TimeDelta() }, |
| 891 { "0", true, base::TimeDelta::FromSeconds(0) }, |
| 892 { "1", true, base::TimeDelta::FromSeconds(1) }, |
| 893 { "2", true, base::TimeDelta::FromSeconds(2) }, |
| 894 { "3", true, base::TimeDelta::FromSeconds(3) }, |
| 895 { "60", true, base::TimeDelta::FromSeconds(60) }, |
| 896 { "3600", true, base::TimeDelta::FromSeconds(3600) }, |
| 897 { "86400", true, base::TimeDelta::FromSeconds(86400) }, |
| 898 { "Thu, 1 Jan 2015 12:34:56 GMT", true, later - now }, |
| 899 { "Mon, 1 Jan 1900 12:34:56 GMT", false, base::TimeDelta() } |
| 900 }; |
| 901 |
| 902 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 903 base::TimeDelta retry_after; |
| 904 bool return_value = HttpUtil::ParseRetryAfterHeader( |
| 905 tests[i].retry_after_string, now, &retry_after); |
| 906 EXPECT_EQ(tests[i].expected_return_value, return_value) |
| 907 << "Test case " << i << ": expected " << tests[i].expected_return_value |
| 908 << " but got " << return_value << "."; |
| 909 if (tests[i].expected_return_value && return_value) { |
| 910 EXPECT_EQ(tests[i].expected_retry_after, retry_after) |
| 911 << "Test case " << i << ": expected " |
| 912 << tests[i].expected_retry_after.InSeconds() << "s but got " |
| 913 << retry_after.InSeconds() << "s."; |
| 914 } |
| 915 } |
| 916 } |
| 917 |
875 namespace { | 918 namespace { |
876 void CheckCurrentNameValuePair(HttpUtil::NameValuePairsIterator* parser, | 919 void CheckCurrentNameValuePair(HttpUtil::NameValuePairsIterator* parser, |
877 bool expect_valid, | 920 bool expect_valid, |
878 std::string expected_name, | 921 std::string expected_name, |
879 std::string expected_value) { | 922 std::string expected_value) { |
880 ASSERT_EQ(expect_valid, parser->valid()); | 923 ASSERT_EQ(expect_valid, parser->valid()); |
881 if (!expect_valid) { | 924 if (!expect_valid) { |
882 return; | 925 return; |
883 } | 926 } |
884 | 927 |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1064 TEST(HttpUtilTest, NameValuePairsIteratorMissingEndQuote) { | 1107 TEST(HttpUtilTest, NameValuePairsIteratorMissingEndQuote) { |
1065 std::string data = "name='value"; | 1108 std::string data = "name='value"; |
1066 HttpUtil::NameValuePairsIterator parser(data.begin(), data.end(), ';'); | 1109 HttpUtil::NameValuePairsIterator parser(data.begin(), data.end(), ';'); |
1067 EXPECT_TRUE(parser.valid()); | 1110 EXPECT_TRUE(parser.valid()); |
1068 | 1111 |
1069 ASSERT_NO_FATAL_FAILURE( | 1112 ASSERT_NO_FATAL_FAILURE( |
1070 CheckNextNameValuePair(&parser, true, true, "name", "value")); | 1113 CheckNextNameValuePair(&parser, true, true, "name", "value")); |
1071 ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair( | 1114 ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair( |
1072 &parser, false, true, std::string(), std::string())); | 1115 &parser, false, true, std::string(), std::string())); |
1073 } | 1116 } |
OLD | NEW |