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