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     }
141 
142     @Test
143     public void testResolve() {
144         Assert.assertEquals("g:h", URIUtils.resolve(this.baseURI, "g:h").toString());
145         Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "g").toString());
146         Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "./g").toString());
147         Assert.assertEquals("http://a/b/c/g/", URIUtils.resolve(this.baseURI, "g/").toString());
148         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/g").toString());
149         Assert.assertEquals("http://g/", URIUtils.resolve(this.baseURI, "//g").toString());
150         Assert.assertEquals("http://a/b/c/d;p?y", URIUtils.resolve(this.baseURI, "?y").toString());
151         Assert.assertEquals("http://a/b/c/d;p?y#f", URIUtils.resolve(this.baseURI, "?y#f")
152                 .toString());
153         Assert.assertEquals("http://a/b/c/g?y", URIUtils.resolve(this.baseURI, "g?y").toString());
154         Assert.assertEquals("http://a/b/c/d;p?q#s", URIUtils.resolve(this.baseURI, "#s")
155                 .toString());
156         Assert.assertEquals("http://a/b/c/g#s", URIUtils.resolve(this.baseURI, "g#s").toString());
157         Assert.assertEquals("http://a/b/c/g?y#s", URIUtils.resolve(this.baseURI, "g?y#s")
158                 .toString());
159         Assert.assertEquals("http://a/b/c/;x", URIUtils.resolve(this.baseURI, ";x").toString());
160         Assert.assertEquals("http://a/b/c/g;x", URIUtils.resolve(this.baseURI, "g;x").toString());
161         Assert.assertEquals("http://a/b/c/g;x?y#s", URIUtils.resolve(this.baseURI, "g;x?y#s")
162                 .toString());
163         Assert.assertEquals("http://a/b/c/d;p?q", URIUtils.resolve(this.baseURI, "").toString());
164         Assert.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, ".").toString());
165         Assert.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, "./").toString());
166         Assert.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "..").toString());
167         Assert.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "../").toString());
168         Assert.assertEquals("http://a/b/g", URIUtils.resolve(this.baseURI, "../g").toString());
169         Assert.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../..").toString());
170         Assert.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../../").toString());
171         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../g").toString());
172         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../../g").toString());
173         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../../../g")
174                 .toString());
175         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/./g").toString());
176         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/../g").toString());
177         Assert.assertEquals("http://a/b/c/g.", URIUtils.resolve(this.baseURI, "g.").toString());
178         Assert.assertEquals("http://a/b/c/.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/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/c/g/h", URIUtils.resolve(this.baseURI, "g/./h").toString());
184         Assert.assertEquals("http://a/b/c/h", URIUtils.resolve(this.baseURI, "g/../h").toString());
185         Assert.assertEquals("http://a/b/c/g;x=1/y", URIUtils.resolve(this.baseURI, "g;x=1/./y")
186                 .toString());
187         Assert.assertEquals("http://a/b/c/y", URIUtils.resolve(this.baseURI, "g;x=1/../y")
188                 .toString());
189         Assert.assertEquals("http://a/b/c/g?y/./x", URIUtils.resolve(this.baseURI, "g?y/./x")
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#s/./x", URIUtils.resolve(this.baseURI, "g#s/./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:g", URIUtils.resolve(this.baseURI, "http:g").toString());
198         // examples from section 5.2.4
199         Assert.assertEquals("http://s/a/g", URIUtils.resolve(this.baseURI,
200                 "http://s/a/b/c/./../../g").toString());
201         Assert.assertEquals("http://s/mid/6", URIUtils.resolve(this.baseURI,
202                 "http://s/mid/content=5/../6").toString());
203     }
204 
205     @Test
206     public void testResolveOpaque() {
207         Assert.assertEquals("example://a/b/c/%7Bfoo%7D", URIUtils.resolve(this.baseURI, "eXAMPLE://a/./b/../b/%63/%7bfoo%7d").toString());
208         Assert.assertEquals("file://localhost/etc/fstab", URIUtils.resolve(this.baseURI, "file://localhost/etc/fstab").toString());
209         Assert.assertEquals("file:///etc/fstab", URIUtils.resolve(this.baseURI, "file:///etc/fstab").toString());
210         Assert.assertEquals("file://localhost/c:/WINDOWS/clock.avi", URIUtils.resolve(this.baseURI, "file://localhost/c:/WINDOWS/clock.avi").toString());
211         Assert.assertEquals("file:///c:/WINDOWS/clock.avi", URIUtils.resolve(this.baseURI, "file:///c:/WINDOWS/clock.avi").toString());
212         Assert.assertEquals("file://hostname/path/to/the%20file.txt", URIUtils.resolve(this.baseURI, "file://hostname/path/to/the%20file.txt").toString());
213         Assert.assertEquals("file:///c:/path/to/the%20file.txt", URIUtils.resolve(this.baseURI, "file:///c:/path/to/the%20file.txt").toString());
214         Assert.assertEquals("urn:issn:1535-3613", URIUtils.resolve(this.baseURI, "urn:issn:1535-3613").toString());
215         Assert.assertEquals("mailto:user@example.com", URIUtils.resolve(this.baseURI, "mailto:user@example.com").toString());
216         Assert.assertEquals("ftp://example.org/resource.txt", URIUtils.resolve(this.baseURI, "ftp://example.org/resource.txt").toString());
217     }
218 
219     @Test
220     public void testExtractHost() throws Exception {
221         Assert.assertEquals(new HttpHost("localhost"),
222                 URIUtils.extractHost(new URI("http://localhost/abcd")));
223         Assert.assertEquals(new HttpHost("localhost"),
224                 URIUtils.extractHost(new URI("http://localhost/abcd%3A")));
225 
226         Assert.assertEquals(new HttpHost("local_host"),
227                 URIUtils.extractHost(new URI("http://local_host/abcd")));
228         Assert.assertEquals(new HttpHost("local_host"),
229                 URIUtils.extractHost(new URI("http://local_host/abcd%3A")));
230 
231         Assert.assertEquals(new HttpHost("localhost",8),
232                 URIUtils.extractHost(new URI("http://localhost:8/abcd")));
233         Assert.assertEquals(new HttpHost("local_host",8),
234                 URIUtils.extractHost(new URI("http://local_host:8/abcd")));
235 
236         // URI seems to OK with missing port number
237         Assert.assertEquals(new HttpHost("localhost",-1),URIUtils.extractHost(
238                 new URI("http://localhost:/abcd")));
239         Assert.assertEquals(new HttpHost("local_host",-1),URIUtils.extractHost(
240                 new URI("http://local_host:/abcd")));
241 
242         Assert.assertEquals(new HttpHost("localhost",8080),
243                 URIUtils.extractHost(new URI("http://user:pass@localhost:8080/abcd")));
244         Assert.assertEquals(new HttpHost("local_host",8080),
245                 URIUtils.extractHost(new URI("http://user:pass@local_host:8080/abcd")));
246 
247         Assert.assertEquals(new HttpHost("localhost",8080),URIUtils.extractHost(
248                 new URI("http://@localhost:8080/abcd")));
249         Assert.assertEquals(new HttpHost("local_host",8080),URIUtils.extractHost(
250                 new URI("http://@local_host:8080/abcd")));
251 
252         Assert.assertEquals(new HttpHost("[2a00:1450:400c:c01::69]",8080),
253                 URIUtils.extractHost(new URI("http://[2a00:1450:400c:c01::69]:8080/")));
254 
255         Assert.assertEquals(new HttpHost("localhost",8080),
256                 URIUtils.extractHost(new URI("http://localhost:8080/;sessionid=stuff/abcd")));
257         Assert.assertEquals(new HttpHost("localhost",8080),
258                 URIUtils.extractHost(new URI("http://localhost:8080;sessionid=stuff/abcd")));
259         Assert.assertEquals(new HttpHost("localhost",-1),
260                 URIUtils.extractHost(new URI("http://localhost:;sessionid=stuff/abcd")));
261         Assert.assertEquals(null,
262                 URIUtils.extractHost(new URI("http://:80/robots.txt")));
263         Assert.assertEquals(null,
264                 URIUtils.extractHost(new URI("http://some%20domain:80/robots.txt")));
265     }
266 
267     @Test
268     public void testHttpLocationWithRelativeFragment() throws Exception {
269         final HttpHost target = new HttpHost("localhost", -1, "http");
270         final URI requestURI = new URI("/stuff#blahblah");
271 
272         final URI location = URIUtils.resolve(requestURI, target, null);
273         final URI expectedURI = new URIBuilder(requestURI)
274                 .setHost(target.getHostName())
275                 .setScheme(target.getSchemeName())
276                 .build();
277         Assert.assertEquals(expectedURI, location);
278     }
279 
280     @Test
281     public void testHttpLocationWithAbsoluteFragment() throws Exception {
282         final HttpHost target = new HttpHost("localhost", 80, "http");
283 
284         final URI requestURI = new URIBuilder()
285             .setHost(target.getHostName())
286             .setScheme(target.getSchemeName())
287             .setPath("/stuff")
288             .setFragment("blahblah")
289             .build();
290 
291         final URI location = URIUtils.resolve(requestURI, target, null);
292         final URI expectedURI = requestURI;
293         Assert.assertEquals(expectedURI, location);
294     }
295 
296     @Test
297     public void testHttpLocationRedirect() throws Exception {
298         final HttpHost target = new HttpHost("localhost", -1, "http");
299         final URI requestURI = new URI("/People.htm#tim");
300 
301         final URI redirect = new URI("http://localhost/people.html");
302 
303         final URI location = URIUtils.resolve(requestURI, target, Arrays.asList(redirect));
304         final URI expectedURI = new URIBuilder()
305                 .setHost(target.getHostName())
306                 .setScheme(target.getSchemeName())
307                 .setPath("/people.html")
308                 .setFragment("tim")
309                 .build();
310         Assert.assertEquals(expectedURI, location);
311     }
312 
313     @Test
314     public void testHttpLocationWithRedirectFragment() throws Exception {
315         final HttpHost target = new HttpHost("localhost", -1, "http");
316         final URI requestURI = new URI("/~tim");
317 
318         final URI redirect1 = new URI("http://localhost/People.htm#tim");
319         final URI redirect2 = new URI("http://localhost/people.html");
320 
321         final URI location = URIUtils.resolve(requestURI, target, Arrays.asList(redirect1, redirect2));
322         final URI expectedURI = new URIBuilder()
323                 .setHost(target.getHostName())
324                 .setScheme(target.getSchemeName())
325                 .setPath("/people.html")
326                 .setFragment("tim")
327                 .build();
328         Assert.assertEquals(expectedURI, location);
329     }
330 
331 }