View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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  /**
38   * This TestCase contains test methods for URI resolving according to RFC 3986.
39   * The examples are listed in section "5.4 Reference Resolution Examples"
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      }
82  
83      @Test
84      public void testRewritePort() throws Exception {
85          HttpHost target = new HttpHost("thathost", 8080); // port should be copied
86          Assert.assertEquals("http://thathost:8080/stuff", URIUtils.rewriteURI(
87                  URI.create("http://thishost:80/stuff#crap"), target, true).toString());
88          Assert.assertEquals("http://thathost:8080/stuff#crap", URIUtils.rewriteURI(
89                  URI.create("http://thishost:80/stuff#crap"), target, false).toString());
90          target = new HttpHost("thathost", -1); // input port should be dropped
91          Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
92                  URI.create("http://thishost:80/stuff#crap"), target, true).toString());
93          Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI(
94                  URI.create("http://thishost:80/stuff#crap"), target, false).toString());
95      }
96  
97      @Test
98      public void testRewriteScheme() throws Exception {
99          final HttpHost target = new HttpHost("thathost", -1, "file"); // scheme should be copied
100         Assert.assertEquals("file://thathost/stuff", URIUtils.rewriteURI(
101                 URI.create("http://thishost:80/stuff#crap"), target, true).toString());
102     }
103 
104     @Test
105     public void testRewriteForRoute() throws Exception {
106 
107         final HttpHost target1 = new HttpHost("foo", 80);
108         final HttpHost target2 = new HttpHost("foo", 443, "https");
109         final HttpHost proxy = new HttpHost("bar", 8888);
110 
111         // Direct route
112         Assert.assertEquals(new URI("/test"), URIUtils
113                 .rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(target1)));
114 
115         // Direct route
116         Assert.assertEquals(new URI("/"), URIUtils
117                 .rewriteURIForRoute(new URI(""), new HttpRoute(target1)));
118 
119         // Via proxy
120         Assert.assertEquals(new URI("http://foo/test"), URIUtils
121                 .rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(target1, proxy)));
122 
123         // Via proxy
124         Assert.assertEquals(new URI("http://foo:80/test"), URIUtils
125                 .rewriteURIForRoute(new URI("/test"), new HttpRoute(target1, proxy)));
126 
127         // Via proxy tunnel
128         Assert.assertEquals(new URI("/test"), URIUtils
129                 .rewriteURIForRoute(new URI("https://foo:443/test"), new HttpRoute(target2, null, proxy, true)));
130     }
131 
132     @Test
133     public void testNormalization() {
134         Assert.assertEquals("example://a/b/c/%7Bfoo%7D", URIUtils.resolve(this.baseURI, "eXAMPLE://a/./b/../b/%63/%7bfoo%7d").toString());
135         Assert.assertEquals("http://www.example.com/%3C", URIUtils.resolve(this.baseURI, "http://www.example.com/%3c").toString());
136         Assert.assertEquals("http://www.example.com/", URIUtils.resolve(this.baseURI, "HTTP://www.EXAMPLE.com/").toString());
137         Assert.assertEquals("http://www.example.com/a/", URIUtils.resolve(this.baseURI, "http://www.example.com/a%2f").toString());
138         Assert.assertEquals("http://www.example.com/?q=%26", URIUtils.resolve(this.baseURI, "http://www.example.com/?q=%26").toString());
139         Assert.assertEquals("http://www.example.com/%23?q=%26", URIUtils.resolve(this.baseURI, "http://www.example.com/%23?q=%26").toString());
140         Assert.assertEquals("http://www.example.com/blah-(%20-blah-%20&%20-blah-%20)-blah/",
141                 URIUtils.resolve(this.baseURI, "http://www.example.com/blah-%28%20-blah-%20%26%20-blah-%20%29-blah/").toString());
142     }
143 
144     @Test
145     public void testResolve() {
146         Assert.assertEquals("g:h", URIUtils.resolve(this.baseURI, "g:h").toString());
147         Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "g").toString());
148         Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "./g").toString());
149         Assert.assertEquals("http://a/b/c/g/", URIUtils.resolve(this.baseURI, "g/").toString());
150         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/g").toString());
151         Assert.assertEquals("http://g/", URIUtils.resolve(this.baseURI, "//g").toString());
152         Assert.assertEquals("http://a/b/c/d;p?y", URIUtils.resolve(this.baseURI, "?y").toString());
153         Assert.assertEquals("http://a/b/c/d;p?y#f", URIUtils.resolve(this.baseURI, "?y#f")
154                 .toString());
155         Assert.assertEquals("http://a/b/c/g?y", URIUtils.resolve(this.baseURI, "g?y").toString());
156         Assert.assertEquals("http://a/b/c/d;p?q#s", URIUtils.resolve(this.baseURI, "#s")
157                 .toString());
158         Assert.assertEquals("http://a/b/c/g#s", URIUtils.resolve(this.baseURI, "g#s").toString());
159         Assert.assertEquals("http://a/b/c/g?y#s", URIUtils.resolve(this.baseURI, "g?y#s")
160                 .toString());
161         Assert.assertEquals("http://a/b/c/;x", URIUtils.resolve(this.baseURI, ";x").toString());
162         Assert.assertEquals("http://a/b/c/g;x", URIUtils.resolve(this.baseURI, "g;x").toString());
163         Assert.assertEquals("http://a/b/c/g;x?y#s", URIUtils.resolve(this.baseURI, "g;x?y#s")
164                 .toString());
165         Assert.assertEquals("http://a/b/c/d;p?q", URIUtils.resolve(this.baseURI, "").toString());
166         Assert.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, ".").toString());
167         Assert.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, "./").toString());
168         Assert.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "..").toString());
169         Assert.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "../").toString());
170         Assert.assertEquals("http://a/b/g", URIUtils.resolve(this.baseURI, "../g").toString());
171         Assert.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../..").toString());
172         Assert.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../../").toString());
173         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../g").toString());
174         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../../g").toString());
175         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../../../g")
176                 .toString());
177         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/./g").toString());
178         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/../g").toString());
179         Assert.assertEquals("http://a/b/c/g.", URIUtils.resolve(this.baseURI, "g.").toString());
180         Assert.assertEquals("http://a/b/c/.g", URIUtils.resolve(this.baseURI, ".g").toString());
181         Assert.assertEquals("http://a/b/c/g..", URIUtils.resolve(this.baseURI, "g..").toString());
182         Assert.assertEquals("http://a/b/c/..g", URIUtils.resolve(this.baseURI, "..g").toString());
183         Assert.assertEquals("http://a/b/g", URIUtils.resolve(this.baseURI, "./../g").toString());
184         Assert.assertEquals("http://a/b/c/g/", URIUtils.resolve(this.baseURI, "./g/.").toString());
185         Assert.assertEquals("http://a/b/c/g/h", URIUtils.resolve(this.baseURI, "g/./h").toString());
186         Assert.assertEquals("http://a/b/c/h", URIUtils.resolve(this.baseURI, "g/../h").toString());
187         Assert.assertEquals("http://a/b/c/g;x=1/y", URIUtils.resolve(this.baseURI, "g;x=1/./y")
188                 .toString());
189         Assert.assertEquals("http://a/b/c/y", URIUtils.resolve(this.baseURI, "g;x=1/../y")
190                 .toString());
191         Assert.assertEquals("http://a/b/c/g?y/./x", URIUtils.resolve(this.baseURI, "g?y/./x")
192                 .toString());
193         Assert.assertEquals("http://a/b/c/g?y/../x", URIUtils.resolve(this.baseURI, "g?y/../x")
194                 .toString());
195         Assert.assertEquals("http://a/b/c/g#s/./x", URIUtils.resolve(this.baseURI, "g#s/./x")
196                 .toString());
197         Assert.assertEquals("http://a/b/c/g#s/../x", URIUtils.resolve(this.baseURI, "g#s/../x")
198                 .toString());
199         Assert.assertEquals("http:g", URIUtils.resolve(this.baseURI, "http:g").toString());
200         // examples from section 5.2.4
201         Assert.assertEquals("http://s/a/g", URIUtils.resolve(this.baseURI,
202                 "http://s/a/b/c/./../../g").toString());
203         Assert.assertEquals("http://s/mid/6", URIUtils.resolve(this.baseURI,
204                 "http://s/mid/content=5/../6").toString());
205     }
206 
207     @Test
208     public void testResolveOpaque() {
209         Assert.assertEquals("example://a/b/c/%7Bfoo%7D", URIUtils.resolve(this.baseURI, "eXAMPLE://a/./b/../b/%63/%7bfoo%7d").toString());
210         Assert.assertEquals("file://localhost/etc/fstab", URIUtils.resolve(this.baseURI, "file://localhost/etc/fstab").toString());
211         Assert.assertEquals("file:///etc/fstab", URIUtils.resolve(this.baseURI, "file:///etc/fstab").toString());
212         Assert.assertEquals("file://localhost/c:/WINDOWS/clock.avi", URIUtils.resolve(this.baseURI, "file://localhost/c:/WINDOWS/clock.avi").toString());
213         Assert.assertEquals("file:///c:/WINDOWS/clock.avi", URIUtils.resolve(this.baseURI, "file:///c:/WINDOWS/clock.avi").toString());
214         Assert.assertEquals("file://hostname/path/to/the%20file.txt", URIUtils.resolve(this.baseURI, "file://hostname/path/to/the%20file.txt").toString());
215         Assert.assertEquals("file:///c:/path/to/the%20file.txt", URIUtils.resolve(this.baseURI, "file:///c:/path/to/the%20file.txt").toString());
216         Assert.assertEquals("urn:issn:1535-3613", URIUtils.resolve(this.baseURI, "urn:issn:1535-3613").toString());
217         Assert.assertEquals("mailto:user@example.com", URIUtils.resolve(this.baseURI, "mailto:user@example.com").toString());
218         Assert.assertEquals("ftp://example.org/resource.txt", URIUtils.resolve(this.baseURI, "ftp://example.org/resource.txt").toString());
219     }
220 
221     @Test
222     public void testExtractHost() throws Exception {
223         Assert.assertEquals(new HttpHost("localhost"),
224                 URIUtils.extractHost(new URI("http://localhost/abcd")));
225         Assert.assertEquals(new HttpHost("localhost"),
226                 URIUtils.extractHost(new URI("http://localhost/abcd%3A")));
227 
228         Assert.assertEquals(new HttpHost("local_host"),
229                 URIUtils.extractHost(new URI("http://local_host/abcd")));
230         Assert.assertEquals(new HttpHost("local_host"),
231                 URIUtils.extractHost(new URI("http://local_host/abcd%3A")));
232 
233         Assert.assertEquals(new HttpHost("localhost",8),
234                 URIUtils.extractHost(new URI("http://localhost:8/abcd")));
235         Assert.assertEquals(new HttpHost("local_host",8),
236                 URIUtils.extractHost(new URI("http://local_host:8/abcd")));
237 
238         // URI seems to OK with missing port number
239         Assert.assertEquals(new HttpHost("localhost",-1),URIUtils.extractHost(
240                 new URI("http://localhost:/abcd")));
241         Assert.assertEquals(new HttpHost("local_host",-1),URIUtils.extractHost(
242                 new URI("http://local_host:/abcd")));
243 
244         Assert.assertEquals(new HttpHost("localhost",8080),
245                 URIUtils.extractHost(new URI("http://user:pass@localhost:8080/abcd")));
246         Assert.assertEquals(new HttpHost("local_host",8080),
247                 URIUtils.extractHost(new URI("http://user:pass@local_host:8080/abcd")));
248 
249         Assert.assertEquals(new HttpHost("localhost",8080),URIUtils.extractHost(
250                 new URI("http://@localhost:8080/abcd")));
251         Assert.assertEquals(new HttpHost("local_host",8080),URIUtils.extractHost(
252                 new URI("http://@local_host:8080/abcd")));
253 
254         Assert.assertEquals(new HttpHost("[2a00:1450:400c:c01::69]",8080),
255                 URIUtils.extractHost(new URI("http://[2a00:1450:400c:c01::69]:8080/")));
256 
257         Assert.assertEquals(new HttpHost("localhost",8080),
258                 URIUtils.extractHost(new URI("http://localhost:8080/;sessionid=stuff/abcd")));
259         Assert.assertEquals(new HttpHost("localhost",8080),
260                 URIUtils.extractHost(new URI("http://localhost:8080;sessionid=stuff/abcd")));
261         Assert.assertEquals(new HttpHost("localhost",-1),
262                 URIUtils.extractHost(new URI("http://localhost:;sessionid=stuff/abcd")));
263         Assert.assertEquals(null,
264                 URIUtils.extractHost(new URI("http://:80/robots.txt")));
265         Assert.assertEquals(null,
266                 URIUtils.extractHost(new URI("http://some%20domain:80/robots.txt")));
267     }
268 
269     @Test
270     public void testHttpLocationWithRelativeFragment() throws Exception {
271         final HttpHost target = new HttpHost("localhost", -1, "http");
272         final URI requestURI = new URI("/stuff#blahblah");
273 
274         final URI location = URIUtils.resolve(requestURI, target, null);
275         final URI expectedURI = new URIBuilder(requestURI)
276                 .setHost(target.getHostName())
277                 .setScheme(target.getSchemeName())
278                 .build();
279         Assert.assertEquals(expectedURI, location);
280     }
281 
282     @Test
283     public void testHttpLocationWithAbsoluteFragment() throws Exception {
284         final HttpHost target = new HttpHost("localhost", 80, "http");
285 
286         final URI requestURI = new URIBuilder()
287             .setHost(target.getHostName())
288             .setScheme(target.getSchemeName())
289             .setPath("/stuff")
290             .setFragment("blahblah")
291             .build();
292 
293         final URI location = URIUtils.resolve(requestURI, target, null);
294         final URI expectedURI = requestURI;
295         Assert.assertEquals(expectedURI, location);
296     }
297 
298     @Test
299     public void testHttpLocationRedirect() throws Exception {
300         final HttpHost target = new HttpHost("localhost", -1, "http");
301         final URI requestURI = new URI("/People.htm#tim");
302 
303         final URI redirect = new URI("http://localhost/people.html");
304 
305         final URI location = URIUtils.resolve(requestURI, target, Arrays.asList(redirect));
306         final URI expectedURI = new URIBuilder()
307                 .setHost(target.getHostName())
308                 .setScheme(target.getSchemeName())
309                 .setPath("/people.html")
310                 .setFragment("tim")
311                 .build();
312         Assert.assertEquals(expectedURI, location);
313     }
314 
315     @Test
316     public void testHttpLocationWithRedirectFragment() throws Exception {
317         final HttpHost target = new HttpHost("localhost", -1, "http");
318         final URI requestURI = new URI("/~tim");
319 
320         final URI redirect1 = new URI("http://localhost/People.htm#tim");
321         final URI redirect2 = new URI("http://localhost/people.html");
322 
323         final URI location = URIUtils.resolve(requestURI, target, Arrays.asList(redirect1, redirect2));
324         final URI expectedURI = new URIBuilder()
325                 .setHost(target.getHostName())
326                 .setScheme(target.getSchemeName())
327                 .setPath("/people.html")
328                 .setFragment("tim")
329                 .build();
330         Assert.assertEquals(expectedURI, location);
331     }
332 
333 }