Paul Stewart | 9513356 | 2012-01-18 18:36:57 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/http_url.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | using std::string; |
| 10 | using testing::Test; |
| 11 | |
| 12 | namespace shill { |
| 13 | |
| 14 | struct StringAndResult { |
| 15 | StringAndResult(const string &in_url_string, |
| 16 | bool in_result) |
| 17 | : url_string(in_url_string), |
| 18 | result(in_result) {} |
| 19 | StringAndResult(const string &in_url_string, |
| 20 | bool in_result, |
| 21 | HTTPURL::Protocol in_protocol, |
| 22 | const string &in_host, |
| 23 | int in_port, |
| 24 | const string &in_path) |
| 25 | : url_string(in_url_string), |
| 26 | result(in_result), |
| 27 | protocol(in_protocol), |
| 28 | host(in_host), |
| 29 | port(in_port), |
| 30 | path(in_path) {} |
| 31 | string url_string; |
| 32 | bool result; |
| 33 | HTTPURL::Protocol protocol; |
| 34 | string host; |
| 35 | int port; |
| 36 | string path; |
| 37 | }; |
| 38 | |
| 39 | class HTTPURLParseTest : public testing::TestWithParam<StringAndResult> { |
| 40 | protected: |
| 41 | HTTPURL url_; |
| 42 | }; |
| 43 | |
| 44 | TEST_P(HTTPURLParseTest, ParseURL) { |
| 45 | bool result = url_.ParseFromString(GetParam().url_string); |
| 46 | EXPECT_EQ(GetParam().result, result); |
| 47 | if (GetParam().result && result) { |
| 48 | EXPECT_EQ(GetParam().host, url_.host()); |
| 49 | EXPECT_EQ(GetParam().path, url_.path()); |
| 50 | EXPECT_EQ(GetParam().protocol, url_.protocol()); |
| 51 | EXPECT_EQ(GetParam().port, url_.port()); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | INSTANTIATE_TEST_CASE_P( |
| 56 | HTTPURLParseStringsTest, |
| 57 | HTTPURLParseTest, |
| 58 | ::testing::Values( |
| 59 | StringAndResult("", false), // Empty string |
| 60 | StringAndResult("xxx", false), // No known prefix |
| 61 | StringAndResult(" http://www.foo.com", false), // Leading garbage |
| 62 | StringAndResult("http://", false), // No hostname |
| 63 | StringAndResult("http://:100", false), // Port but no hostname |
| 64 | StringAndResult("http://www.foo.com:", false), // Colon but no port |
| 65 | StringAndResult("http://www.foo.com:x", false), // Non-numeric port |
| 66 | StringAndResult("http://foo.com:10:20", false), // Too many colons |
| 67 | StringAndResult("http://www.foo.com", true, |
| 68 | HTTPURL::kProtocolHTTP, |
| 69 | "www.foo.com", |
| 70 | HTTPURL::kDefaultHTTPPort, |
| 71 | "/"), |
| 72 | StringAndResult("https://www.foo.com", true, |
| 73 | HTTPURL::kProtocolHTTPS, |
| 74 | "www.foo.com", |
| 75 | HTTPURL::kDefaultHTTPSPort, |
| 76 | "/"), |
| 77 | StringAndResult("https://www.foo.com:4443", true, |
| 78 | HTTPURL::kProtocolHTTPS, |
| 79 | "www.foo.com", |
| 80 | 4443, |
| 81 | "/"), |
| 82 | StringAndResult("http://www.foo.com/bar", true, |
| 83 | HTTPURL::kProtocolHTTP, |
| 84 | "www.foo.com", |
| 85 | HTTPURL::kDefaultHTTPPort, |
| 86 | "/bar"), |
| 87 | StringAndResult("http://www.foo.com?bar", true, |
| 88 | HTTPURL::kProtocolHTTP, |
| 89 | "www.foo.com", |
| 90 | HTTPURL::kDefaultHTTPPort, |
| 91 | "/?bar"))); |
| 92 | |
| 93 | } // namespace shill |