Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Paul Stewart | 9513356 | 2012-01-18 18:36:57 -0800 | [diff] [blame] | 16 | |
| 17 | #include "shill/http_url.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | using std::string; |
| 22 | using testing::Test; |
| 23 | |
| 24 | namespace shill { |
| 25 | |
| 26 | struct StringAndResult { |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 27 | StringAndResult(const string& in_url_string, |
Paul Stewart | 9513356 | 2012-01-18 18:36:57 -0800 | [diff] [blame] | 28 | bool in_result) |
| 29 | : url_string(in_url_string), |
| 30 | result(in_result) {} |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 31 | StringAndResult(const string& in_url_string, |
Paul Stewart | 9513356 | 2012-01-18 18:36:57 -0800 | [diff] [blame] | 32 | bool in_result, |
| 33 | HTTPURL::Protocol in_protocol, |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 34 | const string& in_host, |
Paul Stewart | 9513356 | 2012-01-18 18:36:57 -0800 | [diff] [blame] | 35 | int in_port, |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 36 | const string& in_path) |
Paul Stewart | 9513356 | 2012-01-18 18:36:57 -0800 | [diff] [blame] | 37 | : url_string(in_url_string), |
| 38 | result(in_result), |
| 39 | protocol(in_protocol), |
| 40 | host(in_host), |
| 41 | port(in_port), |
| 42 | path(in_path) {} |
| 43 | string url_string; |
| 44 | bool result; |
| 45 | HTTPURL::Protocol protocol; |
| 46 | string host; |
| 47 | int port; |
| 48 | string path; |
| 49 | }; |
| 50 | |
| 51 | class HTTPURLParseTest : public testing::TestWithParam<StringAndResult> { |
| 52 | protected: |
| 53 | HTTPURL url_; |
| 54 | }; |
| 55 | |
| 56 | TEST_P(HTTPURLParseTest, ParseURL) { |
| 57 | bool result = url_.ParseFromString(GetParam().url_string); |
| 58 | EXPECT_EQ(GetParam().result, result); |
| 59 | if (GetParam().result && result) { |
| 60 | EXPECT_EQ(GetParam().host, url_.host()); |
| 61 | EXPECT_EQ(GetParam().path, url_.path()); |
| 62 | EXPECT_EQ(GetParam().protocol, url_.protocol()); |
| 63 | EXPECT_EQ(GetParam().port, url_.port()); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | INSTANTIATE_TEST_CASE_P( |
| 68 | HTTPURLParseStringsTest, |
| 69 | HTTPURLParseTest, |
| 70 | ::testing::Values( |
| 71 | StringAndResult("", false), // Empty string |
| 72 | StringAndResult("xxx", false), // No known prefix |
| 73 | StringAndResult(" http://www.foo.com", false), // Leading garbage |
| 74 | StringAndResult("http://", false), // No hostname |
| 75 | StringAndResult("http://:100", false), // Port but no hostname |
| 76 | StringAndResult("http://www.foo.com:", false), // Colon but no port |
| 77 | StringAndResult("http://www.foo.com:x", false), // Non-numeric port |
| 78 | StringAndResult("http://foo.com:10:20", false), // Too many colons |
| 79 | StringAndResult("http://www.foo.com", true, |
| 80 | HTTPURL::kProtocolHTTP, |
| 81 | "www.foo.com", |
| 82 | HTTPURL::kDefaultHTTPPort, |
| 83 | "/"), |
| 84 | StringAndResult("https://www.foo.com", true, |
| 85 | HTTPURL::kProtocolHTTPS, |
| 86 | "www.foo.com", |
| 87 | HTTPURL::kDefaultHTTPSPort, |
| 88 | "/"), |
| 89 | StringAndResult("https://www.foo.com:4443", true, |
| 90 | HTTPURL::kProtocolHTTPS, |
| 91 | "www.foo.com", |
| 92 | 4443, |
| 93 | "/"), |
| 94 | StringAndResult("http://www.foo.com/bar", true, |
| 95 | HTTPURL::kProtocolHTTP, |
| 96 | "www.foo.com", |
| 97 | HTTPURL::kDefaultHTTPPort, |
| 98 | "/bar"), |
| 99 | StringAndResult("http://www.foo.com?bar", true, |
| 100 | HTTPURL::kProtocolHTTP, |
| 101 | "www.foo.com", |
| 102 | HTTPURL::kDefaultHTTPPort, |
| 103 | "/?bar"))); |
| 104 | |
| 105 | } // namespace shill |