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