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.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.jupiter.api.Assertions;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * This TestCase contains test methods for URI resolving according to RFC 3986.
40   * The examples are listed in section "5.4 Reference Resolution Examples"
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 testResolve() {
48          Assertions.assertEquals("g:h", URIUtils.resolve(this.baseURI, "g:h").toString());
49          Assertions.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "g").toString());
50          Assertions.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "./g").toString());
51          Assertions.assertEquals("http://a/b/c/g/", URIUtils.resolve(this.baseURI, "g/").toString());
52          Assertions.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/g").toString());
53          Assertions.assertEquals("http://g/", URIUtils.resolve(this.baseURI, "//g").toString());
54          Assertions.assertEquals("http://a/b/c/d;p?y", URIUtils.resolve(this.baseURI, "?y").toString());
55          Assertions.assertEquals("http://a/b/c/d;p?y#f", URIUtils.resolve(this.baseURI, "?y#f")
56                  .toString());
57          Assertions.assertEquals("http://a/b/c/g?y", URIUtils.resolve(this.baseURI, "g?y").toString());
58          Assertions.assertEquals("http://a/b/c/d;p?q#s", URIUtils.resolve(this.baseURI, "#s")
59                  .toString());
60          Assertions.assertEquals("http://a/b/c/g#s", URIUtils.resolve(this.baseURI, "g#s").toString());
61          Assertions.assertEquals("http://a/b/c/g?y#s", URIUtils.resolve(this.baseURI, "g?y#s")
62                  .toString());
63          Assertions.assertEquals("http://a/b/c/;x", URIUtils.resolve(this.baseURI, ";x").toString());
64          Assertions.assertEquals("http://a/b/c/g;x", URIUtils.resolve(this.baseURI, "g;x").toString());
65          Assertions.assertEquals("http://a/b/c/g;x?y#s", URIUtils.resolve(this.baseURI, "g;x?y#s")
66                  .toString());
67          Assertions.assertEquals("http://a/b/c/d;p?q", URIUtils.resolve(this.baseURI, "").toString());
68          Assertions.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, ".").toString());
69          Assertions.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, "./").toString());
70          Assertions.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "..").toString());
71          Assertions.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "../").toString());
72          Assertions.assertEquals("http://a/b/g", URIUtils.resolve(this.baseURI, "../g").toString());
73          Assertions.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../..").toString());
74          Assertions.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../../").toString());
75          Assertions.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../g").toString());
76          Assertions.assertEquals("http://a/../g", URIUtils.resolve(this.baseURI, "../../../g").toString());
77          Assertions.assertEquals("http://a/../../g", URIUtils.resolve(this.baseURI, "../../../../g")
78                  .toString());
79          Assertions.assertEquals("http://a/./g", URIUtils.resolve(this.baseURI, "/./g").toString());
80          Assertions.assertEquals("http://a/../g", URIUtils.resolve(this.baseURI, "/../g").toString());
81          Assertions.assertEquals("http://a/b/c/g.", URIUtils.resolve(this.baseURI, "g.").toString());
82          Assertions.assertEquals("http://a/b/c/.g", URIUtils.resolve(this.baseURI, ".g").toString());
83          Assertions.assertEquals("http://a/b/c/g..", URIUtils.resolve(this.baseURI, "g..").toString());
84          Assertions.assertEquals("http://a/b/c/..g", URIUtils.resolve(this.baseURI, "..g").toString());
85          Assertions.assertEquals("http://a/b/g", URIUtils.resolve(this.baseURI, "./../g").toString());
86          Assertions.assertEquals("http://a/b/c/g/", URIUtils.resolve(this.baseURI, "./g/.").toString());
87          Assertions.assertEquals("http://a/b/c/g/h", URIUtils.resolve(this.baseURI, "g/./h").toString());
88          Assertions.assertEquals("http://a/b/c/h", URIUtils.resolve(this.baseURI, "g/../h").toString());
89          Assertions.assertEquals("http://a/b/c/g;x=1/y", URIUtils.resolve(this.baseURI, "g;x=1/./y")
90                  .toString());
91          Assertions.assertEquals("http://a/b/c/y", URIUtils.resolve(this.baseURI, "g;x=1/../y")
92                  .toString());
93          Assertions.assertEquals("http://a/b/c/g?y/./x", URIUtils.resolve(this.baseURI, "g?y/./x")
94                  .toString());
95          Assertions.assertEquals("http://a/b/c/g?y/../x", URIUtils.resolve(this.baseURI, "g?y/../x")
96                  .toString());
97          Assertions.assertEquals("http://a/b/c/g#s/./x", URIUtils.resolve(this.baseURI, "g#s/./x")
98                  .toString());
99          Assertions.assertEquals("http://a/b/c/g#s/../x", URIUtils.resolve(this.baseURI, "g#s/../x")
100                 .toString());
101         Assertions.assertEquals("http:g", URIUtils.resolve(this.baseURI, "http:g").toString());
102         // examples from section 5.2.4
103         Assertions.assertEquals("http://s/a/b/c/./../../g", URIUtils.resolve(this.baseURI,
104                 "http://s/a/b/c/./../../g").toString());
105         Assertions.assertEquals("http://s/mid/content=5/../6", URIUtils.resolve(this.baseURI,
106                 "http://s/mid/content=5/../6").toString());
107     }
108 
109     @Test
110     public void testResolveOpaque() {
111         Assertions.assertEquals("example://a/./b/../b/%63/%7bfoo%7d", URIUtils.resolve(this.baseURI, "eXAMPLE://a/./b/../b/%63/%7bfoo%7d").toString());
112         Assertions.assertEquals("file://localhost/etc/fstab", URIUtils.resolve(this.baseURI, "file://localhost/etc/fstab").toString());
113         Assertions.assertEquals("file:///etc/fstab", URIUtils.resolve(this.baseURI, "file:///etc/fstab").toString());
114         Assertions.assertEquals("file://localhost/c:/WINDOWS/clock.avi", URIUtils.resolve(this.baseURI, "file://localhost/c:/WINDOWS/clock.avi").toString());
115         Assertions.assertEquals("file:///c:/WINDOWS/clock.avi", URIUtils.resolve(this.baseURI, "file:///c:/WINDOWS/clock.avi").toString());
116         Assertions.assertEquals("file://hostname/path/to/the%20file.txt", URIUtils.resolve(this.baseURI, "file://hostname/path/to/the%20file.txt").toString());
117         Assertions.assertEquals("file:///c:/path/to/the%20file.txt", URIUtils.resolve(this.baseURI, "file:///c:/path/to/the%20file.txt").toString());
118         Assertions.assertEquals("urn:issn:1535-3613", URIUtils.resolve(this.baseURI, "urn:issn:1535-3613").toString());
119         Assertions.assertEquals("mailto:user@example.com", URIUtils.resolve(this.baseURI, "mailto:user@example.com").toString());
120         Assertions.assertEquals("ftp://example.org/resource.txt", URIUtils.resolve(this.baseURI, "ftp://example.org/resource.txt").toString());
121     }
122 
123     @Test
124     public void testExtractHost() throws Exception {
125         Assertions.assertEquals(new HttpHost("localhost"),
126                 URIUtils.extractHost(new URI("http://localhost/abcd")));
127         Assertions.assertEquals(new HttpHost("localhost"),
128                 URIUtils.extractHost(new URI("http://localhost/abcd%3A")));
129 
130         Assertions.assertEquals(new HttpHost("local_host"),
131                 URIUtils.extractHost(new URI("http://local_host/abcd")));
132         Assertions.assertEquals(new HttpHost("local_host"),
133                 URIUtils.extractHost(new URI("http://local_host/abcd%3A")));
134 
135         Assertions.assertEquals(new HttpHost("localhost",8),
136                 URIUtils.extractHost(new URI("http://localhost:8/abcd")));
137         Assertions.assertEquals(new HttpHost("local_host",8),
138                 URIUtils.extractHost(new URI("http://local_host:8/abcd")));
139 
140         // URI seems to OK with missing port number
141         Assertions.assertEquals(new HttpHost("localhost",-1),URIUtils.extractHost(
142                 new URI("http://localhost:/abcd")));
143         Assertions.assertEquals(new HttpHost("local_host",-1),URIUtils.extractHost(
144                 new URI("http://local_host:/abcd")));
145 
146         Assertions.assertEquals(new HttpHost("localhost",8080),
147                 URIUtils.extractHost(new URI("http://user:pass@localhost:8080/abcd")));
148 
149         Assertions.assertEquals(new HttpHost("local_host",8080),
150                 URIUtils.extractHost(new URI("http://user:pass@local_host:8080/abcd")));
151 
152         Assertions.assertEquals(new HttpHost("localhost",8080),URIUtils.extractHost(
153                 new URI("http://@localhost:8080/abcd")));
154         Assertions.assertEquals(new HttpHost("local_host",8080),URIUtils.extractHost(
155                 new URI("http://@local_host:8080/abcd")));
156 
157         Assertions.assertEquals(new HttpHost("2a00:1450:400c:c01::69",8080),
158                 URIUtils.extractHost(new URI("http://[2a00:1450:400c:c01::69]:8080/")));
159 
160         Assertions.assertEquals(new HttpHost("localhost",8080),
161                 URIUtils.extractHost(new URI("http://localhost:8080/;sessionid=stuff/abcd")));
162         Assertions.assertNull(URIUtils.extractHost(new URI("http://localhost:8080;sessionid=stuff/abcd")));
163         Assertions.assertNull(URIUtils.extractHost(new URI("http://localhost:;sessionid=stuff/abcd")));
164         Assertions.assertNull(URIUtils.extractHost(new URI("http://:80/robots.txt")));
165         Assertions.assertNull(URIUtils.extractHost(new URI("http://some%20domain:80/robots.txt")));
166     }
167 
168     @Test
169     public void testHttpLocationWithRelativeFragment() throws Exception {
170         final HttpHost target = new HttpHost("http", "localhost", -1);
171         final URI requestURI = new URI("/stuff#blahblah");
172 
173         final URI location = URIUtils.resolve(requestURI, target, null);
174         final URI expectedURI = new URIBuilder(requestURI)
175                 .setHost(target.getHostName())
176                 .setScheme(target.getSchemeName())
177                 .build();
178         Assertions.assertEquals(expectedURI, location);
179     }
180 
181     @Test
182     public void testHttpLocationWithAbsoluteFragment() throws Exception {
183         final HttpHost target = new HttpHost("http", "localhost", 80);
184 
185         final URI requestURI = new URIBuilder()
186             .setHost(target.getHostName())
187             .setScheme(target.getSchemeName())
188             .setPath("/stuff")
189             .setFragment("blahblah")
190             .build();
191 
192         final URI location = URIUtils.resolve(requestURI, target, null);
193         final URI expectedURI = requestURI;
194         Assertions.assertEquals(expectedURI, location);
195     }
196 
197     @Test
198     public void testHttpLocationRedirect() throws Exception {
199         final HttpHost target = new HttpHost("http", "localhost", -1);
200         final URI requestURI = new URI("/People.htm#tim");
201 
202         final URI redirect = new URI("http://localhost/people.html");
203 
204         final URI location = URIUtils.resolve(requestURI, target, Collections.singletonList(redirect));
205         final URI expectedURI = new URIBuilder()
206                 .setHost(target.getHostName())
207                 .setScheme(target.getSchemeName())
208                 .setPath("/people.html")
209                 .setFragment("tim")
210                 .build();
211         Assertions.assertEquals(expectedURI, location);
212     }
213 
214     @Test
215     public void testHttpLocationWithRedirectFragment() throws Exception {
216         final HttpHost target = new HttpHost("http", "localhost", -1);
217         final URI requestURI = new URI("/~tim");
218 
219         final URI redirect1 = new URI("http://localhost/People.htm#tim");
220         final URI redirect2 = new URI("http://localhost/people.html");
221 
222         final URI location = URIUtils.resolve(requestURI, target, Arrays.asList(redirect1, redirect2));
223         final URI expectedURI = new URIBuilder()
224                 .setHost(target.getHostName())
225                 .setScheme(target.getSchemeName())
226                 .setPath("/people.html")
227                 .setFragment("tim")
228                 .build();
229         Assertions.assertEquals(expectedURI, location);
230     }
231 
232 }