Copyright 2013 -> 2014
[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(8080, u.port());
83     EXPECT_EQ("[::1]:8080", u.authority());
84     EXPECT_EQ("/hello/world", u.path());
85     EXPECT_EQ("query", u.query());
86     EXPECT_EQ("fragment", u.fragment());
87     EXPECT_EQ(s, u.fbstr());  // canonical
88   }
89
90   {
91     fbstring s("http://user:pass@host.com/");
92     Uri u(s);
93     EXPECT_EQ("http", u.scheme());
94     EXPECT_EQ("user", u.username());
95     EXPECT_EQ("pass", u.password());
96     EXPECT_EQ("host.com", u.host());
97     EXPECT_EQ(0, u.port());
98     EXPECT_EQ("user:pass@host.com", u.authority());
99     EXPECT_EQ("/", u.path());
100     EXPECT_EQ("", u.query());
101     EXPECT_EQ("", u.fragment());
102     EXPECT_EQ(s, u.fbstr());
103   }
104
105   {
106     fbstring s("http://user@host.com/");
107     Uri u(s);
108     EXPECT_EQ("http", u.scheme());
109     EXPECT_EQ("user", u.username());
110     EXPECT_EQ("", u.password());
111     EXPECT_EQ("host.com", u.host());
112     EXPECT_EQ(0, u.port());
113     EXPECT_EQ("user@host.com", u.authority());
114     EXPECT_EQ("/", u.path());
115     EXPECT_EQ("", u.query());
116     EXPECT_EQ("", u.fragment());
117     EXPECT_EQ(s, u.fbstr());
118   }
119
120   {
121     fbstring s("http://user:@host.com/");
122     Uri u(s);
123     EXPECT_EQ("http", u.scheme());
124     EXPECT_EQ("user", u.username());
125     EXPECT_EQ("", u.password());
126     EXPECT_EQ("host.com", u.host());
127     EXPECT_EQ(0, u.port());
128     EXPECT_EQ("user@host.com", u.authority());
129     EXPECT_EQ("/", u.path());
130     EXPECT_EQ("", u.query());
131     EXPECT_EQ("", u.fragment());
132     EXPECT_EQ("http://user@host.com/", u.fbstr());
133   }
134
135   {
136     fbstring s("http://:pass@host.com/");
137     Uri u(s);
138     EXPECT_EQ("http", u.scheme());
139     EXPECT_EQ("", u.username());
140     EXPECT_EQ("pass", u.password());
141     EXPECT_EQ("host.com", u.host());
142     EXPECT_EQ(0, u.port());
143     EXPECT_EQ(":pass@host.com", u.authority());
144     EXPECT_EQ("/", u.path());
145     EXPECT_EQ("", u.query());
146     EXPECT_EQ("", u.fragment());
147     EXPECT_EQ(s, u.fbstr());
148   }
149
150   {
151     fbstring s("http://@host.com/");
152     Uri u(s);
153     EXPECT_EQ("http", u.scheme());
154     EXPECT_EQ("", u.username());
155     EXPECT_EQ("", u.password());
156     EXPECT_EQ("host.com", u.host());
157     EXPECT_EQ(0, u.port());
158     EXPECT_EQ("host.com", u.authority());
159     EXPECT_EQ("/", u.path());
160     EXPECT_EQ("", u.query());
161     EXPECT_EQ("", u.fragment());
162     EXPECT_EQ("http://host.com/", u.fbstr());
163   }
164
165   {
166     fbstring s("http://:@host.com/");
167     Uri u(s);
168     EXPECT_EQ("http", u.scheme());
169     EXPECT_EQ("", u.username());
170     EXPECT_EQ("", u.password());
171     EXPECT_EQ("host.com", u.host());
172     EXPECT_EQ(0, u.port());
173     EXPECT_EQ("host.com", u.authority());
174     EXPECT_EQ("/", u.path());
175     EXPECT_EQ("", u.query());
176     EXPECT_EQ("", u.fragment());
177     EXPECT_EQ("http://host.com/", u.fbstr());
178   }
179
180   {
181     fbstring s("file:///etc/motd");
182     Uri u(s);
183     EXPECT_EQ("file", u.scheme());
184     EXPECT_EQ("", u.username());
185     EXPECT_EQ("", u.password());
186     EXPECT_EQ("", u.host());
187     EXPECT_EQ(0, u.port());
188     EXPECT_EQ("", u.authority());
189     EXPECT_EQ("/etc/motd", u.path());
190     EXPECT_EQ("", u.query());
191     EXPECT_EQ("", u.fragment());
192     EXPECT_EQ(s, u.fbstr());
193   }
194
195   {
196     fbstring s("file:/etc/motd");
197     Uri u(s);
198     EXPECT_EQ("file", u.scheme());
199     EXPECT_EQ("", u.username());
200     EXPECT_EQ("", u.password());
201     EXPECT_EQ("", u.host());
202     EXPECT_EQ(0, u.port());
203     EXPECT_EQ("", u.authority());
204     EXPECT_EQ("/etc/motd", u.path());
205     EXPECT_EQ("", u.query());
206     EXPECT_EQ("", u.fragment());
207     EXPECT_EQ("file:///etc/motd", u.fbstr());
208   }
209
210   {
211     fbstring s("file://etc/motd");
212     Uri u(s);
213     EXPECT_EQ("file", u.scheme());
214     EXPECT_EQ("", u.username());
215     EXPECT_EQ("", u.password());
216     EXPECT_EQ("etc", u.host());
217     EXPECT_EQ(0, u.port());
218     EXPECT_EQ("etc", u.authority());
219     EXPECT_EQ("/motd", u.path());
220     EXPECT_EQ("", u.query());
221     EXPECT_EQ("", u.fragment());
222     EXPECT_EQ(s, u.fbstr());
223   }
224
225   {
226     fbstring s("2http://www.facebook.com");
227
228     try {
229       Uri u(s);
230       CHECK(false) << "Control should not have reached here";
231     } catch (const std::invalid_argument& ex) {
232       EXPECT_TRUE(boost::algorithm::ends_with(ex.what(), s));
233     }
234   }
235
236   {
237     fbstring s("www[facebook]com");
238
239     try {
240       Uri u("http://" + s);
241       CHECK(false) << "Control should not have reached here";
242     } catch (const std::invalid_argument& ex) {
243       EXPECT_TRUE(boost::algorithm::ends_with(ex.what(), s));
244     }
245   }
246 }