| 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/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "net/http/http_util.h" | 8 #include "net/http/http_util.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 {"foo\nbar\n\r\njunk", 10}, | 371 {"foo\nbar\n\r\njunk", 10}, |
| 372 {"foo\nbar\r\n\njunk", 10}, | 372 {"foo\nbar\r\n\njunk", 10}, |
| 373 }; | 373 }; |
| 374 for (size_t i = 0; i < arraysize(tests); ++i) { | 374 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 375 int input_len = static_cast<int>(strlen(tests[i].input)); | 375 int input_len = static_cast<int>(strlen(tests[i].input)); |
| 376 int eoh = HttpUtil::LocateEndOfAdditionalHeaders(tests[i].input, input_len); | 376 int eoh = HttpUtil::LocateEndOfAdditionalHeaders(tests[i].input, input_len); |
| 377 EXPECT_EQ(tests[i].expected_result, eoh); | 377 EXPECT_EQ(tests[i].expected_result, eoh); |
| 378 } | 378 } |
| 379 } | 379 } |
| 380 TEST(HttpUtilTest, AssembleRawHeaders) { | 380 TEST(HttpUtilTest, AssembleRawHeaders) { |
| 381 // clang-format off |
| 381 struct { | 382 struct { |
| 382 const char* const input; // with '|' representing '\0' | 383 const char* const input; // with '|' representing '\0' |
| 383 const char* const expected_result; // with '\0' changed to '|' | 384 const char* const expected_result; // with '\0' changed to '|' |
| 384 } tests[] = { | 385 } tests[] = { |
| 385 { "HTTP/1.0 200 OK\r\nFoo: 1\r\nBar: 2\r\n\r\n", | 386 { "HTTP/1.0 200 OK\r\nFoo: 1\r\nBar: 2\r\n\r\n", |
| 386 "HTTP/1.0 200 OK|Foo: 1|Bar: 2||" }, | 387 "HTTP/1.0 200 OK|Foo: 1|Bar: 2||" }, |
| 387 | 388 |
| 388 { "HTTP/1.0 200 OK\nFoo: 1\nBar: 2\n\n", | 389 { "HTTP/1.0 200 OK\nFoo: 1\nBar: 2\n\n", |
| 389 "HTTP/1.0 200 OK|Foo: 1|Bar: 2||" }, | 390 "HTTP/1.0 200 OK|Foo: 1|Bar: 2||" }, |
| 390 | 391 |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 "HTTP/1.0 200 OK|Bar2:0|Baz2:1\r\nFoo: 1\r\nBar: 2\r\n\r\n", | 673 "HTTP/1.0 200 OK|Bar2:0|Baz2:1\r\nFoo: 1\r\nBar: 2\r\n\r\n", |
| 673 "HTTP/1.0 200 OKBar2:0Baz2:1|Foo: 1|Bar: 2||" | 674 "HTTP/1.0 200 OKBar2:0Baz2:1|Foo: 1|Bar: 2||" |
| 674 }, | 675 }, |
| 675 | 676 |
| 676 // Embed NULLs in a header line. They should not be understood as | 677 // Embed NULLs in a header line. They should not be understood as |
| 677 // line separators. | 678 // line separators. |
| 678 { | 679 { |
| 679 "HTTP/1.0 200 OK\nFoo: 1|Foo2: 3\nBar: 2\n\n", | 680 "HTTP/1.0 200 OK\nFoo: 1|Foo2: 3\nBar: 2\n\n", |
| 680 "HTTP/1.0 200 OK|Foo: 1Foo2: 3|Bar: 2||" | 681 "HTTP/1.0 200 OK|Foo: 1Foo2: 3|Bar: 2||" |
| 681 }, | 682 }, |
| 683 |
| 684 // The embedded NUL at the start of the line (before "Blah:") should not be |
| 685 // interpreted as LWS (as that would mistake it for a header line |
| 686 // continuation). |
| 687 { |
| 688 "HTTP/1.0 200 OK\n" |
| 689 "Foo: 1\n" |
| 690 "|Blah: 3\n" |
| 691 "Bar: 2\n\n", |
| 692 "HTTP/1.0 200 OK|Foo: 1|Blah: 3|Bar: 2||" |
| 693 }, |
| 682 }; | 694 }; |
| 695 // clang-format on |
| 683 for (size_t i = 0; i < arraysize(tests); ++i) { | 696 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 684 std::string input = tests[i].input; | 697 std::string input = tests[i].input; |
| 685 std::replace(input.begin(), input.end(), '|', '\0'); | 698 std::replace(input.begin(), input.end(), '|', '\0'); |
| 686 std::string raw = HttpUtil::AssembleRawHeaders(input.data(), input.size()); | 699 std::string raw = HttpUtil::AssembleRawHeaders(input.data(), input.size()); |
| 687 std::replace(raw.begin(), raw.end(), '\0', '|'); | 700 std::replace(raw.begin(), raw.end(), '\0', '|'); |
| 688 EXPECT_EQ(tests[i].expected_result, raw); | 701 EXPECT_EQ(tests[i].expected_result, raw); |
| 689 } | 702 } |
| 690 } | 703 } |
| 691 | 704 |
| 692 // Test SpecForRequest(). | 705 // Test SpecForRequest(). |
| (...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1401 EXPECT_FALSE(HttpUtil::IsToken(base::StringPiece())); | 1414 EXPECT_FALSE(HttpUtil::IsToken(base::StringPiece())); |
| 1402 EXPECT_FALSE(HttpUtil::IsToken("hello, world")); | 1415 EXPECT_FALSE(HttpUtil::IsToken("hello, world")); |
| 1403 EXPECT_FALSE(HttpUtil::IsToken(" ")); | 1416 EXPECT_FALSE(HttpUtil::IsToken(" ")); |
| 1404 EXPECT_FALSE(HttpUtil::IsToken(base::StringPiece("\0", 1))); | 1417 EXPECT_FALSE(HttpUtil::IsToken(base::StringPiece("\0", 1))); |
| 1405 EXPECT_FALSE(HttpUtil::IsToken("\x01")); | 1418 EXPECT_FALSE(HttpUtil::IsToken("\x01")); |
| 1406 EXPECT_FALSE(HttpUtil::IsToken("\x7F")); | 1419 EXPECT_FALSE(HttpUtil::IsToken("\x7F")); |
| 1407 EXPECT_FALSE(HttpUtil::IsToken("\x80")); | 1420 EXPECT_FALSE(HttpUtil::IsToken("\x80")); |
| 1408 EXPECT_FALSE(HttpUtil::IsToken("\xff")); | 1421 EXPECT_FALSE(HttpUtil::IsToken("\xff")); |
| 1409 } | 1422 } |
| 1410 | 1423 |
| 1424 TEST(HttpUtilTest, IsLWS) { |
| 1425 EXPECT_FALSE(HttpUtil::IsLWS('\v')); |
| 1426 EXPECT_FALSE(HttpUtil::IsLWS('\0')); |
| 1427 EXPECT_FALSE(HttpUtil::IsLWS('1')); |
| 1428 EXPECT_FALSE(HttpUtil::IsLWS('a')); |
| 1429 EXPECT_FALSE(HttpUtil::IsLWS('.')); |
| 1430 EXPECT_FALSE(HttpUtil::IsLWS('\n')); |
| 1431 EXPECT_FALSE(HttpUtil::IsLWS('\r')); |
| 1432 |
| 1433 EXPECT_TRUE(HttpUtil::IsLWS('\t')); |
| 1434 EXPECT_TRUE(HttpUtil::IsLWS(' ')); |
| 1435 } |
| 1436 |
| 1411 } // namespace net | 1437 } // namespace net |
| OLD | NEW |