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(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
91 fbstring s("http://user:pass@host.com/");
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());
106 fbstring s("http://user@host.com/");
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());
121 fbstring s("http://user:@host.com/");
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());
136 fbstring s("http://:pass@host.com/");
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());
151 fbstring s("http://@host.com/");
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());
166 fbstring s("http://:@host.com/");
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());
181 fbstring s("file:///etc/motd");
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());
196 fbstring s("file:/etc/motd");
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());
211 fbstring s("file://etc/motd");
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());
226 fbstring s("2http://www.facebook.com");
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));
237 fbstring s("www[facebook]com");
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));