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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 bool expected_result; | 107 bool expected_result; |
108 } tests[] = { | 108 } tests[] = { |
109 { "", "foo", false }, | 109 { "", "foo", false }, |
110 { "foo\r\nbar", "foo", false }, | 110 { "foo\r\nbar", "foo", false }, |
111 { "ffoo: 1", "foo", false }, | 111 { "ffoo: 1", "foo", false }, |
112 { "foo: 1", "foo", true }, | 112 { "foo: 1", "foo", true }, |
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_UNSAFE(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) { | 123 TEST(HttpUtilTest, StripHeaders) { |
124 static const char* headers = | 124 static const char* headers = |
125 "Origin: origin\r\n" | 125 "Origin: origin\r\n" |
126 "Content-Type: text/plain\r\n" | 126 "Content-Type: text/plain\r\n" |
127 "Cookies: foo1\r\n" | 127 "Cookies: foo1\r\n" |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 const char* input; | 265 const char* input; |
266 int expected_result; | 266 int expected_result; |
267 } tests[] = { | 267 } tests[] = { |
268 { "foo\r\nbar\r\n\r\n", 12 }, | 268 { "foo\r\nbar\r\n\r\n", 12 }, |
269 { "foo\nbar\n\n", 9 }, | 269 { "foo\nbar\n\n", 9 }, |
270 { "foo\r\nbar\r\n\r\njunk", 12 }, | 270 { "foo\r\nbar\r\n\r\njunk", 12 }, |
271 { "foo\nbar\n\njunk", 9 }, | 271 { "foo\nbar\n\njunk", 9 }, |
272 { "foo\nbar\n\r\njunk", 10 }, | 272 { "foo\nbar\n\r\njunk", 10 }, |
273 { "foo\nbar\r\n\njunk", 10 }, | 273 { "foo\nbar\r\n\njunk", 10 }, |
274 }; | 274 }; |
275 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 275 for (size_t i = 0; i < arraysize(tests); ++i) { |
276 int input_len = static_cast<int>(strlen(tests[i].input)); | 276 int input_len = static_cast<int>(strlen(tests[i].input)); |
277 int eoh = HttpUtil::LocateEndOfHeaders(tests[i].input, input_len); | 277 int eoh = HttpUtil::LocateEndOfHeaders(tests[i].input, input_len); |
278 EXPECT_EQ(tests[i].expected_result, eoh); | 278 EXPECT_EQ(tests[i].expected_result, eoh); |
279 } | 279 } |
280 } | 280 } |
281 | 281 |
282 TEST(HttpUtilTest, AssembleRawHeaders) { | 282 TEST(HttpUtilTest, AssembleRawHeaders) { |
283 struct { | 283 struct { |
284 const char* input; // with '|' representing '\0' | 284 const char* input; // with '|' representing '\0' |
285 const char* expected_result; // with '\0' changed to '|' | 285 const char* expected_result; // with '\0' changed to '|' |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 "HTTP/1.0 200 OKBar2:0Baz2:1|Foo: 1|Bar: 2||" | 575 "HTTP/1.0 200 OKBar2:0Baz2:1|Foo: 1|Bar: 2||" |
576 }, | 576 }, |
577 | 577 |
578 // Embed NULLs in a header line. They should not be understood as | 578 // Embed NULLs in a header line. They should not be understood as |
579 // line separators. | 579 // line separators. |
580 { | 580 { |
581 "HTTP/1.0 200 OK\nFoo: 1|Foo2: 3\nBar: 2\n\n", | 581 "HTTP/1.0 200 OK\nFoo: 1|Foo2: 3\nBar: 2\n\n", |
582 "HTTP/1.0 200 OK|Foo: 1Foo2: 3|Bar: 2||" | 582 "HTTP/1.0 200 OK|Foo: 1Foo2: 3|Bar: 2||" |
583 }, | 583 }, |
584 }; | 584 }; |
585 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 585 for (size_t i = 0; i < arraysize(tests); ++i) { |
586 std::string input = tests[i].input; | 586 std::string input = tests[i].input; |
587 std::replace(input.begin(), input.end(), '|', '\0'); | 587 std::replace(input.begin(), input.end(), '|', '\0'); |
588 std::string raw = HttpUtil::AssembleRawHeaders(input.data(), input.size()); | 588 std::string raw = HttpUtil::AssembleRawHeaders(input.data(), input.size()); |
589 std::replace(raw.begin(), raw.end(), '\0', '|'); | 589 std::replace(raw.begin(), raw.end(), '\0', '|'); |
590 EXPECT_EQ(tests[i].expected_result, raw); | 590 EXPECT_EQ(tests[i].expected_result, raw); |
591 } | 591 } |
592 } | 592 } |
593 | 593 |
594 // Test SpecForRequest() and PathForRequest(). | 594 // Test SpecForRequest() and PathForRequest(). |
595 TEST(HttpUtilTest, RequestUrlSanitize) { | 595 TEST(HttpUtilTest, RequestUrlSanitize) { |
(...skipping 26 matching lines...) Expand all Loading... |
622 "ws://www.google.com:78/foobar?query=1#hash", | 622 "ws://www.google.com:78/foobar?query=1#hash", |
623 "ws://www.google.com:78/foobar?query=1", | 623 "ws://www.google.com:78/foobar?query=1", |
624 "/foobar?query=1" | 624 "/foobar?query=1" |
625 }, | 625 }, |
626 { // WebSocket's wss scheme | 626 { // WebSocket's wss scheme |
627 "wss://www.google.com:78/foobar?query=1#hash", | 627 "wss://www.google.com:78/foobar?query=1#hash", |
628 "wss://www.google.com:78/foobar?query=1", | 628 "wss://www.google.com:78/foobar?query=1", |
629 "/foobar?query=1" | 629 "/foobar?query=1" |
630 } | 630 } |
631 }; | 631 }; |
632 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 632 for (size_t i = 0; i < arraysize(tests); ++i) { |
633 GURL url(GURL(tests[i].url)); | 633 GURL url(GURL(tests[i].url)); |
634 std::string expected_spec(tests[i].expected_spec); | 634 std::string expected_spec(tests[i].expected_spec); |
635 std::string expected_path(tests[i].expected_path); | 635 std::string expected_path(tests[i].expected_path); |
636 | 636 |
637 EXPECT_EQ(expected_spec, HttpUtil::SpecForRequest(url)); | 637 EXPECT_EQ(expected_spec, HttpUtil::SpecForRequest(url)); |
638 EXPECT_EQ(expected_path, HttpUtil::PathForRequest(url)); | 638 EXPECT_EQ(expected_path, HttpUtil::PathForRequest(url)); |
639 } | 639 } |
640 } | 640 } |
641 | 641 |
642 // Test SpecForRequest() for "ftp" scheme. | 642 // Test SpecForRequest() for "ftp" scheme. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 "\"WebKit-ada-df-dsf-adsfadsfs \"" | 718 "\"WebKit-ada-df-dsf-adsfadsfs \"" |
719 }, | 719 }, |
720 { "text/html; boundary=WebKit-ada-df-dsf-adsfadsfs", | 720 { "text/html; boundary=WebKit-ada-df-dsf-adsfadsfs", |
721 "text/html", | 721 "text/html", |
722 "", | 722 "", |
723 false, | 723 false, |
724 "WebKit-ada-df-dsf-adsfadsfs" | 724 "WebKit-ada-df-dsf-adsfadsfs" |
725 }, | 725 }, |
726 // TODO(abarth): Add more interesting test cases. | 726 // TODO(abarth): Add more interesting test cases. |
727 }; | 727 }; |
728 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 728 for (size_t i = 0; i < arraysize(tests); ++i) { |
729 std::string mime_type; | 729 std::string mime_type; |
730 std::string charset; | 730 std::string charset; |
731 bool had_charset = false; | 731 bool had_charset = false; |
732 std::string boundary; | 732 std::string boundary; |
733 net::HttpUtil::ParseContentType(tests[i].content_type, &mime_type, | 733 net::HttpUtil::ParseContentType(tests[i].content_type, &mime_type, |
734 &charset, &had_charset, &boundary); | 734 &charset, &had_charset, &boundary); |
735 EXPECT_EQ(tests[i].expected_mime_type, mime_type) << "i=" << i; | 735 EXPECT_EQ(tests[i].expected_mime_type, mime_type) << "i=" << i; |
736 EXPECT_EQ(tests[i].expected_charset, charset) << "i=" << i; | 736 EXPECT_EQ(tests[i].expected_charset, charset) << "i=" << i; |
737 EXPECT_EQ(tests[i].expected_had_charset, had_charset) << "i=" << i; | 737 EXPECT_EQ(tests[i].expected_had_charset, had_charset) << "i=" << i; |
738 EXPECT_EQ(tests[i].expected_boundary, boundary) << "i=" << i; | 738 EXPECT_EQ(tests[i].expected_boundary, boundary) << "i=" << i; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 5, | 846 5, |
847 { {0, 1, -1}, | 847 { {0, 1, -1}, |
848 {10, 20, -1}, | 848 {10, 20, -1}, |
849 {100, 200, -1}, | 849 {100, 200, -1}, |
850 {100, -1, -1}, | 850 {100, -1, -1}, |
851 {-1, -1, 200}, | 851 {-1, -1, 200}, |
852 } | 852 } |
853 }, | 853 }, |
854 }; | 854 }; |
855 | 855 |
856 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 856 for (size_t i = 0; i < arraysize(tests); ++i) { |
857 std::vector<net::HttpByteRange> ranges; | 857 std::vector<net::HttpByteRange> ranges; |
858 bool return_value = HttpUtil::ParseRanges(std::string(tests[i].headers), | 858 bool return_value = HttpUtil::ParseRanges(std::string(tests[i].headers), |
859 &ranges); | 859 &ranges); |
860 EXPECT_EQ(tests[i].expected_return_value, return_value); | 860 EXPECT_EQ(tests[i].expected_return_value, return_value); |
861 if (return_value) { | 861 if (return_value) { |
862 EXPECT_EQ(tests[i].expected_ranges_size, ranges.size()); | 862 EXPECT_EQ(tests[i].expected_ranges_size, ranges.size()); |
863 for (size_t j = 0; j < ranges.size(); ++j) { | 863 for (size_t j = 0; j < ranges.size(); ++j) { |
864 EXPECT_EQ(tests[i].expected_ranges[j].expected_first_byte_position, | 864 EXPECT_EQ(tests[i].expected_ranges[j].expected_first_byte_position, |
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, |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1064 TEST(HttpUtilTest, NameValuePairsIteratorMissingEndQuote) { | 1064 TEST(HttpUtilTest, NameValuePairsIteratorMissingEndQuote) { |
1065 std::string data = "name='value"; | 1065 std::string data = "name='value"; |
1066 HttpUtil::NameValuePairsIterator parser(data.begin(), data.end(), ';'); | 1066 HttpUtil::NameValuePairsIterator parser(data.begin(), data.end(), ';'); |
1067 EXPECT_TRUE(parser.valid()); | 1067 EXPECT_TRUE(parser.valid()); |
1068 | 1068 |
1069 ASSERT_NO_FATAL_FAILURE( | 1069 ASSERT_NO_FATAL_FAILURE( |
1070 CheckNextNameValuePair(&parser, true, true, "name", "value")); | 1070 CheckNextNameValuePair(&parser, true, true, "name", "value")); |
1071 ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair( | 1071 ASSERT_NO_FATAL_FAILURE(CheckNextNameValuePair( |
1072 &parser, false, true, std::string(), std::string())); | 1072 &parser, false, true, std::string(), std::string())); |
1073 } | 1073 } |
OLD | NEW |