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