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