1 31 32 package org.apache.commons.httpclient; 33 34 import junit.framework.Test; 35 import junit.framework.TestSuite; 36 37 42 public class TestURI extends TestNoHost { 43 44 48 public TestURI(String testName) { 49 super(testName); 50 } 51 52 public static Test suite() { 53 return new TestSuite(TestURI.class); 54 } 55 56 public void testIPv4Address() throws URIException { 57 58 URI base = new URI("http://10.0.1.10:8830"); 59 60 URI uri = base; 61 assertTrue("Should be an IPv4 address", uri.isIPv4address()); 62 63 uri = new URI(base, "/04-1.html"); 64 assertTrue("Should be an IPv4 address", uri.isIPv4address()); 65 66 uri = new URI("/04-1.html"); 67 assertFalse("Should NOT be an IPv4 address", uri.isIPv4address()); 68 69 uri = new URI(base, "http://10.0.1.10:8830/04-1.html"); 70 assertTrue("Should be an IPv4 address", uri.isIPv4address()); 71 72 uri = new URI("http://10.0.1.10:8830/04-1.html"); 73 assertTrue("Should be an IPv4 address", uri.isIPv4address()); 74 75 uri = new URI(base, "http://host.org/04-1.html"); 76 assertFalse("Should NOT be an IPv4 address", uri.isIPv4address()); 77 78 uri = new URI("http://host.org/04-1.html"); 79 assertFalse("Should NOT be an IPv4 address", uri.isIPv4address()); 80 81 } 82 83 public void testUrl() throws URIException { 84 URI url = new HttpURL("http://jakarta.apache.org"); 85 assertEquals(80, url.getPort()); 86 assertEquals("http", url.getScheme()); 87 88 url = new HttpsURL("https://jakarta.apache.org"); 89 assertEquals(443, url.getPort()); 90 assertEquals("https", url.getScheme()); 91 } 92 93 99 public void testRelativeURIConstructor() { 100 101 URI baseURI = null; 102 103 try { 104 baseURI = new URI( "http://a/b/c/d;p?q" ); 105 } catch ( URIException e ) { 106 fail( "unable to create base URI: " + e ); 107 } 108 109 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/", "y", null, "http://a/b/c/?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 }; 157 for (int i = 0; i < testRelativeURIs.length; i++) { 158 URI testURI = null; 159 160 try { 161 testURI = new URI( baseURI, testRelativeURIs[i][0] ); 162 } catch ( URIException e ) { 163 e.printStackTrace(); 164 fail( 165 "unable to create URI with relative value(" 166 + testRelativeURIs[i][0] + "): " + e 167 ); 168 } 169 170 try { 171 assertEquals( testURI.getScheme(), testRelativeURIs[i][1] ); 172 assertEquals( testURI.getAuthority(), testRelativeURIs[i][2] ); 173 assertEquals( testURI.getPath(), testRelativeURIs[i][3] ); 174 assertEquals( testURI.getQuery(), testRelativeURIs[i][4] ); 175 assertEquals( testURI.getFragment(), testRelativeURIs[i][5] ); 176 assertEquals( testURI.getURIReference(), testRelativeURIs[i][6] ); 177 } catch ( URIException e ) { 178 fail( "error getting URI property: " + e ); 179 } 180 } 181 182 } 183 184 public void testTestHttpUrlAuthorityString() throws Exception { 185 HttpURL url = new HttpURL("localhost", -1, "/"); 186 assertEquals("http://localhost/", url.toString()); 187 url.setRawUserinfo("user".toCharArray(), "password".toCharArray()); 188 assertEquals("http://localhost/", url.toString()); 189 assertEquals("user:password@localhost", url.getAuthority()); 190 191 url = new HttpURL("user", "pass#", "localhost", 8080, "/"); 192 assertEquals("http://localhost:8080/", url.toString()); 193 assertEquals("user:pass#", url.getUserinfo()); 194 assertEquals("user:pass%23", url.getEscapedUserinfo()); 195 196 url = new HttpURL("localhost", 8080, "/"); 197 assertEquals("http://localhost:8080/", url.toString()); 198 url.setRawUserinfo("user".toCharArray(), "password".toCharArray()); 199 assertEquals("http://localhost:8080/", url.toString()); 200 assertEquals("user:password@localhost:8080", url.getAuthority()); 201 } 202 203 public void testTestHttpsUrlAuthorityString() throws Exception { 204 HttpsURL url = new HttpsURL("localhost", -1, "/"); 205 assertEquals("https://localhost/", url.toString()); 206 url.setRawUserinfo("user".toCharArray(), "password".toCharArray()); 207 assertEquals("https://localhost/", url.toString()); 208 assertEquals("user:password@localhost", url.getAuthority()); 209 210 url = new HttpsURL("user", "pass#", "localhost", 8080, "/"); 211 assertEquals("https://localhost:8080/", url.toString()); 212 assertEquals("user:pass#", url.getUserinfo()); 213 assertEquals("user:pass%23", url.getEscapedUserinfo()); 214 215 url = new HttpsURL("localhost", 8080, "/"); 216 assertEquals("https://localhost:8080/", url.toString()); 217 url.setRawUserinfo("user".toCharArray(), "password".toCharArray()); 218 assertEquals("https://localhost:8080/", url.toString()); 219 assertEquals("user:password@localhost:8080", url.getAuthority()); 220 221 } 222 223 } 224 | Popular Tags |