1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.http.client.utils;
28
29 import java.net.URI;
30 import java.util.Arrays;
31
32 import org.apache.http.HttpHost;
33 import org.apache.http.conn.routing.HttpRoute;
34 import org.junit.Assert;
35 import org.junit.Test;
36
37 import static org.apache.http.client.utils.URIUtils.DROP_FRAGMENT;
38 import static org.apache.http.client.utils.URIUtils.DROP_FRAGMENT_AND_NORMALIZE;
39 import static org.apache.http.client.utils.URIUtils.NORMALIZE;
40
41
42
43
44
45 public class TestURIUtils {
46
47 private final URI baseURI = URI.create("http://a/b/c/d;p?q");
48
49 @Test
50 public void testRewrite() throws Exception {
51 final HttpHost target = new HttpHost("thathost", -1);
52 Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
53 URI.create("http://thishost/stuff"), target).toString());
54 Assert.assertEquals("/stuff", URIUtils.rewriteURI(
55 URI.create("http://thishost/stuff"), null).toString());
56 Assert.assertEquals("/", URIUtils.rewriteURI(
57 URI.create("http://thishost//"), null).toString());
58 Assert.assertEquals("/stuff/morestuff", URIUtils.rewriteURI(
59 URI.create("http://thishost//stuff///morestuff"), null).toString());
60 Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
61 URI.create("http://thishost/stuff#crap"), target, DROP_FRAGMENT_AND_NORMALIZE).toString());
62 Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI(
63 URI.create("http://thishost/stuff#crap"), target, NORMALIZE).toString());
64 Assert.assertEquals("http://thathost/", URIUtils.rewriteURI(
65 URI.create("http://thishost#crap"), target, DROP_FRAGMENT_AND_NORMALIZE).toString());
66 Assert.assertEquals("http://thathost/#crap", URIUtils.rewriteURI(
67 URI.create("http://thishost#crap"), target, NORMALIZE).toString());
68 Assert.assertEquals("/stuff/", URIUtils.rewriteURI(
69 URI.create("http://thishost//////////////stuff/"), null).toString());
70 Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
71 URI.create("http://thathost/stuff")).toString());
72 Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
73 URI.create("http://thathost/stuff#fragment")).toString());
74 Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
75 URI.create("http://userinfo@thathost/stuff#fragment")).toString());
76 Assert.assertEquals("http://thathost/", URIUtils.rewriteURI(
77 URI.create("http://thathost")).toString());
78 Assert.assertEquals("http://thathost/", URIUtils.rewriteURI(
79 URI.create("http://ThatHost")).toString());
80 Assert.assertEquals("http://That_Host/", URIUtils.rewriteURI(
81 URI.create("http://That_Host")).toString());
82 Assert.assertEquals("http://thishost/Fragment_identifier%23Examples",
83 URIUtils.rewriteURI(
84 URI.create("http://thishost/Fragment_identifier%23Examples")).toString());
85 Assert.assertEquals("http://thathost/foo%3Abar", URIUtils.rewriteURI(
86 URI.create("http://thishost/foo%3Abar"), target).toString());
87 }
88
89 @Test
90 public void testRewriteWithEmptyPath() throws Exception {
91 final HttpHost target = new HttpHost("thathost", -1);
92
93 Assert.assertEquals("http://thathost/", URIUtils.rewriteURI(
94 URI.create("http://thathost"), target, DROP_FRAGMENT_AND_NORMALIZE).toString());
95 Assert.assertEquals("http://thathost/", URIUtils.rewriteURI(
96 URI.create("http://thathost"), target, DROP_FRAGMENT).toString());
97 }
98
99
100 @Test
101 public void testRewritePort() throws Exception {
102 HttpHost target = new HttpHost("thathost", 8080);
103 Assert.assertEquals("http://thathost:8080/stuff", URIUtils.rewriteURI(
104 URI.create("http://thishost:80/stuff#crap"), target, DROP_FRAGMENT_AND_NORMALIZE).toString());
105 Assert.assertEquals("http://thathost:8080/stuff#crap", URIUtils.rewriteURI(
106 URI.create("http://thishost:80/stuff#crap"), target, NORMALIZE).toString());
107 target = new HttpHost("thathost", -1);
108 Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
109 URI.create("http://thishost:80/stuff#crap"), target, DROP_FRAGMENT_AND_NORMALIZE).toString());
110 Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI(
111 URI.create("http://thishost:80/stuff#crap"), target, NORMALIZE).toString());
112 }
113
114 @Test
115 public void testRewriteScheme() throws Exception {
116 final HttpHost target = new HttpHost("thathost", -1, "file");
117 Assert.assertEquals("file://thathost/stuff", URIUtils.rewriteURI(
118 URI.create("http://thishost:80/stuff#crap"), target, DROP_FRAGMENT_AND_NORMALIZE).toString());
119 }
120
121 @Test
122 public void testRewriteForRoute() throws Exception {
123
124 final HttpHost target1 = new HttpHost("foo", 80);
125 final HttpHost target2 = new HttpHost("foo", 443, "https");
126 final HttpHost proxy = new HttpHost("bar", 8888);
127
128
129 Assert.assertEquals(new URI("/test"), URIUtils
130 .rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(target1), true));
131
132
133 Assert.assertEquals(new URI("/"), URIUtils
134 .rewriteURIForRoute(new URI(""), new HttpRoute(target1), true));
135
136
137 Assert.assertEquals(new URI("http://foo/test"), URIUtils
138 .rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(target1, proxy), true));
139
140
141 Assert.assertEquals(new URI("http://foo:80/test"), URIUtils
142 .rewriteURIForRoute(new URI("/test"), new HttpRoute(target1, proxy), true));
143
144
145 Assert.assertEquals(new URI("/test"), URIUtils
146 .rewriteURIForRoute(new URI("https://foo:443/test"), new HttpRoute(target2, null, proxy, true), true));
147 }
148
149 @Test
150 public void testNormalization() {
151 Assert.assertEquals("example://a/b/c/%7Bfoo%7D", URIUtils.resolve(this.baseURI, "eXAMPLE://a/./b/../b/%63/%7bfoo%7d").toString());
152 Assert.assertEquals("http://www.example.com/%3C", URIUtils.resolve(this.baseURI, "http://www.example.com/%3c").toString());
153 Assert.assertEquals("http://www.example.com/", URIUtils.resolve(this.baseURI, "HTTP://www.EXAMPLE.com/").toString());
154 Assert.assertEquals("http://www.example.com/a%2F", URIUtils.resolve(this.baseURI, "http://www.example.com/a%2f").toString());
155 Assert.assertEquals("http://www.example.com/?q=%26", URIUtils.resolve(this.baseURI, "http://www.example.com/?q=%26").toString());
156 Assert.assertEquals("http://www.example.com/%23?q=%26", URIUtils.resolve(this.baseURI, "http://www.example.com/%23?q=%26").toString());
157 Assert.assertEquals("http://www.example.com/blah-(%20-blah-%20&%20-blah-%20)-blah/",
158 URIUtils.resolve(this.baseURI, "http://www.example.com/blah-%28%20-blah-%20%26%20-blah-%20%29-blah/").toString());
159 }
160
161 @Test
162 public void testResolve() {
163 Assert.assertEquals("g:h", URIUtils.resolve(this.baseURI, "g:h").toString());
164 Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "g").toString());
165 Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "./g").toString());
166 Assert.assertEquals("http://a/b/c/g/", URIUtils.resolve(this.baseURI, "g/").toString());
167 Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/g").toString());
168 Assert.assertEquals("http://g/", URIUtils.resolve(this.baseURI, "//g").toString());
169 Assert.assertEquals("http://a/b/c/d;p?y", URIUtils.resolve(this.baseURI, "?y").toString());
170 Assert.assertEquals("http://a/b/c/d;p?y#f", URIUtils.resolve(this.baseURI, "?y#f")
171 .toString());
172 Assert.assertEquals("http://a/b/c/g?y", URIUtils.resolve(this.baseURI, "g?y").toString());
173 Assert.assertEquals("http://a/b/c/d;p?q#s", URIUtils.resolve(this.baseURI, "#s")
174 .toString());
175 Assert.assertEquals("http://a/b/c/g#s", URIUtils.resolve(this.baseURI, "g#s").toString());
176 Assert.assertEquals("http://a/b/c/g?y#s", URIUtils.resolve(this.baseURI, "g?y#s")
177 .toString());
178 Assert.assertEquals("http://a/b/c/;x", URIUtils.resolve(this.baseURI, ";x").toString());
179 Assert.assertEquals("http://a/b/c/g;x", URIUtils.resolve(this.baseURI, "g;x").toString());
180 Assert.assertEquals("http://a/b/c/g;x?y#s", URIUtils.resolve(this.baseURI, "g;x?y#s")
181 .toString());
182 Assert.assertEquals("http://a/b/c/d;p?q", URIUtils.resolve(this.baseURI, "").toString());
183 Assert.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, ".").toString());
184 Assert.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, "./").toString());
185 Assert.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "..").toString());
186 Assert.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "../").toString());
187 Assert.assertEquals("http://a/b/g", URIUtils.resolve(this.baseURI, "../g").toString());
188 Assert.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../..").toString());
189 Assert.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../../").toString());
190 Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../g").toString());
191 Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../../g").toString());
192 Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../../../g")
193 .toString());
194 Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/./g").toString());
195 Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/../g").toString());
196 Assert.assertEquals("http://a/b/c/g.", URIUtils.resolve(this.baseURI, "g.").toString());
197 Assert.assertEquals("http://a/b/c/.g", URIUtils.resolve(this.baseURI, ".g").toString());
198 Assert.assertEquals("http://a/b/c/g..", URIUtils.resolve(this.baseURI, "g..").toString());
199 Assert.assertEquals("http://a/b/c/..g", URIUtils.resolve(this.baseURI, "..g").toString());
200 Assert.assertEquals("http://a/b/g", URIUtils.resolve(this.baseURI, "./../g").toString());
201 Assert.assertEquals("http://a/b/c/g/", URIUtils.resolve(this.baseURI, "./g/.").toString());
202 Assert.assertEquals("http://a/b/c/g/h", URIUtils.resolve(this.baseURI, "g/./h").toString());
203 Assert.assertEquals("http://a/b/c/h", URIUtils.resolve(this.baseURI, "g/../h").toString());
204 Assert.assertEquals("http://a/b/c/g;x=1/y", URIUtils.resolve(this.baseURI, "g;x=1/./y")
205 .toString());
206 Assert.assertEquals("http://a/b/c/y", URIUtils.resolve(this.baseURI, "g;x=1/../y")
207 .toString());
208 Assert.assertEquals("http://a/b/c/g?y/./x", URIUtils.resolve(this.baseURI, "g?y/./x")
209 .toString());
210 Assert.assertEquals("http://a/b/c/g?y/../x", URIUtils.resolve(this.baseURI, "g?y/../x")
211 .toString());
212 Assert.assertEquals("http://a/b/c/g#s/./x", URIUtils.resolve(this.baseURI, "g#s/./x")
213 .toString());
214 Assert.assertEquals("http://a/b/c/g#s/../x", URIUtils.resolve(this.baseURI, "g#s/../x")
215 .toString());
216 Assert.assertEquals("http:g", URIUtils.resolve(this.baseURI, "http:g").toString());
217
218 Assert.assertEquals("http://s/a/g", URIUtils.resolve(this.baseURI,
219 "http://s/a/b/c/./../../g").toString());
220 Assert.assertEquals("http://s/mid/6", URIUtils.resolve(this.baseURI,
221 "http://s/mid/content=5/../6").toString());
222 }
223
224 @Test
225 public void testResolveOpaque() {
226 Assert.assertEquals("example://a/b/c/%7Bfoo%7D", URIUtils.resolve(this.baseURI, "eXAMPLE://a/./b/../b/%63/%7bfoo%7d").toString());
227 Assert.assertEquals("file://localhost/etc/fstab", URIUtils.resolve(this.baseURI, "file://localhost/etc/fstab").toString());
228 Assert.assertEquals("file:///etc/fstab", URIUtils.resolve(this.baseURI, "file:///etc/fstab").toString());
229 Assert.assertEquals("file://localhost/c:/WINDOWS/clock.avi", URIUtils.resolve(this.baseURI, "file://localhost/c:/WINDOWS/clock.avi").toString());
230 Assert.assertEquals("file:///c:/WINDOWS/clock.avi", URIUtils.resolve(this.baseURI, "file:///c:/WINDOWS/clock.avi").toString());
231 Assert.assertEquals("file://hostname/path/to/the%20file.txt", URIUtils.resolve(this.baseURI, "file://hostname/path/to/the%20file.txt").toString());
232 Assert.assertEquals("file:///c:/path/to/the%20file.txt", URIUtils.resolve(this.baseURI, "file:///c:/path/to/the%20file.txt").toString());
233 Assert.assertEquals("urn:issn:1535-3613", URIUtils.resolve(this.baseURI, "urn:issn:1535-3613").toString());
234 Assert.assertEquals("mailto:user@example.com", URIUtils.resolve(this.baseURI, "mailto:user@example.com").toString());
235 Assert.assertEquals("ftp://example.org/resource.txt", URIUtils.resolve(this.baseURI, "ftp://example.org/resource.txt").toString());
236 }
237
238 @Test
239 public void testExtractHost() throws Exception {
240 Assert.assertEquals(new HttpHost("localhost"),
241 URIUtils.extractHost(new URI("http://localhost/abcd")));
242 Assert.assertEquals(new HttpHost("localhost"),
243 URIUtils.extractHost(new URI("http://localhost/abcd%3A")));
244
245 Assert.assertEquals(new HttpHost("local_host"),
246 URIUtils.extractHost(new URI("http://local_host/abcd")));
247 Assert.assertEquals(new HttpHost("local_host"),
248 URIUtils.extractHost(new URI("http://local_host/abcd%3A")));
249
250 Assert.assertEquals(new HttpHost("localhost",8),
251 URIUtils.extractHost(new URI("http://localhost:8/abcd")));
252 Assert.assertEquals(new HttpHost("local_host",8),
253 URIUtils.extractHost(new URI("http://local_host:8/abcd")));
254
255
256 Assert.assertEquals(new HttpHost("localhost",-1),URIUtils.extractHost(
257 new URI("http://localhost:/abcd")));
258 Assert.assertEquals(new HttpHost("local_host",-1),URIUtils.extractHost(
259 new URI("http://local_host:/abcd")));
260
261 Assert.assertEquals(new HttpHost("localhost",8080),
262 URIUtils.extractHost(new URI("http://user:pass@localhost:8080/abcd")));
263 Assert.assertEquals(new HttpHost("local_host",8080),
264 URIUtils.extractHost(new URI("http://user:pass@local_host:8080/abcd")));
265
266 Assert.assertEquals(new HttpHost("localhost",8080),URIUtils.extractHost(
267 new URI("http://@localhost:8080/abcd")));
268 Assert.assertEquals(new HttpHost("local_host",8080),URIUtils.extractHost(
269 new URI("http://@local_host:8080/abcd")));
270
271 Assert.assertEquals(new HttpHost("[2a00:1450:400c:c01::69]",8080),
272 URIUtils.extractHost(new URI("http://[2a00:1450:400c:c01::69]:8080/")));
273
274 Assert.assertEquals(new HttpHost("localhost",8080),
275 URIUtils.extractHost(new URI("http://localhost:8080/;sessionid=stuff/abcd")));
276 Assert.assertEquals(null,
277 URIUtils.extractHost(new URI("http://localhost:8080;sessionid=stuff/abcd")));
278 Assert.assertEquals(null,
279 URIUtils.extractHost(new URI("http://localhost:;sessionid=stuff/abcd")));
280 Assert.assertEquals(null,
281 URIUtils.extractHost(new URI("http://:80/robots.txt")));
282 Assert.assertEquals(null,
283 URIUtils.extractHost(new URI("http://some%20domain:80/robots.txt")));
284 Assert.assertEquals(null,
285 URIUtils.extractHost(new URI("http://blah@goggle.com:80@google.com/")));
286 }
287
288 @Test
289 public void testHttpLocationWithRelativeFragment() throws Exception {
290 final HttpHost target = new HttpHost("localhost", -1, "http");
291 final URI requestURI = new URI("/stuff#blahblah");
292
293 final URI location = URIUtils.resolve(requestURI, target, null);
294 final URI expectedURI = new URIBuilder(requestURI)
295 .setHost(target.getHostName())
296 .setScheme(target.getSchemeName())
297 .build();
298 Assert.assertEquals(expectedURI, location);
299 }
300
301 @Test
302 public void testHttpLocationWithAbsoluteFragment() throws Exception {
303 final HttpHost target = new HttpHost("localhost", 80, "http");
304
305 final URI requestURI = new URIBuilder()
306 .setHost(target.getHostName())
307 .setScheme(target.getSchemeName())
308 .setPath("/stuff")
309 .setFragment("blahblah")
310 .build();
311
312 final URI location = URIUtils.resolve(requestURI, target, null);
313 final URI expectedURI = requestURI;
314 Assert.assertEquals(expectedURI, location);
315 }
316
317 @Test
318 public void testHttpLocationRedirect() throws Exception {
319 final HttpHost target = new HttpHost("localhost", -1, "http");
320 final URI requestURI = new URI("/People.htm#tim");
321
322 final URI redirect = new URI("http://localhost/people.html");
323
324 final URI location = URIUtils.resolve(requestURI, target, Arrays.asList(redirect));
325 final URI expectedURI = new URIBuilder()
326 .setHost(target.getHostName())
327 .setScheme(target.getSchemeName())
328 .setPath("/people.html")
329 .setFragment("tim")
330 .build();
331 Assert.assertEquals(expectedURI, location);
332 }
333
334 @Test
335 public void testHttpLocationWithRedirectFragment() throws Exception {
336 final HttpHost target = new HttpHost("localhost", -1, "http");
337 final URI requestURI = new URI("/~tim");
338
339 final URI redirect1 = new URI("http://localhost/People.htm#tim");
340 final URI redirect2 = new URI("http://localhost/people.html");
341
342 final URI location = URIUtils.resolve(requestURI, target, Arrays.asList(redirect1, redirect2));
343 final URI expectedURI = new URIBuilder()
344 .setHost(target.getHostName())
345 .setScheme(target.getSchemeName())
346 .setPath("/people.html")
347 .setFragment("tim")
348 .build();
349 Assert.assertEquals(expectedURI, location);
350 }
351
352 }