blob: d5b39e9f3063508b604344d3180453e44b8efd3e [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
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 Stewart95133562012-01-18 18:36:57 -080016
17#include "shill/http_url.h"
18
19#include <gtest/gtest.h>
20
21using std::string;
22using testing::Test;
23
24namespace shill {
25
26struct StringAndResult {
Paul Stewart3b30ca52015-06-16 13:13:10 -070027 StringAndResult(const string& in_url_string,
Paul Stewart95133562012-01-18 18:36:57 -080028 bool in_result)
29 : url_string(in_url_string),
30 result(in_result) {}
Paul Stewart3b30ca52015-06-16 13:13:10 -070031 StringAndResult(const string& in_url_string,
Paul Stewart95133562012-01-18 18:36:57 -080032 bool in_result,
33 HTTPURL::Protocol in_protocol,
Paul Stewart3b30ca52015-06-16 13:13:10 -070034 const string& in_host,
Paul Stewart95133562012-01-18 18:36:57 -080035 int in_port,
Paul Stewart3b30ca52015-06-16 13:13:10 -070036 const string& in_path)
Paul Stewart95133562012-01-18 18:36:57 -080037 : 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
51class HTTPURLParseTest : public testing::TestWithParam<StringAndResult> {
52 protected:
53 HTTPURL url_;
54};
55
56TEST_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
67INSTANTIATE_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