a209f25576dd882b5f3db77e37f48ffeeb8e0dae
[folly.git] / folly / test / UriTest.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
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  */
16
17 #include "folly/Uri.h"
18
19 #include <boost/algorithm/string.hpp>
20 #include <glog/logging.h>
21 #include <gtest/gtest.h>
22
23 using namespace folly;
24
25 namespace {
26
27 }  // namespace
28
29 TEST(Uri, Simple) {
30   {
31     fbstring s("http://www.facebook.com/hello/world?query#fragment");
32     Uri u(s);
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
43   }
44
45   {
46     fbstring s("http://www.facebook.com:8080/hello/world?query#fragment");
47     Uri u(s);
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
58   }
59
60   {
61     fbstring s("http://127.0.0.1:8080/hello/world?query#fragment");
62     Uri u(s);
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
73   }
74
75   {
76     fbstring s("http://[::1]:8080/hello/world?query#fragment");
77     Uri u(s);
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
89   }
90
91   {
92     fbstring s("http://[2401:db00:20:7004:face:0:29:0]:8080/hello/world?query");
93     Uri u(s);
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
105   }
106
107   {
108     fbstring s("http://[2401:db00:20:7004:face:0:29:0]/hello/world?query");
109     Uri u(s);
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
121   }
122
123   {
124     fbstring s("http://user:pass@host.com/");
125     Uri u(s);
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());
136   }
137
138   {
139     fbstring s("http://user@host.com/");
140     Uri u(s);
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());
151   }
152
153   {
154     fbstring s("http://user:@host.com/");
155     Uri u(s);
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());
166   }
167
168   {
169     fbstring s("http://:pass@host.com/");
170     Uri u(s);
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());
181   }
182
183   {
184     fbstring s("http://@host.com/");
185     Uri u(s);
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());
196   }
197
198   {
199     fbstring s("http://:@host.com/");
200     Uri u(s);
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());
211   }
212
213   {
214     fbstring s("file:///etc/motd");
215     Uri u(s);
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());
226   }
227
228   {
229     fbstring s("file:/etc/motd");
230     Uri u(s);
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());
241   }
242
243   {
244     fbstring s("file://etc/motd");
245     Uri u(s);
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());
256   }
257
258   {
259     fbstring s("2http://www.facebook.com");
260
261     try {
262       Uri u(s);
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));
266     }
267   }
268
269   {
270     fbstring s("www[facebook]com");
271
272     try {
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));
277     }
278   }
279
280   {
281     fbstring s("http://[::1:8080/hello/world?query#fragment");
282
283     try {
284       Uri u(s);
285       CHECK(false) << "Control should not have reached here";
286     } catch (const std::invalid_argument& ex) {
287       // success
288     }
289   }
290
291   {
292     fbstring s("http://::1]:8080/hello/world?query#fragment");
293
294     try {
295       Uri u(s);
296       CHECK(false) << "Control should not have reached here";
297     } catch (const std::invalid_argument& ex) {
298       // success
299     }
300   }
301
302   {
303     fbstring s("http://::1:8080/hello/world?query#fragment");
304
305     try {
306       Uri u(s);
307       CHECK(false) << "Control should not have reached here";
308     } catch (const std::invalid_argument& ex) {
309       // success
310     }
311   }
312
313   {
314     fbstring s("http://2401:db00:20:7004:face:0:29:0/hello/world?query");
315
316     try {
317       Uri u(s);
318       CHECK(false) << "Control should not have reached here";
319     } catch (const std::invalid_argument& ex) {
320       // success
321     }
322   }
323 }