2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "folly/Uri.h"
19 #include <boost/algorithm/string.hpp>
20 #include <glog/logging.h>
21 #include <gtest/gtest.h>
23 using namespace folly;
31 fbstring s("http://www.facebook.com/hello/world?query#fragment");
33 EXPECT_EQ("http", u.scheme());
34 EXPECT_EQ("", u.username());
35 EXPECT_EQ("", u.password());
36 EXPECT_EQ("www.facebook.com", u.host());
37 EXPECT_EQ(0, u.port());
38 EXPECT_EQ("www.facebook.com", u.authority());
39 EXPECT_EQ("/hello/world", u.path());
40 EXPECT_EQ("query", u.query());
41 EXPECT_EQ("fragment", u.fragment());
42 EXPECT_EQ(s, u.fbstr()); // canonical
46 fbstring s("http://www.facebook.com:8080/hello/world?query#fragment");
48 EXPECT_EQ("http", u.scheme());
49 EXPECT_EQ("", u.username());
50 EXPECT_EQ("", u.password());
51 EXPECT_EQ("www.facebook.com", u.host());
52 EXPECT_EQ(8080, u.port());
53 EXPECT_EQ("www.facebook.com:8080", u.authority());
54 EXPECT_EQ("/hello/world", u.path());
55 EXPECT_EQ("query", u.query());
56 EXPECT_EQ("fragment", u.fragment());
57 EXPECT_EQ(s, u.fbstr()); // canonical
61 fbstring s("http://127.0.0.1:8080/hello/world?query#fragment");
63 EXPECT_EQ("http", u.scheme());
64 EXPECT_EQ("", u.username());
65 EXPECT_EQ("", u.password());
66 EXPECT_EQ("127.0.0.1", u.host());
67 EXPECT_EQ(8080, u.port());
68 EXPECT_EQ("127.0.0.1:8080", u.authority());
69 EXPECT_EQ("/hello/world", u.path());
70 EXPECT_EQ("query", u.query());
71 EXPECT_EQ("fragment", u.fragment());
72 EXPECT_EQ(s, u.fbstr()); // canonical
76 fbstring s("http://[::1]:8080/hello/world?query#fragment");
78 EXPECT_EQ("http", u.scheme());
79 EXPECT_EQ("", u.username());
80 EXPECT_EQ("", u.password());
81 EXPECT_EQ("[::1]", u.host());
82 EXPECT_EQ("::1", u.hostname());
83 EXPECT_EQ(8080, u.port());
84 EXPECT_EQ("[::1]:8080", u.authority());
85 EXPECT_EQ("/hello/world", u.path());
86 EXPECT_EQ("query", u.query());
87 EXPECT_EQ("fragment", u.fragment());
88 EXPECT_EQ(s, u.fbstr()); // canonical
92 fbstring s("http://[2401:db00:20:7004:face:0:29:0]:8080/hello/world?query");
94 EXPECT_EQ("http", u.scheme());
95 EXPECT_EQ("", u.username());
96 EXPECT_EQ("", u.password());
97 EXPECT_EQ("[2401:db00:20:7004:face:0:29:0]", u.host());
98 EXPECT_EQ("2401:db00:20:7004:face:0:29:0", u.hostname());
99 EXPECT_EQ(8080, u.port());
100 EXPECT_EQ("[2401:db00:20:7004:face:0:29:0]:8080", u.authority());
101 EXPECT_EQ("/hello/world", u.path());
102 EXPECT_EQ("query", u.query());
103 EXPECT_EQ("", u.fragment());
104 EXPECT_EQ(s, u.fbstr()); // canonical
108 fbstring s("http://[2401:db00:20:7004:face:0:29:0]/hello/world?query");
110 EXPECT_EQ("http", u.scheme());
111 EXPECT_EQ("", u.username());
112 EXPECT_EQ("", u.password());
113 EXPECT_EQ("[2401:db00:20:7004:face:0:29:0]", u.host());
114 EXPECT_EQ("2401:db00:20:7004:face:0:29:0", u.hostname());
115 EXPECT_EQ(0, u.port());
116 EXPECT_EQ("[2401:db00:20:7004:face:0:29:0]", u.authority());
117 EXPECT_EQ("/hello/world", u.path());
118 EXPECT_EQ("query", u.query());
119 EXPECT_EQ("", u.fragment());
120 EXPECT_EQ(s, u.fbstr()); // canonical
124 fbstring s("http://user:pass@host.com/");
126 EXPECT_EQ("http", u.scheme());
127 EXPECT_EQ("user", u.username());
128 EXPECT_EQ("pass", u.password());
129 EXPECT_EQ("host.com", u.host());
130 EXPECT_EQ(0, u.port());
131 EXPECT_EQ("user:pass@host.com", u.authority());
132 EXPECT_EQ("/", u.path());
133 EXPECT_EQ("", u.query());
134 EXPECT_EQ("", u.fragment());
135 EXPECT_EQ(s, u.fbstr());
139 fbstring s("http://user@host.com/");
141 EXPECT_EQ("http", u.scheme());
142 EXPECT_EQ("user", u.username());
143 EXPECT_EQ("", u.password());
144 EXPECT_EQ("host.com", u.host());
145 EXPECT_EQ(0, u.port());
146 EXPECT_EQ("user@host.com", u.authority());
147 EXPECT_EQ("/", u.path());
148 EXPECT_EQ("", u.query());
149 EXPECT_EQ("", u.fragment());
150 EXPECT_EQ(s, u.fbstr());
154 fbstring s("http://user:@host.com/");
156 EXPECT_EQ("http", u.scheme());
157 EXPECT_EQ("user", u.username());
158 EXPECT_EQ("", u.password());
159 EXPECT_EQ("host.com", u.host());
160 EXPECT_EQ(0, u.port());
161 EXPECT_EQ("user@host.com", u.authority());
162 EXPECT_EQ("/", u.path());
163 EXPECT_EQ("", u.query());
164 EXPECT_EQ("", u.fragment());
165 EXPECT_EQ("http://user@host.com/", u.fbstr());
169 fbstring s("http://:pass@host.com/");
171 EXPECT_EQ("http", u.scheme());
172 EXPECT_EQ("", u.username());
173 EXPECT_EQ("pass", u.password());
174 EXPECT_EQ("host.com", u.host());
175 EXPECT_EQ(0, u.port());
176 EXPECT_EQ(":pass@host.com", u.authority());
177 EXPECT_EQ("/", u.path());
178 EXPECT_EQ("", u.query());
179 EXPECT_EQ("", u.fragment());
180 EXPECT_EQ(s, u.fbstr());
184 fbstring s("http://@host.com/");
186 EXPECT_EQ("http", u.scheme());
187 EXPECT_EQ("", u.username());
188 EXPECT_EQ("", u.password());
189 EXPECT_EQ("host.com", u.host());
190 EXPECT_EQ(0, u.port());
191 EXPECT_EQ("host.com", u.authority());
192 EXPECT_EQ("/", u.path());
193 EXPECT_EQ("", u.query());
194 EXPECT_EQ("", u.fragment());
195 EXPECT_EQ("http://host.com/", u.fbstr());
199 fbstring s("http://:@host.com/");
201 EXPECT_EQ("http", u.scheme());
202 EXPECT_EQ("", u.username());
203 EXPECT_EQ("", u.password());
204 EXPECT_EQ("host.com", u.host());
205 EXPECT_EQ(0, u.port());
206 EXPECT_EQ("host.com", u.authority());
207 EXPECT_EQ("/", u.path());
208 EXPECT_EQ("", u.query());
209 EXPECT_EQ("", u.fragment());
210 EXPECT_EQ("http://host.com/", u.fbstr());
214 fbstring s("file:///etc/motd");
216 EXPECT_EQ("file", u.scheme());
217 EXPECT_EQ("", u.username());
218 EXPECT_EQ("", u.password());
219 EXPECT_EQ("", u.host());
220 EXPECT_EQ(0, u.port());
221 EXPECT_EQ("", u.authority());
222 EXPECT_EQ("/etc/motd", u.path());
223 EXPECT_EQ("", u.query());
224 EXPECT_EQ("", u.fragment());
225 EXPECT_EQ(s, u.fbstr());
229 fbstring s("file:/etc/motd");
231 EXPECT_EQ("file", u.scheme());
232 EXPECT_EQ("", u.username());
233 EXPECT_EQ("", u.password());
234 EXPECT_EQ("", u.host());
235 EXPECT_EQ(0, u.port());
236 EXPECT_EQ("", u.authority());
237 EXPECT_EQ("/etc/motd", u.path());
238 EXPECT_EQ("", u.query());
239 EXPECT_EQ("", u.fragment());
240 EXPECT_EQ("file:///etc/motd", u.fbstr());
244 fbstring s("file://etc/motd");
246 EXPECT_EQ("file", u.scheme());
247 EXPECT_EQ("", u.username());
248 EXPECT_EQ("", u.password());
249 EXPECT_EQ("etc", u.host());
250 EXPECT_EQ(0, u.port());
251 EXPECT_EQ("etc", u.authority());
252 EXPECT_EQ("/motd", u.path());
253 EXPECT_EQ("", u.query());
254 EXPECT_EQ("", u.fragment());
255 EXPECT_EQ(s, u.fbstr());
259 fbstring s("2http://www.facebook.com");
263 CHECK(false) << "Control should not have reached here";
264 } catch (const std::invalid_argument& ex) {
265 EXPECT_TRUE(boost::algorithm::ends_with(ex.what(), s));
270 fbstring s("www[facebook]com");
273 Uri u("http://" + s);
274 CHECK(false) << "Control should not have reached here";
275 } catch (const std::invalid_argument& ex) {
276 EXPECT_TRUE(boost::algorithm::ends_with(ex.what(), s));
281 fbstring s("http://[::1:8080/hello/world?query#fragment");
285 CHECK(false) << "Control should not have reached here";
286 } catch (const std::invalid_argument& ex) {
292 fbstring s("http://::1]:8080/hello/world?query#fragment");
296 CHECK(false) << "Control should not have reached here";
297 } catch (const std::invalid_argument& ex) {
303 fbstring s("http://::1:8080/hello/world?query#fragment");
307 CHECK(false) << "Control should not have reached here";
308 } catch (const std::invalid_argument& ex) {
314 fbstring s("http://2401:db00:20:7004:face:0:29:0/hello/world?query");
318 CHECK(false) << "Control should not have reached here";
319 } catch (const std::invalid_argument& ex) {