Fix performance issues with folly::Uri::authority().
[folly.git] / folly / test / UriTest.cpp
1 /*
2  * Copyright 2013 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 <glog/logging.h>
20 #include <gtest/gtest.h>
21
22 using namespace folly;
23
24 namespace {
25
26 }  // namespace
27
28 TEST(Uri, Simple) {
29   {
30     fbstring s("http://www.facebook.com/hello/world?query#fragment");
31     Uri u(s);
32     EXPECT_EQ("http", u.scheme());
33     EXPECT_EQ("", u.username());
34     EXPECT_EQ("", u.password());
35     EXPECT_EQ("www.facebook.com", u.host());
36     EXPECT_EQ(0, u.port());
37     EXPECT_EQ("www.facebook.com", u.authority());
38     EXPECT_EQ("/hello/world", u.path());
39     EXPECT_EQ("query", u.query());
40     EXPECT_EQ("fragment", u.fragment());
41     EXPECT_EQ(s, u.fbstr());  // canonical
42   }
43
44   {
45     fbstring s("http://www.facebook.com:8080/hello/world?query#fragment");
46     Uri u(s);
47     EXPECT_EQ("http", u.scheme());
48     EXPECT_EQ("", u.username());
49     EXPECT_EQ("", u.password());
50     EXPECT_EQ("www.facebook.com", u.host());
51     EXPECT_EQ(8080, u.port());
52     EXPECT_EQ("www.facebook.com:8080", u.authority());
53     EXPECT_EQ("/hello/world", u.path());
54     EXPECT_EQ("query", u.query());
55     EXPECT_EQ("fragment", u.fragment());
56     EXPECT_EQ(s, u.fbstr());  // canonical
57   }
58
59   {
60     fbstring s("http://127.0.0.1:8080/hello/world?query#fragment");
61     Uri u(s);
62     EXPECT_EQ("http", u.scheme());
63     EXPECT_EQ("", u.username());
64     EXPECT_EQ("", u.password());
65     EXPECT_EQ("127.0.0.1", u.host());
66     EXPECT_EQ(8080, u.port());
67     EXPECT_EQ("127.0.0.1:8080", u.authority());
68     EXPECT_EQ("/hello/world", u.path());
69     EXPECT_EQ("query", u.query());
70     EXPECT_EQ("fragment", u.fragment());
71     EXPECT_EQ(s, u.fbstr());  // canonical
72   }
73
74   {
75     fbstring s("http://[::1]:8080/hello/world?query#fragment");
76     Uri u(s);
77     EXPECT_EQ("http", u.scheme());
78     EXPECT_EQ("", u.username());
79     EXPECT_EQ("", u.password());
80     EXPECT_EQ("[::1]", u.host());
81     EXPECT_EQ(8080, u.port());
82     EXPECT_EQ("[::1]:8080", u.authority());
83     EXPECT_EQ("/hello/world", u.path());
84     EXPECT_EQ("query", u.query());
85     EXPECT_EQ("fragment", u.fragment());
86     EXPECT_EQ(s, u.fbstr());  // canonical
87   }
88
89   {
90     fbstring s("http://user:pass@host.com/");
91     Uri u(s);
92     EXPECT_EQ("http", u.scheme());
93     EXPECT_EQ("user", u.username());
94     EXPECT_EQ("pass", u.password());
95     EXPECT_EQ("host.com", u.host());
96     EXPECT_EQ(0, u.port());
97     EXPECT_EQ("user:pass@host.com", u.authority());
98     EXPECT_EQ("/", u.path());
99     EXPECT_EQ("", u.query());
100     EXPECT_EQ("", u.fragment());
101     EXPECT_EQ(s, u.fbstr());
102   }
103
104   {
105     fbstring s("http://user@host.com/");
106     Uri u(s);
107     EXPECT_EQ("http", u.scheme());
108     EXPECT_EQ("user", u.username());
109     EXPECT_EQ("", u.password());
110     EXPECT_EQ("host.com", u.host());
111     EXPECT_EQ(0, u.port());
112     EXPECT_EQ("user@host.com", u.authority());
113     EXPECT_EQ("/", u.path());
114     EXPECT_EQ("", u.query());
115     EXPECT_EQ("", u.fragment());
116     EXPECT_EQ(s, u.fbstr());
117   }
118
119   {
120     fbstring s("http://user:@host.com/");
121     Uri u(s);
122     EXPECT_EQ("http", u.scheme());
123     EXPECT_EQ("user", u.username());
124     EXPECT_EQ("", u.password());
125     EXPECT_EQ("host.com", u.host());
126     EXPECT_EQ(0, u.port());
127     EXPECT_EQ("user@host.com", u.authority());
128     EXPECT_EQ("/", u.path());
129     EXPECT_EQ("", u.query());
130     EXPECT_EQ("", u.fragment());
131     EXPECT_EQ("http://user@host.com/", u.fbstr());
132   }
133
134   {
135     fbstring s("http://:pass@host.com/");
136     Uri u(s);
137     EXPECT_EQ("http", u.scheme());
138     EXPECT_EQ("", u.username());
139     EXPECT_EQ("pass", u.password());
140     EXPECT_EQ("host.com", u.host());
141     EXPECT_EQ(0, u.port());
142     EXPECT_EQ(":pass@host.com", u.authority());
143     EXPECT_EQ("/", u.path());
144     EXPECT_EQ("", u.query());
145     EXPECT_EQ("", u.fragment());
146     EXPECT_EQ(s, u.fbstr());
147   }
148
149   {
150     fbstring s("http://@host.com/");
151     Uri u(s);
152     EXPECT_EQ("http", u.scheme());
153     EXPECT_EQ("", u.username());
154     EXPECT_EQ("", u.password());
155     EXPECT_EQ("host.com", u.host());
156     EXPECT_EQ(0, u.port());
157     EXPECT_EQ("host.com", u.authority());
158     EXPECT_EQ("/", u.path());
159     EXPECT_EQ("", u.query());
160     EXPECT_EQ("", u.fragment());
161     EXPECT_EQ("http://host.com/", u.fbstr());
162   }
163
164   {
165     fbstring s("http://:@host.com/");
166     Uri u(s);
167     EXPECT_EQ("http", u.scheme());
168     EXPECT_EQ("", u.username());
169     EXPECT_EQ("", u.password());
170     EXPECT_EQ("host.com", u.host());
171     EXPECT_EQ(0, u.port());
172     EXPECT_EQ("host.com", u.authority());
173     EXPECT_EQ("/", u.path());
174     EXPECT_EQ("", u.query());
175     EXPECT_EQ("", u.fragment());
176     EXPECT_EQ("http://host.com/", u.fbstr());
177   }
178
179   {
180     fbstring s("file:///etc/motd");
181     Uri u(s);
182     EXPECT_EQ("file", u.scheme());
183     EXPECT_EQ("", u.username());
184     EXPECT_EQ("", u.password());
185     EXPECT_EQ("", u.host());
186     EXPECT_EQ(0, u.port());
187     EXPECT_EQ("", u.authority());
188     EXPECT_EQ("/etc/motd", u.path());
189     EXPECT_EQ("", u.query());
190     EXPECT_EQ("", u.fragment());
191     EXPECT_EQ(s, u.fbstr());
192   }
193
194   {
195     fbstring s("file:/etc/motd");
196     Uri u(s);
197     EXPECT_EQ("file", u.scheme());
198     EXPECT_EQ("", u.username());
199     EXPECT_EQ("", u.password());
200     EXPECT_EQ("", u.host());
201     EXPECT_EQ(0, u.port());
202     EXPECT_EQ("", u.authority());
203     EXPECT_EQ("/etc/motd", u.path());
204     EXPECT_EQ("", u.query());
205     EXPECT_EQ("", u.fragment());
206     EXPECT_EQ("file:///etc/motd", u.fbstr());
207   }
208
209   {
210     fbstring s("file://etc/motd");
211     Uri u(s);
212     EXPECT_EQ("file", u.scheme());
213     EXPECT_EQ("", u.username());
214     EXPECT_EQ("", u.password());
215     EXPECT_EQ("etc", u.host());
216     EXPECT_EQ(0, u.port());
217     EXPECT_EQ("etc", u.authority());
218     EXPECT_EQ("/motd", u.path());
219     EXPECT_EQ("", u.query());
220     EXPECT_EQ("", u.fragment());
221     EXPECT_EQ(s, u.fbstr());
222   }
223
224   EXPECT_THROW({Uri("2http://www.facebook.com/");},
225                std::invalid_argument);
226 }