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