| 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/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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 { "foo: 1\r\nbar: 2", "foo", true }, | 113 { "foo: 1\r\nbar: 2", "foo", true }, |
| 114 { "fOO: 1\r\nbar: 2", "foo", true }, | 114 { "fOO: 1\r\nbar: 2", "foo", true }, |
| 115 { "g: 0\r\nfoo: 1\r\nbar: 2", "foo", true }, | 115 { "g: 0\r\nfoo: 1\r\nbar: 2", "foo", true }, |
| 116 }; | 116 }; |
| 117 for (size_t i = 0; i < arraysize(tests); ++i) { | 117 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 118 bool result = HttpUtil::HasHeader(tests[i].headers, tests[i].name); | 118 bool result = HttpUtil::HasHeader(tests[i].headers, tests[i].name); |
| 119 EXPECT_EQ(tests[i].expected_result, result); | 119 EXPECT_EQ(tests[i].expected_result, result); |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 TEST(HttpUtilTest, StripHeaders) { | |
| 124 static const char* const headers = | |
| 125 "Origin: origin\r\n" | |
| 126 "Content-Type: text/plain\r\n" | |
| 127 "Cookies: foo1\r\n" | |
| 128 "Custom: baz\r\n" | |
| 129 "COOKIES: foo2\r\n" | |
| 130 "Server: Apache\r\n" | |
| 131 "OrIGin: origin2\r\n"; | |
| 132 | |
| 133 static const char* const header_names[] = { | |
| 134 "origin", "content-type", "cookies" | |
| 135 }; | |
| 136 | |
| 137 static const char* const expected_stripped_headers = | |
| 138 "Custom: baz\r\n" | |
| 139 "Server: Apache\r\n"; | |
| 140 | |
| 141 EXPECT_EQ(expected_stripped_headers, | |
| 142 HttpUtil::StripHeaders(headers, header_names, | |
| 143 arraysize(header_names))); | |
| 144 } | |
| 145 | |
| 146 TEST(HttpUtilTest, HeadersIterator) { | 123 TEST(HttpUtilTest, HeadersIterator) { |
| 147 std::string headers = "foo: 1\t\r\nbar: hello world\r\nbaz: 3 \r\n"; | 124 std::string headers = "foo: 1\t\r\nbar: hello world\r\nbaz: 3 \r\n"; |
| 148 | 125 |
| 149 HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\r\n"); | 126 HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\r\n"); |
| 150 | 127 |
| 151 ASSERT_TRUE(it.GetNext()); | 128 ASSERT_TRUE(it.GetNext()); |
| 152 EXPECT_EQ(std::string("foo"), it.name()); | 129 EXPECT_EQ(std::string("foo"), it.name()); |
| 153 EXPECT_EQ(std::string("1"), it.values()); | 130 EXPECT_EQ(std::string("1"), it.values()); |
| 154 | 131 |
| 155 ASSERT_TRUE(it.GetNext()); | 132 ASSERT_TRUE(it.GetNext()); |
| (...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 std::string boundary; | 814 std::string boundary; |
| 838 HttpUtil::ParseContentType(tests[i].content_type, &mime_type, &charset, | 815 HttpUtil::ParseContentType(tests[i].content_type, &mime_type, &charset, |
| 839 &had_charset, &boundary); | 816 &had_charset, &boundary); |
| 840 EXPECT_EQ(tests[i].expected_mime_type, mime_type) << "i=" << i; | 817 EXPECT_EQ(tests[i].expected_mime_type, mime_type) << "i=" << i; |
| 841 EXPECT_EQ(tests[i].expected_charset, charset) << "i=" << i; | 818 EXPECT_EQ(tests[i].expected_charset, charset) << "i=" << i; |
| 842 EXPECT_EQ(tests[i].expected_had_charset, had_charset) << "i=" << i; | 819 EXPECT_EQ(tests[i].expected_had_charset, had_charset) << "i=" << i; |
| 843 EXPECT_EQ(tests[i].expected_boundary, boundary) << "i=" << i; | 820 EXPECT_EQ(tests[i].expected_boundary, boundary) << "i=" << i; |
| 844 } | 821 } |
| 845 } | 822 } |
| 846 | 823 |
| 847 TEST(HttpUtilTest, ParseRanges) { | |
| 848 const struct { | |
| 849 const char* const headers; | |
| 850 bool expected_return_value; | |
| 851 size_t expected_ranges_size; | |
| 852 const struct { | |
| 853 int64_t expected_first_byte_position; | |
| 854 int64_t expected_last_byte_position; | |
| 855 int64_t expected_suffix_length; | |
| 856 } expected_ranges[10]; | |
| 857 } tests[] = { | |
| 858 { "Range: bytes=0-10", | |
| 859 true, | |
| 860 1, | |
| 861 { {0, 10, -1}, } | |
| 862 }, | |
| 863 { "Range: bytes=10-0", | |
| 864 false, | |
| 865 0, | |
| 866 {} | |
| 867 }, | |
| 868 { "Range: BytES=0-10", | |
| 869 true, | |
| 870 1, | |
| 871 { {0, 10, -1}, } | |
| 872 }, | |
| 873 { "Range: megabytes=0-10", | |
| 874 false, | |
| 875 0, | |
| 876 {} | |
| 877 }, | |
| 878 { "Range: bytes0-10", | |
| 879 false, | |
| 880 0, | |
| 881 {} | |
| 882 }, | |
| 883 { "Range: bytes=0-0,0-10,10-20,100-200,100-,-200", | |
| 884 true, | |
| 885 6, | |
| 886 { {0, 0, -1}, | |
| 887 {0, 10, -1}, | |
| 888 {10, 20, -1}, | |
| 889 {100, 200, -1}, | |
| 890 {100, -1, -1}, | |
| 891 {-1, -1, 200}, | |
| 892 } | |
| 893 }, | |
| 894 { "Range: bytes=0-10\r\n" | |
| 895 "Range: bytes=0-10,10-20,100-200,100-,-200", | |
| 896 true, | |
| 897 1, | |
| 898 { {0, 10, -1} | |
| 899 } | |
| 900 }, | |
| 901 { "Range: bytes=", | |
| 902 false, | |
| 903 0, | |
| 904 {} | |
| 905 }, | |
| 906 { "Range: bytes=-", | |
| 907 false, | |
| 908 0, | |
| 909 {} | |
| 910 }, | |
| 911 { "Range: bytes=0-10-", | |
| 912 false, | |
| 913 0, | |
| 914 {} | |
| 915 }, | |
| 916 { "Range: bytes=-0-10", | |
| 917 false, | |
| 918 0, | |
| 919 {} | |
| 920 }, | |
| 921 { "Range: bytes =0-10\r\n", | |
| 922 true, | |
| 923 1, | |
| 924 { {0, 10, -1} | |
| 925 } | |
| 926 }, | |
| 927 { "Range: bytes= 0-10 \r\n", | |
| 928 true, | |
| 929 1, | |
| 930 { {0, 10, -1} | |
| 931 } | |
| 932 }, | |
| 933 { "Range: bytes = 0 - 10 \r\n", | |
| 934 true, | |
| 935 1, | |
| 936 { {0, 10, -1} | |
| 937 } | |
| 938 }, | |
| 939 { "Range: bytes= 0-1 0\r\n", | |
| 940 false, | |
| 941 0, | |
| 942 {} | |
| 943 }, | |
| 944 { "Range: bytes= 0- -10\r\n", | |
| 945 false, | |
| 946 0, | |
| 947 {} | |
| 948 }, | |
| 949 { "Range: bytes= 0 - 1 , 10 -20, 100- 200 , 100-, -200 \r\n", | |
| 950 true, | |
| 951 5, | |
| 952 { {0, 1, -1}, | |
| 953 {10, 20, -1}, | |
| 954 {100, 200, -1}, | |
| 955 {100, -1, -1}, | |
| 956 {-1, -1, 200}, | |
| 957 } | |
| 958 }, | |
| 959 }; | |
| 960 | |
| 961 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 962 std::vector<HttpByteRange> ranges; | |
| 963 bool return_value = HttpUtil::ParseRanges(std::string(tests[i].headers), | |
| 964 &ranges); | |
| 965 EXPECT_EQ(tests[i].expected_return_value, return_value); | |
| 966 if (return_value) { | |
| 967 EXPECT_EQ(tests[i].expected_ranges_size, ranges.size()); | |
| 968 for (size_t j = 0; j < ranges.size(); ++j) { | |
| 969 EXPECT_EQ(tests[i].expected_ranges[j].expected_first_byte_position, | |
| 970 ranges[j].first_byte_position()); | |
| 971 EXPECT_EQ(tests[i].expected_ranges[j].expected_last_byte_position, | |
| 972 ranges[j].last_byte_position()); | |
| 973 EXPECT_EQ(tests[i].expected_ranges[j].expected_suffix_length, | |
| 974 ranges[j].suffix_length()); | |
| 975 } | |
| 976 } | |
| 977 } | |
| 978 } | |
| 979 | |
| 980 TEST(HttpUtilTest, ParseContentRangeHeader) { | 824 TEST(HttpUtilTest, ParseContentRangeHeader) { |
| 981 const struct { | 825 const struct { |
| 982 const char* const content_range_header_spec; | 826 const char* const content_range_header_spec; |
| 983 bool expected_return_value; | 827 bool expected_return_value; |
| 984 int64_t expected_first_byte_position; | 828 int64_t expected_first_byte_position; |
| 985 int64_t expected_last_byte_position; | 829 int64_t expected_last_byte_position; |
| 986 int64_t expected_instance_length; | 830 int64_t expected_instance_length; |
| 987 } tests[] = { | 831 } tests[] = { |
| 988 {"", false, -1, -1, -1}, | 832 {"", false, -1, -1, -1}, |
| 989 {"megabytes 0-10/50", false, -1, -1, -1}, | 833 {"megabytes 0-10/50", false, -1, -1, -1}, |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1493 EXPECT_FALSE(HttpUtil::IsLWS('a')); | 1337 EXPECT_FALSE(HttpUtil::IsLWS('a')); |
| 1494 EXPECT_FALSE(HttpUtil::IsLWS('.')); | 1338 EXPECT_FALSE(HttpUtil::IsLWS('.')); |
| 1495 EXPECT_FALSE(HttpUtil::IsLWS('\n')); | 1339 EXPECT_FALSE(HttpUtil::IsLWS('\n')); |
| 1496 EXPECT_FALSE(HttpUtil::IsLWS('\r')); | 1340 EXPECT_FALSE(HttpUtil::IsLWS('\r')); |
| 1497 | 1341 |
| 1498 EXPECT_TRUE(HttpUtil::IsLWS('\t')); | 1342 EXPECT_TRUE(HttpUtil::IsLWS('\t')); |
| 1499 EXPECT_TRUE(HttpUtil::IsLWS(' ')); | 1343 EXPECT_TRUE(HttpUtil::IsLWS(' ')); |
| 1500 } | 1344 } |
| 1501 | 1345 |
| 1502 } // namespace net | 1346 } // namespace net |
| OLD | NEW |