|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 | 4 | #include <gtest/gtest.h>
|
5 |
| -#include <map> |
6 | 5 | #include <string>
|
7 |
| -#include <utility> |
| 6 | +#include <tuple> |
8 | 7 |
|
9 | 8 | #include "opentelemetry/ext/http/common/url_parser.h"
|
10 | 9 |
|
11 | 10 | namespace http_common = opentelemetry::ext::http::common;
|
12 | 11 |
|
13 |
| -inline const char *BoolToString(bool b) |
| 12 | +struct ParsedUrl |
14 | 13 | {
|
15 |
| - return b ? "true" : "false"; |
16 |
| -} |
| 14 | + std::string scheme; |
| 15 | + std::string host; |
| 16 | + std::uint16_t port; |
| 17 | + std::string path; |
| 18 | + std::string query; |
| 19 | + bool success; |
17 | 20 |
|
18 |
| -TEST(UrlParserTests, BasicTests) |
19 |
| -{ |
20 |
| - std::map<std::string, std::map<std::string, std::string>> urls_map{ |
21 |
| - {"www.abc.com", |
22 |
| - {{"host", "www.abc.com"}, |
23 |
| - {"port", "80"}, |
24 |
| - {"scheme", "http"}, |
25 |
| - {"path", "/"}, |
26 |
| - {"query", ""}, |
27 |
| - {"success", "true"}}}, |
28 |
| - {"http://www.abc.com", |
29 |
| - {{"host", "www.abc.com"}, |
30 |
| - {"port", "80"}, |
31 |
| - {"scheme", "http"}, |
32 |
| - {"path", "/"}, |
33 |
| - {"query", ""}, |
34 |
| - {"success", "true"}}}, |
35 |
| - {"https://www.abc.com", |
36 |
| - {{"host", "www.abc.com"}, |
37 |
| - {"port", "443"}, |
38 |
| - {"scheme", "https"}, |
39 |
| - {"path", "/"}, |
40 |
| - {"query", ""}, |
41 |
| - {"success", "true"}}}, |
42 |
| - {"https://www.abc.com:4431", |
43 |
| - {{"host", "www.abc.com"}, |
44 |
| - {"port", "4431"}, |
45 |
| - {"scheme", "https"}, |
46 |
| - {"path", "/"}, |
47 |
| - {"query", ""}, |
48 |
| - {"success", "true"}}}, |
49 |
| - {"https://www.abc.com:4431", |
50 |
| - {{"host", "www.abc.com"}, |
51 |
| - {"port", "4431"}, |
52 |
| - {"scheme", "https"}, |
53 |
| - {"path", "/"}, |
54 |
| - {"query", ""}, |
55 |
| - {"success", "true"}}}, |
56 |
| - {"https://www.abc.com:4431/path1", |
57 |
| - {{"host", "www.abc.com"}, |
58 |
| - {"port", "4431"}, |
59 |
| - {"scheme", "https"}, |
60 |
| - {"path", "/path1"}, |
61 |
| - {"query", ""}, |
62 |
| - {"success", "true"}}}, |
63 |
| - {"https://www.abc.com:4431/path1/path2", |
64 |
| - {{"host", "www.abc.com"}, |
65 |
| - {"port", "4431"}, |
66 |
| - {"scheme", "https"}, |
67 |
| - {"path", "/path1/path2"}, |
68 |
| - {"query", ""}, |
69 |
| - {"success", "true"}}}, |
70 |
| - {"https://www.abc.com/path1/path2", |
71 |
| - {{"host", "www.abc.com"}, |
72 |
| - {"port", "443"}, |
73 |
| - {"scheme", "https"}, |
74 |
| - {"path", "/path1/path2"}, |
75 |
| - {"query", ""}, |
76 |
| - {"success", "true"}}}, |
77 |
| - {"http://www.abc.com/path1/path2?q1=a1&q2=a2", |
78 |
| - {{"host", "www.abc.com"}, |
79 |
| - {"port", "80"}, |
80 |
| - {"scheme", "http"}, |
81 |
| - {"path", "/path1/path2"}, |
82 |
| - {"query", "q1=a1&q2=a2"}, |
83 |
| - {"success", "true"}}}, |
84 |
| - {"http://www.abc.com:8080/path1/path2?q1=a1&q2=a2", |
85 |
| - {{"host", "www.abc.com"}, |
86 |
| - {"port", "8080"}, |
87 |
| - {"scheme", "http"}, |
88 |
| - {"path", "/path1/path2"}, |
89 |
| - {"query", "q1=a1&q2=a2"}, |
90 |
| - {"success", "true"}}}, |
91 |
| - {"www.abc.com:8080/path1/path2?q1=a1&q2=a2", |
92 |
| - {{"host", "www.abc.com"}, |
93 |
| - {"port", "8080"}, |
94 |
| - {"scheme", "http"}, |
95 |
| - {"path", "/path1/path2"}, |
96 |
| - {"query", "q1=a1&q2=a2"}, |
97 |
| - {"success", "true"}}}, |
98 |
| - { "http://user:[email protected]:8080/path1/path2?q1=a1&q2=a2", |
99 |
| - {{"host", "www.abc.com"}, |
100 |
| - {"port", "8080"}, |
101 |
| - {"scheme", "http"}, |
102 |
| - {"path", "/path1/path2"}, |
103 |
| - {"query", "q1=a1&q2=a2"}, |
104 |
| - {"success", "true"}}}, |
105 |
| - { "user:[email protected]:8080/path1/path2?q1=a1&q2=a2", |
106 |
| - {{"host", "www.abc.com"}, |
107 |
| - {"port", "8080"}, |
108 |
| - {"scheme", "http"}, |
109 |
| - {"path", "/path1/path2"}, |
110 |
| - {"query", "q1=a1&q2=a2"}, |
111 |
| - {"success", "true"}}}, |
112 |
| - { "https://[email protected]/path1/path2?q1=a1&q2=a2", |
113 |
| - {{"host", "www.abc.com"}, |
114 |
| - {"port", "443"}, |
115 |
| - {"scheme", "https"}, |
116 |
| - {"path", "/path1/path2"}, |
117 |
| - {"query", "q1=a1&q2=a2"}, |
118 |
| - {"success", "true"}}}, |
119 |
| - {"http://www.abc.com/path1@bbb/path2?q1=a1&q2=a2", |
120 |
| - {{"host", "www.abc.com"}, |
121 |
| - {"port", "80"}, |
122 |
| - {"scheme", "http"}, |
123 |
| - {"path", "/path1@bbb/path2"}, |
124 |
| - {"query", "q1=a1&q2=a2"}, |
125 |
| - {"success", "true"}}}, |
126 |
| - {"http://1.2.3.4/path1/path2?q1=a1&q2=a2", |
127 |
| - {{"host", "1.2.3.4"}, |
128 |
| - {"port", "80"}, |
129 |
| - {"scheme", "http"}, |
130 |
| - {"path", "/path1/path2"}, |
131 |
| - {"query", "q1=a1&q2=a2"}, |
132 |
| - {"success", "true"}}}, |
133 |
| - { "user:[email protected]:8080/path1/path2?q1=a1&q2=a2", |
134 |
| - {{"host", "1.2.3.4"}, |
135 |
| - {"port", "8080"}, |
136 |
| - {"scheme", "http"}, |
137 |
| - {"path", "/path1/path2"}, |
138 |
| - {"query", "q1=a1&q2=a2"}, |
139 |
| - {"success", "true"}}}, |
140 |
| - { "https://[email protected]/path1/path2?q1=a1&q2=a2", |
141 |
| - {{"host", "1.2.3.4"}, |
142 |
| - {"port", "443"}, |
143 |
| - {"scheme", "https"}, |
144 |
| - {"path", "/path1/path2"}, |
145 |
| - {"query", "q1=a1&q2=a2"}, |
146 |
| - {"success", "true"}}}, |
147 |
| - {"http://1.2.3.4/path1@bbb/path2?q1=a1&q2=a2", |
148 |
| - {{"host", "1.2.3.4"}, |
149 |
| - {"port", "80"}, |
150 |
| - {"scheme", "http"}, |
151 |
| - {"path", "/path1@bbb/path2"}, |
152 |
| - {"query", "q1=a1&q2=a2"}, |
153 |
| - {"success", "true"}}}, |
154 |
| - {"http://[fe80::225:93da:bfde:b5f5]/path1/path2?q1=a1&q2=a2", |
155 |
| - {{"host", "[fe80::225:93da:bfde:b5f5]"}, |
156 |
| - {"port", "80"}, |
157 |
| - {"scheme", "http"}, |
158 |
| - {"path", "/path1/path2"}, |
159 |
| - {"query", "q1=a1&q2=a2"}, |
160 |
| - {"success", "true"}}}, |
161 |
| - {"user:password@[fe80::225:93da:bfde:b5f5]:8080/path1/path2?q1=a1&q2=a2", |
162 |
| - {{"host", "[fe80::225:93da:bfde:b5f5]"}, |
163 |
| - {"port", "8080"}, |
164 |
| - {"scheme", "http"}, |
165 |
| - {"path", "/path1/path2"}, |
166 |
| - {"query", "q1=a1&q2=a2"}, |
167 |
| - {"success", "true"}}}, |
168 |
| - {"https://user@[fe80::225:93da:bfde:b5f5]/path1/path2?q1=a1&q2=a2", |
169 |
| - {{"host", "[fe80::225:93da:bfde:b5f5]"}, |
170 |
| - {"port", "443"}, |
171 |
| - {"scheme", "https"}, |
172 |
| - {"path", "/path1/path2"}, |
173 |
| - {"query", "q1=a1&q2=a2"}, |
174 |
| - {"success", "true"}}}, |
175 |
| - {"http://[fe80::225:93da:bfde:b5f5]/path1@bbb/path2?q1=a1&q2=a2", |
176 |
| - {{"host", "[fe80::225:93da:bfde:b5f5]"}, |
177 |
| - {"port", "80"}, |
178 |
| - {"scheme", "http"}, |
179 |
| - {"path", "/path1@bbb/path2"}, |
180 |
| - {"query", "q1=a1&q2=a2"}, |
181 |
| - {"success", "true"}}}, |
182 |
| - {"https://https://example.com/some/path", |
183 |
| - {{"host", "https"}, |
184 |
| - {"port", "0"}, |
185 |
| - {"scheme", "https"}, |
186 |
| - {"path", "//example.com/some/path"}, |
187 |
| - {"query", ""}, |
188 |
| - {"success", "false"}}}, |
189 |
| - {"https://example.com:-1/some/path", |
190 |
| - {{"host", "example.com"}, |
191 |
| - {"port", "0"}, |
192 |
| - {"scheme", "https"}, |
193 |
| - {"path", "/some/path"}, |
194 |
| - {"query", ""}, |
195 |
| - {"success", "false"}}}, |
196 |
| - {"https://example.com:65536/some/path", |
197 |
| - {{"host", "example.com"}, |
198 |
| - {"port", "0"}, |
199 |
| - {"scheme", "https"}, |
200 |
| - {"path", "/some/path"}, |
201 |
| - {"query", ""}, |
202 |
| - {"success", "false"}}}, |
203 |
| - {"https://example.com:80a/some/path", |
204 |
| - {{"host", "example.com"}, |
205 |
| - {"port", "0"}, |
206 |
| - {"scheme", "https"}, |
207 |
| - {"path", "/some/path"}, |
208 |
| - {"query", ""}, |
209 |
| - {"success", "false"}}}, |
210 |
| - {"https://example.com:18446744073709551616/some/path", |
211 |
| - {{"host", "example.com"}, |
212 |
| - {"port", "0"}, |
213 |
| - {"scheme", "https"}, |
214 |
| - {"path", "/some/path"}, |
215 |
| - {"query", ""}, |
216 |
| - {"success", "false"}}}, |
217 |
| - }; |
218 |
| - for (auto &url_map : urls_map) |
| 21 | + friend void PrintTo(const ParsedUrl &p, std::ostream *os) |
219 | 22 | {
|
220 |
| - http_common::UrlParser url(url_map.first); |
221 |
| - auto url_properties = url_map.second; |
222 |
| - ASSERT_EQ(BoolToString(url.success_), url_properties["success"]); |
223 |
| - ASSERT_EQ(url.host_, url_properties["host"]); |
224 |
| - ASSERT_EQ(std::to_string(url.port_), url_properties["port"]); |
225 |
| - ASSERT_EQ(url.scheme_, url_properties["scheme"]); |
226 |
| - ASSERT_EQ(url.path_, url_properties["path"]); |
227 |
| - ASSERT_EQ(url.query_, url_properties["query"]); |
| 23 | + *os << "(valid: " << (p.success ? "yes" : "no") << ", scheme: " << p.scheme |
| 24 | + << ", host: " << p.host << ", port: " << p.port << ", path: " << p.path |
| 25 | + << ", query: " << p.query << ")"; |
228 | 26 | }
|
| 27 | +}; |
| 28 | + |
| 29 | +class UrlParserTests : public testing::TestWithParam<std::tuple<std::string, ParsedUrl>> |
| 30 | +{}; |
| 31 | + |
| 32 | +INSTANTIATE_TEST_SUITE_P( |
| 33 | + SampleValues, |
| 34 | + UrlParserTests, |
| 35 | + testing::Values( |
| 36 | + std::make_tuple("www.abc.com", ParsedUrl{"http", "www.abc.com", 80, "/", "", true}), |
| 37 | + std::make_tuple("http://www.abc.com", ParsedUrl{"http", "www.abc.com", 80, "/", "", true}), |
| 38 | + std::make_tuple("https://www.abc.com", |
| 39 | + ParsedUrl{"https", "www.abc.com", 443, "/", "", true}), |
| 40 | + std::make_tuple("https://www.abc.com:4431", |
| 41 | + ParsedUrl{"https", "www.abc.com", 4431, "/", "", true}), |
| 42 | + std::make_tuple("https://www.abc.com:4431/path1", |
| 43 | + ParsedUrl{"https", "www.abc.com", 4431, "/path1", "", true}), |
| 44 | + std::make_tuple("https://www.abc.com:4431/path1/path2", |
| 45 | + ParsedUrl{"https", "www.abc.com", 4431, "/path1/path2", "", true}), |
| 46 | + std::make_tuple("https://www.abc.com/path1/path2", |
| 47 | + ParsedUrl{"https", "www.abc.com", 443, "/path1/path2", "", true}), |
| 48 | + std::make_tuple("http://www.abc.com/path1/path2?q1=a1&q2=a2", |
| 49 | + ParsedUrl{"http", "www.abc.com", 80, "/path1/path2", "q1=a1&q2=a2", true}), |
| 50 | + std::make_tuple("http://www.abc.com:8080/path1/path2?q1=a1&q2=a2", |
| 51 | + ParsedUrl{"http", "www.abc.com", 8080, "/path1/path2", "q1=a1&q2=a2", |
| 52 | + true}), |
| 53 | + std::make_tuple("www.abc.com:8080/path1/path2?q1=a1&q2=a2", |
| 54 | + ParsedUrl{"http", "www.abc.com", 8080, "/path1/path2", "q1=a1&q2=a2", |
| 55 | + true}), |
| 56 | + std::make_tuple( "http://user:[email protected]:8080/path1/path2?q1=a1&q2=a2", |
| 57 | + ParsedUrl{"http", "www.abc.com", 8080, "/path1/path2", "q1=a1&q2=a2", |
| 58 | + true}), |
| 59 | + std::make_tuple( "user:[email protected]:8080/path1/path2?q1=a1&q2=a2", |
| 60 | + ParsedUrl{"http", "www.abc.com", 8080, "/path1/path2", "q1=a1&q2=a2", |
| 61 | + true}), |
| 62 | + std::make_tuple( "https://[email protected]/path1/path2?q1=a1&q2=a2", |
| 63 | + ParsedUrl{"https", "www.abc.com", 443, "/path1/path2", "q1=a1&q2=a2", |
| 64 | + true}), |
| 65 | + std::make_tuple("http://www.abc.com/path1@bbb/path2?q1=a1&q2=a2", |
| 66 | + ParsedUrl{"http", "www.abc.com", 80, "/path1@bbb/path2", "q1=a1&q2=a2", |
| 67 | + true}), |
| 68 | + std::make_tuple("http://1.2.3.4/path1/path2?q1=a1&q2=a2", |
| 69 | + ParsedUrl{"http", "1.2.3.4", 80, "/path1/path2", "q1=a1&q2=a2", true}), |
| 70 | + std::make_tuple( "user:[email protected]:8080/path1/path2?q1=a1&q2=a2", |
| 71 | + ParsedUrl{"http", "1.2.3.4", 8080, "/path1/path2", "q1=a1&q2=a2", true}), |
| 72 | + std::make_tuple( "https://[email protected]/path1/path2?q1=a1&q2=a2", |
| 73 | + ParsedUrl{"https", "1.2.3.4", 443, "/path1/path2", "q1=a1&q2=a2", true}), |
| 74 | + std::make_tuple("http://1.2.3.4/path1@bbb/path2?q1=a1&q2=a2", |
| 75 | + ParsedUrl{"http", "1.2.3.4", 80, "/path1@bbb/path2", "q1=a1&q2=a2", true}), |
| 76 | + std::make_tuple("http://[fe80::225:93da:bfde:b5f5]/path1/path2?q1=a1&q2=a2", |
| 77 | + ParsedUrl{"http", "[fe80::225:93da:bfde:b5f5]", 80, "/path1/path2", |
| 78 | + "q1=a1&q2=a2", true}), |
| 79 | + std::make_tuple("user:password@[fe80::225:93da:bfde:b5f5]:8080/path1/path2?q1=a1&q2=a2", |
| 80 | + ParsedUrl{"http", "[fe80::225:93da:bfde:b5f5]", 8080, "/path1/path2", |
| 81 | + "q1=a1&q2=a2", true}), |
| 82 | + std::make_tuple("https://user@[fe80::225:93da:bfde:b5f5]/path1/path2?q1=a1&q2=a2", |
| 83 | + ParsedUrl{"https", "[fe80::225:93da:bfde:b5f5]", 443, "/path1/path2", |
| 84 | + "q1=a1&q2=a2", true}), |
| 85 | + std::make_tuple("http://[fe80::225:93da:bfde:b5f5]/path1@bbb/path2?q1=a1&q2=a2", |
| 86 | + ParsedUrl{"http", "[fe80::225:93da:bfde:b5f5]", 80, "/path1@bbb/path2", |
| 87 | + "q1=a1&q2=a2", true}), |
| 88 | + std::make_tuple("https://https://example.com/some/path", |
| 89 | + ParsedUrl{"https", "https", 0, "//example.com/some/path", "", false}), |
| 90 | + std::make_tuple("https://example.com:-1/some/path", |
| 91 | + ParsedUrl{"https", "example.com", 0, "/some/path", "", false}), |
| 92 | + std::make_tuple("https://example.com:65536/some/path", |
| 93 | + ParsedUrl{"https", "example.com", 0, "/some/path", "", false}), |
| 94 | + std::make_tuple("https://example.com:80a/some/path", |
| 95 | + ParsedUrl{"https", "example.com", 0, "/some/path", "", false}), |
| 96 | + std::make_tuple("https://example.com:18446744073709551616/some/path", |
| 97 | + ParsedUrl{"https", "example.com", 0, "/some/path", "", false}))); |
| 98 | + |
| 99 | +TEST_P(UrlParserTests, BasicTests) |
| 100 | +{ |
| 101 | + const auto &url = std::get<0>(GetParam()); |
| 102 | + const auto &expected = std::get<1>(GetParam()); |
| 103 | + |
| 104 | + const auto actual = http_common::UrlParser(url); |
| 105 | + |
| 106 | + EXPECT_EQ(actual.success_, expected.success); |
| 107 | + EXPECT_EQ(actual.host_, expected.host); |
| 108 | + EXPECT_EQ(actual.port_, expected.port); |
| 109 | + EXPECT_EQ(actual.scheme_, expected.scheme); |
| 110 | + EXPECT_EQ(actual.path_, expected.path); |
| 111 | + EXPECT_EQ(actual.query_, expected.query); |
229 | 112 | }
|
230 | 113 |
|
231 |
| -TEST(UrlDecoderTests, BasicTests) |
| 114 | +class UrlDecoderTests : public ::testing::TestWithParam<std::tuple<std::string, std::string>> |
| 115 | +{}; |
| 116 | + |
| 117 | +INSTANTIATE_TEST_SUITE_P( |
| 118 | + SampleValues, |
| 119 | + UrlDecoderTests, |
| 120 | + testing::Values(std::make_tuple("Authentication=Basic xxx", "Authentication=Basic xxx"), |
| 121 | + std::make_tuple("Authentication=Basic%20xxx", "Authentication=Basic xxx"), |
| 122 | + std::make_tuple("%C3%B6%C3%A0%C2%A7%C3%96abcd%C3%84", |
| 123 | + "\xc3\xb6\xc3\xa0\xc2\xa7\xc3\x96\x61\x62\x63\x64\xc3\x84"), |
| 124 | + std::make_tuple("%2x", "%2x"), |
| 125 | + std::make_tuple("%20", " "), |
| 126 | + std::make_tuple("text%2", "text%2"), |
| 127 | + std::make_tuple("%20test%zztest", "%20test%zztest"), |
| 128 | + std::make_tuple("%20test%2", "%20test%2"))); |
| 129 | + |
| 130 | +TEST_P(UrlDecoderTests, BasicTests) |
232 | 131 | {
|
233 |
| - std::map<std::string, std::string> testdata{ |
234 |
| - {"Authentication=Basic xxx", "Authentication=Basic xxx"}, |
235 |
| - {"Authentication=Basic%20xxx", "Authentication=Basic xxx"}, |
236 |
| - {"%C3%B6%C3%A0%C2%A7%C3%96abcd%C3%84", |
237 |
| - "\xc3\xb6\xc3\xa0\xc2\xa7\xc3\x96\x61\x62\x63\x64\xc3\x84"}, |
238 |
| - {"%2x", "%2x"}, |
239 |
| - {"%20", " "}, |
240 |
| - {"text%2", "text%2"}, |
241 |
| - {"%20test%zztest", "%20test%zztest"}, |
242 |
| - {"%20test%2", "%20test%2"}}; |
| 132 | + const auto &encoded = std::get<0>(GetParam()); |
| 133 | + const auto &expected = std::get<1>(GetParam()); |
| 134 | + const auto actual = http_common::UrlDecoder::Decode(encoded); |
243 | 135 |
|
244 |
| - for (auto &testsample : testdata) |
245 |
| - { |
246 |
| - ASSERT_EQ(http_common::UrlDecoder::Decode(testsample.first), testsample.second); |
247 |
| - ASSERT_TRUE(http_common::UrlDecoder::Decode(testsample.first) == testsample.second); |
248 |
| - } |
| 136 | + EXPECT_EQ(actual, expected); |
249 | 137 | }
|
0 commit comments