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         Assertions.assertEquals(new HttpHost("local_host",8080),
149                 URIUtils.extractHost(new URI("http://user:pass@local_host:8080/abcd")));
150 
151         Assertions.assertEquals(new HttpHost("localhost",8080),URIUtils.extractHost(
152                 new URI("http://@localhost:8080/abcd")));
153         Assertions.assertEquals(new HttpHost("local_host",8080),URIUtils.extractHost(
154                 new URI("http://@local_host:8080/abcd")));
155 
156         Assertions.assertEquals(new HttpHost("2a00:1450:400c:c01::69",8080),
157                 URIUtils.extractHost(new URI("http://[2a00:1450:400c:c01::69]:8080/")));
158 
159         Assertions.assertEquals(new HttpHost("localhost",8080),
160                 URIUtils.extractHost(new URI("http://localhost:8080/;sessionid=stuff/abcd")));
161         Assertions.assertNull(URIUtils.extractHost(new URI("http://localhost:8080;sessionid=stuff/abcd")));
162         Assertions.assertNull(URIUtils.extractHost(new URI("http://localhost:;sessionid=stuff/abcd")));
163         Assertions.assertNull(URIUtils.extractHost(new URI("http://:80/robots.txt")));
164         Assertions.assertNull(URIUtils.extractHost(new URI("http://some%20domain:80/robots.txt")));
165     }
166 
167     @Test
168     public void testHttpLocationWithRelativeFragment() throws Exception {
169         final HttpHost target = new HttpHost("http", "localhost", -1);
170         final URI requestURI = new URI("/stuff#blahblah");
171 
172         final URI location = URIUtils.resolve(requestURI, target, null);
173         final URI expectedURI = new URIBuilder(requestURI)
174                 .setHost(target.getHostName())
175                 .setScheme(target.getSchemeName())
176                 .build();
177         Assertions.assertEquals(expectedURI, location);
178     }
179 
180     @Test
181     public void testHttpLocationWithAbsoluteFragment() throws Exception {
182         final HttpHost target = new HttpHost("http", "localhost", 80);
183 
184         final URI requestURI = new URIBuilder()
185             .setHost(target.getHostName())
186             .setScheme(target.getSchemeName())
187             .setPath("/stuff")
188             .setFragment("blahblah")
189             .build();
190 
191         final URI location = URIUtils.resolve(requestURI, target, null);
192         final URI expectedURI = requestURI;
193         Assertions.assertEquals(expectedURI, location);
194     }
195 
196     @Test
197     public void testHttpLocationRedirect() throws Exception {
198         final HttpHost target = new HttpHost("http", "localhost", -1);
199         final URI requestURI = new URI("/People.htm#tim");
200 
201         final URI redirect = new URI("http://localhost/people.html");
202 
203         final URI location = URIUtils.resolve(requestURI, target, Collections.singletonList(redirect));
204         final URI expectedURI = new URIBuilder()
205                 .setHost(target.getHostName())
206                 .setScheme(target.getSchemeName())
207                 .setPath("/people.html")
208                 .setFragment("tim")
209                 .build();
210         Assertions.assertEquals(expectedURI, location);
211     }
212 
213     @Test
214     public void testHttpLocationWithRedirectFragment() throws Exception {
215         final HttpHost target = new HttpHost("http", "localhost", -1);
216         final URI requestURI = new URI("/~tim");
217 
218         final URI redirect1 = new URI("http://localhost/People.htm#tim");
219         final URI redirect2 = new URI("http://localhost/people.html");
220 
221         final URI location = URIUtils.resolve(requestURI, target, Arrays.asList(redirect1, redirect2));
222         final URI expectedURI = new URIBuilder()
223                 .setHost(target.getHostName())
224                 .setScheme(target.getSchemeName())
225                 .setPath("/people.html")
226                 .setFragment("tim")
227                 .build();
228         Assertions.assertEquals(expectedURI, location);
229     }
230 
231 }