1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestURI.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 18:29:41 +0000 (Sat, 22 Dec 2012) $
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import org.apache.commons.httpclient.methods.GetMethod;
34  
35  import junit.framework.Test;
36  import junit.framework.TestCase;
37  import junit.framework.TestSuite;
38  
39  /***
40   * Simple tests for the URI class.
41   * 
42   * @author Michael Becke
43   */
44  public class TestURI extends TestCase {
45  
46      /***
47       * Constructor for TestURI.
48       * @param testName
49       */
50      public TestURI(String testName) {
51          super(testName);
52      }
53      
54      public static Test suite() {
55          return new TestSuite(TestURI.class);
56      }
57      
58      public void testIPv4Address() throws URIException {
59  
60          URI base = new URI("http://10.0.1.10:8830", false);
61          
62          URI uri = base;        
63          assertTrue("Should be an IPv4 address", uri.isIPv4address());
64              
65          uri = new URI(base, "/04-1.html", false);
66          assertTrue("Should be an IPv4 address", uri.isIPv4address());
67  
68          uri = new URI("/04-1.html", false);
69          assertFalse("Should NOT be an IPv4 address", uri.isIPv4address());
70  
71          uri = new URI(base, "http://10.0.1.10:8830/04-1.html", false);
72          assertTrue("Should be an IPv4 address", uri.isIPv4address());
73  
74          uri = new URI("http://10.0.1.10:8830/04-1.html", false);
75          assertTrue("Should be an IPv4 address", uri.isIPv4address());
76  
77          uri = new URI(base, "http://host.org/04-1.html", false);
78          assertFalse("Should NOT be an IPv4 address", uri.isIPv4address());
79  
80          uri = new URI("http://host.org/04-1.html", false);
81          assertFalse("Should NOT be an IPv4 address", uri.isIPv4address());
82          
83      }
84      
85      public void testUrl() throws URIException {
86          URI url = new HttpURL("http://jakarta.apache.org");
87          assertEquals(80, url.getPort());
88          assertEquals("http", url.getScheme());
89          
90          url = new HttpsURL("https://jakarta.apache.org");
91          assertEquals(443, url.getPort());
92          assertEquals("https", url.getScheme());
93      }
94      
95      /***
96       * Tests the URI(URI, String) constructor.  This tests URIs ability to
97       * resolve relative URIs.
98       */
99      public void testRelativeURIConstructor() {
100         
101         URI baseURI = null;
102         
103         try {
104             baseURI = new URI("http://a/b/c/d;p?q", false);
105         } catch ( URIException e ) {
106             fail( "unable to create base URI: " + e );
107         }
108         
109         // the following is an array of arrays in the following order
110         // relative URI, scheme, host(authority), path, query, fragment, abs. URI
111         //
112         // these examples were taken from rfc 2396
113         String[][] testRelativeURIs = {
114             { "g:h", "g", null, "h", null, null, "g:h" },
115             { "g", "http", "a", "/b/c/g", null, null, "http://a/b/c/g" },
116             { "./g", "http", "a", "/b/c/g", null, null, "http://a/b/c/g" },
117             { "g/", "http", "a", "/b/c/g/", null, null, "http://a/b/c/g/" },
118             { "/g", "http", "a", "/g", null, null, "http://a/g" },
119             { "//g", "http", "g", null, null, null, "http://g" },
120             { "?y", "http", "a", "/b/c/d;p", "y", null, "http://a/b/c/d;p?y" },
121             { "g?y", "http", "a", "/b/c/g", "y", null, "http://a/b/c/g?y" },
122             { "#s", "http", "a", "/b/c/d;p", "q", "s", "http://a/b/c/d;p?q#s" },
123             { "#", "http", "a", "/b/c/d;p", "q", "", "http://a/b/c/d;p?q#" },
124             { "", "http", "a", "/b/c/d;p", "q", null, "http://a/b/c/d;p?q" },
125             { "g#s", "http", "a", "/b/c/g", null, "s", "http://a/b/c/g#s" },
126             { "g?y#s","http", "a", "/b/c/g", "y", "s", "http://a/b/c/g?y#s" },
127             { ";x", "http", "a", "/b/c/;x", null, null, "http://a/b/c/;x" },
128             { "g;x", "http", "a", "/b/c/g;x", null, null, "http://a/b/c/g;x" },
129             { "g;x?y#s", "http", "a", "/b/c/g;x", "y", "s", "http://a/b/c/g;x?y#s" },
130             { ".", "http", "a", "/b/c/", null, null, "http://a/b/c/" },
131             { "./", "http", "a", "/b/c/", null, null, "http://a/b/c/" },
132             { "..", "http", "a", "/b/", null, null, "http://a/b/" },
133             { "../", "http", "a", "/b/", null, null, "http://a/b/" },
134             { "../g", "http", "a", "/b/g", null, null, "http://a/b/g" },
135             { "../..", "http", "a", "/", null, null, "http://a/" },
136             { "../../", "http", "a", "/", null, null, "http://a/" },
137             { "../../g", "http", "a", "/g", null, null, "http://a/g" },
138             { "../../../g", "http", "a", "/g", null, null, "http://a/g" },
139             { "../../../../g", "http", "a", "/g", null, null, "http://a/g" },
140             { "/./g", "http", "a", "/g", null, null, "http://a/g" },
141             { "/../g", "http", "a", "/g", null, null, "http://a/g" },
142             { "g.", "http", "a", "/b/c/g.", null, null, "http://a/b/c/g." },
143             { ".g", "http", "a", "/b/c/.g", null, null, "http://a/b/c/.g" },
144             { "g..", "http", "a", "/b/c/g..", null, null, "http://a/b/c/g.." },
145             { "..g", "http", "a", "/b/c/..g", null, null, "http://a/b/c/..g" },
146             { "./../g", "http", "a", "/b/g", null, null, "http://a/b/g" },
147             { "./g/.", "http", "a", "/b/c/g/", null, null, "http://a/b/c/g/" },
148             { "g/./h", "http", "a", "/b/c/g/h", null, null, "http://a/b/c/g/h" },
149             { "g/../h", "http", "a", "/b/c/h", null, null, "http://a/b/c/h" },
150             { "g;x=1/./y", "http", "a", "/b/c/g;x=1/y", null, null, "http://a/b/c/g;x=1/y" },
151             { "g;x=1/../y", "http", "a", "/b/c/y", null, null, "http://a/b/c/y" },
152             { "g?y/./x", "http", "a", "/b/c/g", "y/./x", null, "http://a/b/c/g?y/./x" },
153             { "g?y/../x", "http", "a", "/b/c/g", "y/../x", null, "http://a/b/c/g?y/../x" },
154             { "g#s/./x", "http", "a", "/b/c/g", null, "s/./x", "http://a/b/c/g#s/./x" },
155             { "g#s/../x", "http", "a", "/b/c/g", null, "s/../x", "http://a/b/c/g#s/../x" },
156             { ":g", "http", "a", "/b/c/:g", null, null, "http://a/b/c/:g" }, // see issue #35148
157             { "//a/b/c", "http", "a", "/b/c", null, null, "http://a/b/c" } // see HTTPCLIENT-580 
158         };
159         for (int i = 0; i < testRelativeURIs.length; i++) {
160             URI testURI = null;
161             
162             try {
163                 testURI = new URI( baseURI, testRelativeURIs[i][0], false );
164             } catch ( URIException e ) {
165                 e.printStackTrace();
166                 fail( 
167                     "unable to create URI with relative value(" 
168                     + testRelativeURIs[i][0] + "): " + e 
169                 );   
170             }
171             
172             try {
173                 assertEquals("array index "+i, testRelativeURIs[i][1], testURI.getScheme());
174                 assertEquals("array index "+i, testRelativeURIs[i][2], testURI.getAuthority());
175                 assertEquals("array index "+i, testRelativeURIs[i][3], testURI.getPath());
176                 assertEquals("array index "+i, testRelativeURIs[i][4], testURI.getQuery());
177                 assertEquals("array index "+i, testRelativeURIs[i][5], testURI.getFragment());
178                 assertEquals("array index "+i, testRelativeURIs[i][6], testURI.getURIReference());
179             } catch ( URIException e ) {
180                 fail( "error getting URI property: " + e );
181             }            
182         }
183         
184     }
185 
186     public void testTestURIAuthorityString() throws Exception {
187         URI url = new URI("ftp", "user:password", "localhost", -1, "/");
188         assertEquals("ftp://user:password@localhost/", url.toString());
189         assertEquals("user:password@localhost", url.getAuthority());
190     }
191     
192     public void testTestHttpUrlAuthorityString() throws Exception {
193         HttpURL url = new HttpURL("localhost", -1, "/");
194         assertEquals("http://localhost/", url.toString());
195         url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
196         assertEquals("http://localhost/", url.toString());
197         assertEquals("user:password@localhost", url.getAuthority());
198 
199         url = new HttpURL("user#@", "pass#@", "localhost", 8080, "/");
200         assertEquals("http://localhost:8080/", url.toString());
201         assertEquals("user#@:pass#@", url.getUserinfo());
202         assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
203 
204         url = new HttpURL("user%23%40:pass%23%40", "localhost", 8080, "/");
205         assertEquals("http://localhost:8080/", url.toString());
206         assertEquals("user#@:pass#@", url.getUserinfo());
207         assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
208         
209         url = new HttpURL("localhost", 8080, "/");
210         assertEquals("http://localhost:8080/", url.toString());
211         url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
212         assertEquals("http://localhost:8080/", url.toString());
213         assertEquals("user:password@localhost:8080", url.getAuthority());
214     }
215     
216     public void testTestHttpsUrlAuthorityString() throws Exception {
217         HttpsURL url = new HttpsURL("localhost", -1, "/");
218         assertEquals("https://localhost/", url.toString());
219         url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
220         assertEquals("https://localhost/", url.toString());
221         assertEquals("user:password@localhost", url.getAuthority());
222 
223         url = new HttpsURL("user#@", "pass#@", "localhost", 8080, "/");
224         assertEquals("https://localhost:8080/", url.toString());
225         assertEquals("user#@:pass#@", url.getUserinfo());
226         assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
227         
228         url = new HttpsURL("user%23%40:pass%23%40", "localhost", 8080, "/");
229         assertEquals("https://localhost:8080/", url.toString());
230         assertEquals("user#@:pass#@", url.getUserinfo());
231         assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());        
232         
233         url = new HttpsURL("localhost", 8080, "/");
234         assertEquals("https://localhost:8080/", url.toString());
235         url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
236         assertEquals("https://localhost:8080/", url.toString());
237         assertEquals("user:password@localhost:8080", url.getAuthority());
238         
239     }
240 
241     public void testURIEscaping() throws Exception {
242         String escaped = "http://some.host.com/%41.html";
243         String unescaped = "http://some.host.com/A.html";
244         URI u1 = new URI(escaped, true);
245         GetMethod method = new GetMethod();
246         method.setURI(u1);
247         URI u2 = method.getURI();
248 
249         assertEquals(escaped, u1.toString());
250         assertEquals(escaped, new String(u1.getRawURI()));
251         assertEquals(unescaped, u1.getURI());
252         assertEquals(escaped, u2.toString());
253         assertEquals(escaped, new String(u2.getRawURI()));
254         assertEquals(unescaped, u2.getURI());        
255     }
256 
257     public void testBug578() throws Exception {
258         HttpURL url = new HttpURL("http://localhost/test+test");
259         assertEquals("/test+test", url.getPath());
260     }
261     
262     public void testVariousCharacters() throws Exception {
263         verifyInvalidURI("http://authority:123/path/path?query&name=val ue");
264         verifyInvalidURI("http://authority:123/path/path?query&na me=value");
265         verifyInvalidURI("http://authority:123/path/path?qu ery&name=value");
266         verifyInvalidURI("http://authority:123/path/pa th?query&name=value");
267         verifyInvalidURI("http://authority:123/pa th/path?query&name=value");
268         verifyInvalidURI("http://authority:12 3/path/path?query&name=value");
269         verifyInvalidURI("http://autho rity:123/path/path?query&name=value");
270         verifyInvalidURI("htt p://authority:123/path/path?query&name=value");
271     }
272     
273     private void verifyInvalidURI(String uri) {
274         try {
275             new URI(uri, true);
276             fail("should have thrown URIException");
277         } catch(URIException e) {
278             /* expected */
279         }
280     }    
281     
282     /***
283      * Verify proper handling of relative URIs which have a scheme. 
284      * See bug http://issues.apache.org/jira/browse/HTTPCLIENT-587
285      * 
286      * @throws Exception
287      */
288     public void testRelativeWithScheme() throws Exception {
289         URI base = new URI("http://www.example.com/some/path", true);
290         URI rel1 = new URI("http:", true);
291         URI rel2 = new URI("http:foo", true);
292         URI rel3 = new URI("http:../../bar", true);
293         URI derel1 = new URI(base, rel1);
294         assertEquals("http://www.example.com/some/path",derel1.toString());
295         URI derel2 = new URI(base, rel2);
296         assertEquals("http://www.example.com/some/foo",derel2.toString());
297         URI derel3 = new URI(base,rel3);
298         assertEquals("http://www.example.com/bar",derel3.toString());
299     }
300     
301     /***
302      * Verify proper handling of relative URIs with embedded double-slashes,
303      * like "foo//bar//baz". 
304      * See bug http://issues.apache.org/jira/browse/HTTPCLIENT-588
305      * 
306      * @throws Exception
307      */
308     public void testRelativeWithDoubleSlash() throws Exception {
309         URI rel = new URI("foo//bar//baz",true);
310         assertEquals("foo//bar//baz",rel.toString());
311     }
312     
313 }