| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "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 10 matching lines...) Expand all Loading... |
| 21 bool expected_result; | 21 bool expected_result; |
| 22 } tests[] = { | 22 } tests[] = { |
| 23 { "", "foo", false }, | 23 { "", "foo", false }, |
| 24 { "foo\r\nbar", "foo", false }, | 24 { "foo\r\nbar", "foo", false }, |
| 25 { "ffoo: 1", "foo", false }, | 25 { "ffoo: 1", "foo", false }, |
| 26 { "foo: 1", "foo", true }, | 26 { "foo: 1", "foo", true }, |
| 27 { "foo: 1\r\nbar: 2", "foo", true }, | 27 { "foo: 1\r\nbar: 2", "foo", true }, |
| 28 { "fOO: 1\r\nbar: 2", "foo", true }, | 28 { "fOO: 1\r\nbar: 2", "foo", true }, |
| 29 { "g: 0\r\nfoo: 1\r\nbar: 2", "foo", true }, | 29 { "g: 0\r\nfoo: 1\r\nbar: 2", "foo", true }, |
| 30 }; | 30 }; |
| 31 for (size_t i = 0; i < arraysize(tests); ++i) { | 31 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 32 bool result = HttpUtil::HasHeader(tests[i].headers, tests[i].name); | 32 bool result = HttpUtil::HasHeader(tests[i].headers, tests[i].name); |
| 33 EXPECT_EQ(tests[i].expected_result, result); | 33 EXPECT_EQ(tests[i].expected_result, result); |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 TEST(HttpUtilTest, HeadersIterator) { | 37 TEST(HttpUtilTest, HeadersIterator) { |
| 38 std::string headers = "foo: 1\t\r\nbar: hello world\r\nbaz: 3 \r\n"; | 38 std::string headers = "foo: 1\t\r\nbar: hello world\r\nbaz: 3 \r\n"; |
| 39 | 39 |
| 40 HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\r\n"); | 40 HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\r\n"); |
| 41 | 41 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 const char* input; | 100 const char* input; |
| 101 int expected_result; | 101 int expected_result; |
| 102 } tests[] = { | 102 } tests[] = { |
| 103 { "foo\r\nbar\r\n\r\n", 12 }, | 103 { "foo\r\nbar\r\n\r\n", 12 }, |
| 104 { "foo\nbar\n\n", 9 }, | 104 { "foo\nbar\n\n", 9 }, |
| 105 { "foo\r\nbar\r\n\r\njunk", 12 }, | 105 { "foo\r\nbar\r\n\r\njunk", 12 }, |
| 106 { "foo\nbar\n\njunk", 9 }, | 106 { "foo\nbar\n\njunk", 9 }, |
| 107 { "foo\nbar\n\r\njunk", 10 }, | 107 { "foo\nbar\n\r\njunk", 10 }, |
| 108 { "foo\nbar\r\n\njunk", 10 }, | 108 { "foo\nbar\r\n\njunk", 10 }, |
| 109 }; | 109 }; |
| 110 for (size_t i = 0; i < arraysize(tests); ++i) { | 110 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 111 int input_len = static_cast<int>(strlen(tests[i].input)); | 111 int input_len = static_cast<int>(strlen(tests[i].input)); |
| 112 int eoh = HttpUtil::LocateEndOfHeaders(tests[i].input, input_len); | 112 int eoh = HttpUtil::LocateEndOfHeaders(tests[i].input, input_len); |
| 113 EXPECT_EQ(tests[i].expected_result, eoh); | 113 EXPECT_EQ(tests[i].expected_result, eoh); |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 | 116 |
| 117 TEST(HttpUtilTest, AssembleRawHeaders) { | 117 TEST(HttpUtilTest, AssembleRawHeaders) { |
| 118 struct { | 118 struct { |
| 119 const char* input; | 119 const char* input; |
| 120 const char* expected_result; // with '\0' changed to '|' | 120 const char* expected_result; // with '\0' changed to '|' |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 " garbage\n" | 397 " garbage\n" |
| 398 "Bar: 2\n\n", | 398 "Bar: 2\n\n", |
| 399 | 399 |
| 400 "HTTP/1.0 200 OK|" | 400 "HTTP/1.0 200 OK|" |
| 401 " : 1|" | 401 " : 1|" |
| 402 " garbage|" | 402 " garbage|" |
| 403 "Bar: 2||", | 403 "Bar: 2||", |
| 404 }, | 404 }, |
| 405 | 405 |
| 406 }; | 406 }; |
| 407 for (size_t i = 0; i < arraysize(tests); ++i) { | 407 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 408 int input_len = static_cast<int>(strlen(tests[i].input)); | 408 int input_len = static_cast<int>(strlen(tests[i].input)); |
| 409 std::string raw = HttpUtil::AssembleRawHeaders(tests[i].input, input_len); | 409 std::string raw = HttpUtil::AssembleRawHeaders(tests[i].input, input_len); |
| 410 std::replace(raw.begin(), raw.end(), '\0', '|'); | 410 std::replace(raw.begin(), raw.end(), '\0', '|'); |
| 411 EXPECT_TRUE(raw == tests[i].expected_result); | 411 EXPECT_TRUE(raw == tests[i].expected_result); |
| 412 } | 412 } |
| 413 } | 413 } |
| 414 | 414 |
| 415 // Test SpecForRequest() and PathForRequest(). | 415 // Test SpecForRequest() and PathForRequest(). |
| 416 TEST(HttpUtilTest, RequestUrlSanitize) { | 416 TEST(HttpUtilTest, RequestUrlSanitize) { |
| 417 struct { | 417 struct { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 428 "http://192.168.0.1?query=1#hash#10#11#13#14", | 428 "http://192.168.0.1?query=1#hash#10#11#13#14", |
| 429 "http://192.168.0.1/?query=1", | 429 "http://192.168.0.1/?query=1", |
| 430 "/?query=1" | 430 "/?query=1" |
| 431 }, | 431 }, |
| 432 { // Strip username/password. | 432 { // Strip username/password. |
| 433 "http://user:pass@google.com", | 433 "http://user:pass@google.com", |
| 434 "http://google.com/", | 434 "http://google.com/", |
| 435 "/" | 435 "/" |
| 436 } | 436 } |
| 437 }; | 437 }; |
| 438 for (size_t i = 0; i < arraysize(tests); ++i) { | 438 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 439 GURL url(GURL(tests[i].url)); | 439 GURL url(GURL(tests[i].url)); |
| 440 std::string expected_spec(tests[i].expected_spec); | 440 std::string expected_spec(tests[i].expected_spec); |
| 441 std::string expected_path(tests[i].expected_path); | 441 std::string expected_path(tests[i].expected_path); |
| 442 | 442 |
| 443 EXPECT_EQ(expected_spec, HttpUtil::SpecForRequest(url)); | 443 EXPECT_EQ(expected_spec, HttpUtil::SpecForRequest(url)); |
| 444 EXPECT_EQ(expected_path, HttpUtil::PathForRequest(url)); | 444 EXPECT_EQ(expected_path, HttpUtil::PathForRequest(url)); |
| 445 } | 445 } |
| 446 } | 446 } |
| OLD | NEW |