001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/test/org/apache/commons/httpclient/TestURI.java $
003: * $Revision: 564973 $
004: * $Date: 2007-08-11 22:51:47 +0200 (Sat, 11 Aug 2007) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import org.apache.commons.httpclient.methods.GetMethod;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: /**
040: * Simple tests for the URI class.
041: *
042: * @author Michael Becke
043: */
044: public class TestURI extends TestCase {
045:
046: /**
047: * Constructor for TestURI.
048: * @param testName
049: */
050: public TestURI(String testName) {
051: super (testName);
052: }
053:
054: public static Test suite() {
055: return new TestSuite(TestURI.class);
056: }
057:
058: public void testIPv4Address() throws URIException {
059:
060: URI base = new URI("http://10.0.1.10:8830", false);
061:
062: URI uri = base;
063: assertTrue("Should be an IPv4 address", uri.isIPv4address());
064:
065: uri = new URI(base, "/04-1.html", false);
066: assertTrue("Should be an IPv4 address", uri.isIPv4address());
067:
068: uri = new URI("/04-1.html", false);
069: assertFalse("Should NOT be an IPv4 address", uri
070: .isIPv4address());
071:
072: uri = new URI(base, "http://10.0.1.10:8830/04-1.html", false);
073: assertTrue("Should be an IPv4 address", uri.isIPv4address());
074:
075: uri = new URI("http://10.0.1.10:8830/04-1.html", false);
076: assertTrue("Should be an IPv4 address", uri.isIPv4address());
077:
078: uri = new URI(base, "http://host.org/04-1.html", false);
079: assertFalse("Should NOT be an IPv4 address", uri
080: .isIPv4address());
081:
082: uri = new URI("http://host.org/04-1.html", false);
083: assertFalse("Should NOT be an IPv4 address", uri
084: .isIPv4address());
085:
086: }
087:
088: public void testUrl() throws URIException {
089: URI url = new HttpURL("http://jakarta.apache.org");
090: assertEquals(80, url.getPort());
091: assertEquals("http", url.getScheme());
092:
093: url = new HttpsURL("https://jakarta.apache.org");
094: assertEquals(443, url.getPort());
095: assertEquals("https", url.getScheme());
096: }
097:
098: /**
099: * Tests the URI(URI, String) constructor. This tests URIs ability to
100: * resolve relative URIs.
101: */
102: public void testRelativeURIConstructor() {
103:
104: URI baseURI = null;
105:
106: try {
107: baseURI = new URI("http://a/b/c/d;p?q", false);
108: } catch (URIException e) {
109: fail("unable to create base URI: " + e);
110: }
111:
112: // the following is an array of arrays in the following order
113: // relative URI, scheme, host(authority), path, query, fragment, abs. URI
114: //
115: // these examples were taken from rfc 2396
116: String[][] testRelativeURIs = {
117: { "g:h", "g", null, "h", null, null, "g:h" },
118: { "g", "http", "a", "/b/c/g", null, null,
119: "http://a/b/c/g" },
120: { "./g", "http", "a", "/b/c/g", null, null,
121: "http://a/b/c/g" },
122: { "g/", "http", "a", "/b/c/g/", null, null,
123: "http://a/b/c/g/" },
124: { "/g", "http", "a", "/g", null, null, "http://a/g" },
125: { "//g", "http", "g", null, null, null, "http://g" },
126: { "?y", "http", "a", "/b/c/d;p", "y", null,
127: "http://a/b/c/d;p?y" },
128: { "g?y", "http", "a", "/b/c/g", "y", null,
129: "http://a/b/c/g?y" },
130: { "#s", "http", "a", "/b/c/d;p", "q", "s",
131: "http://a/b/c/d;p?q#s" },
132: { "#", "http", "a", "/b/c/d;p", "q", "",
133: "http://a/b/c/d;p?q#" },
134: { "", "http", "a", "/b/c/d;p", "q", null,
135: "http://a/b/c/d;p?q" },
136: { "g#s", "http", "a", "/b/c/g", null, "s",
137: "http://a/b/c/g#s" },
138: { "g?y#s", "http", "a", "/b/c/g", "y", "s",
139: "http://a/b/c/g?y#s" },
140: { ";x", "http", "a", "/b/c/;x", null, null,
141: "http://a/b/c/;x" },
142: { "g;x", "http", "a", "/b/c/g;x", null, null,
143: "http://a/b/c/g;x" },
144: { "g;x?y#s", "http", "a", "/b/c/g;x", "y", "s",
145: "http://a/b/c/g;x?y#s" },
146: { ".", "http", "a", "/b/c/", null, null,
147: "http://a/b/c/" },
148: { "./", "http", "a", "/b/c/", null, null,
149: "http://a/b/c/" },
150: { "..", "http", "a", "/b/", null, null, "http://a/b/" },
151: { "../", "http", "a", "/b/", null, null, "http://a/b/" },
152: { "../g", "http", "a", "/b/g", null, null,
153: "http://a/b/g" },
154: { "../..", "http", "a", "/", null, null, "http://a/" },
155: { "../../", "http", "a", "/", null, null, "http://a/" },
156: { "../../g", "http", "a", "/g", null, null,
157: "http://a/g" },
158: { "../../../g", "http", "a", "/g", null, null,
159: "http://a/g" },
160: { "../../../../g", "http", "a", "/g", null, null,
161: "http://a/g" },
162: { "/./g", "http", "a", "/g", null, null, "http://a/g" },
163: { "/../g", "http", "a", "/g", null, null, "http://a/g" },
164: { "g.", "http", "a", "/b/c/g.", null, null,
165: "http://a/b/c/g." },
166: { ".g", "http", "a", "/b/c/.g", null, null,
167: "http://a/b/c/.g" },
168: { "g..", "http", "a", "/b/c/g..", null, null,
169: "http://a/b/c/g.." },
170: { "..g", "http", "a", "/b/c/..g", null, null,
171: "http://a/b/c/..g" },
172: { "./../g", "http", "a", "/b/g", null, null,
173: "http://a/b/g" },
174: { "./g/.", "http", "a", "/b/c/g/", null, null,
175: "http://a/b/c/g/" },
176: { "g/./h", "http", "a", "/b/c/g/h", null, null,
177: "http://a/b/c/g/h" },
178: { "g/../h", "http", "a", "/b/c/h", null, null,
179: "http://a/b/c/h" },
180: { "g;x=1/./y", "http", "a", "/b/c/g;x=1/y", null, null,
181: "http://a/b/c/g;x=1/y" },
182: { "g;x=1/../y", "http", "a", "/b/c/y", null, null,
183: "http://a/b/c/y" },
184: { "g?y/./x", "http", "a", "/b/c/g", "y/./x", null,
185: "http://a/b/c/g?y/./x" },
186: { "g?y/../x", "http", "a", "/b/c/g", "y/../x", null,
187: "http://a/b/c/g?y/../x" },
188: { "g#s/./x", "http", "a", "/b/c/g", null, "s/./x",
189: "http://a/b/c/g#s/./x" },
190: { "g#s/../x", "http", "a", "/b/c/g", null, "s/../x",
191: "http://a/b/c/g#s/../x" },
192: { ":g", "http", "a", "/b/c/:g", null, null,
193: "http://a/b/c/:g" }, // see issue #35148
194: { "//a/b/c", "http", "a", "/b/c", null, null,
195: "http://a/b/c" } // see HTTPCLIENT-580
196: };
197: for (int i = 0; i < testRelativeURIs.length; i++) {
198: URI testURI = null;
199:
200: try {
201: testURI = new URI(baseURI, testRelativeURIs[i][0],
202: false);
203: } catch (URIException e) {
204: e.printStackTrace();
205: fail("unable to create URI with relative value("
206: + testRelativeURIs[i][0] + "): " + e);
207: }
208:
209: try {
210: assertEquals("array index " + i,
211: testRelativeURIs[i][1], testURI.getScheme());
212: assertEquals("array index " + i,
213: testRelativeURIs[i][2], testURI.getAuthority());
214: assertEquals("array index " + i,
215: testRelativeURIs[i][3], testURI.getPath());
216: assertEquals("array index " + i,
217: testRelativeURIs[i][4], testURI.getQuery());
218: assertEquals("array index " + i,
219: testRelativeURIs[i][5], testURI.getFragment());
220: assertEquals("array index " + i,
221: testRelativeURIs[i][6], testURI
222: .getURIReference());
223: } catch (URIException e) {
224: fail("error getting URI property: " + e);
225: }
226: }
227:
228: }
229:
230: public void testTestURIAuthorityString() throws Exception {
231: URI url = new URI("ftp", "user:password", "localhost", -1, "/");
232: assertEquals("ftp://user:password@localhost/", url.toString());
233: assertEquals("user:password@localhost", url.getAuthority());
234: }
235:
236: public void testTestHttpUrlAuthorityString() throws Exception {
237: HttpURL url = new HttpURL("localhost", -1, "/");
238: assertEquals("http://localhost/", url.toString());
239: url.setRawUserinfo("user".toCharArray(), "password"
240: .toCharArray());
241: assertEquals("http://localhost/", url.toString());
242: assertEquals("user:password@localhost", url.getAuthority());
243:
244: url = new HttpURL("user#@", "pass#@", "localhost", 8080, "/");
245: assertEquals("http://localhost:8080/", url.toString());
246: assertEquals("user#@:pass#@", url.getUserinfo());
247: assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
248:
249: url = new HttpURL("user%23%40:pass%23%40", "localhost", 8080,
250: "/");
251: assertEquals("http://localhost:8080/", url.toString());
252: assertEquals("user#@:pass#@", url.getUserinfo());
253: assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
254:
255: url = new HttpURL("localhost", 8080, "/");
256: assertEquals("http://localhost:8080/", url.toString());
257: url.setRawUserinfo("user".toCharArray(), "password"
258: .toCharArray());
259: assertEquals("http://localhost:8080/", url.toString());
260: assertEquals("user:password@localhost:8080", url.getAuthority());
261: }
262:
263: public void testTestHttpsUrlAuthorityString() throws Exception {
264: HttpsURL url = new HttpsURL("localhost", -1, "/");
265: assertEquals("https://localhost/", url.toString());
266: url.setRawUserinfo("user".toCharArray(), "password"
267: .toCharArray());
268: assertEquals("https://localhost/", url.toString());
269: assertEquals("user:password@localhost", url.getAuthority());
270:
271: url = new HttpsURL("user#@", "pass#@", "localhost", 8080, "/");
272: assertEquals("https://localhost:8080/", url.toString());
273: assertEquals("user#@:pass#@", url.getUserinfo());
274: assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
275:
276: url = new HttpsURL("user%23%40:pass%23%40", "localhost", 8080,
277: "/");
278: assertEquals("https://localhost:8080/", url.toString());
279: assertEquals("user#@:pass#@", url.getUserinfo());
280: assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
281:
282: url = new HttpsURL("localhost", 8080, "/");
283: assertEquals("https://localhost:8080/", url.toString());
284: url.setRawUserinfo("user".toCharArray(), "password"
285: .toCharArray());
286: assertEquals("https://localhost:8080/", url.toString());
287: assertEquals("user:password@localhost:8080", url.getAuthority());
288:
289: }
290:
291: public void testURIEscaping() throws Exception {
292: String escaped = "http://some.host.com/%41.html";
293: String unescaped = "http://some.host.com/A.html";
294: URI u1 = new URI(escaped, true);
295: GetMethod method = new GetMethod();
296: method.setURI(u1);
297: URI u2 = method.getURI();
298:
299: assertEquals(escaped, u1.toString());
300: assertEquals(escaped, new String(u1.getRawURI()));
301: assertEquals(unescaped, u1.getURI());
302: assertEquals(escaped, u2.toString());
303: assertEquals(escaped, new String(u2.getRawURI()));
304: assertEquals(unescaped, u2.getURI());
305: }
306:
307: public void testBug578() throws Exception {
308: HttpURL url = new HttpURL("http://localhost/test+test");
309: assertEquals("/test+test", url.getPath());
310: }
311:
312: public void testVariousCharacters() throws Exception {
313: verifyInvalidURI("http://authority:123/path/path?query&name=val ue");
314: verifyInvalidURI("http://authority:123/path/path?query&na me=value");
315: verifyInvalidURI("http://authority:123/path/path?qu ery&name=value");
316: verifyInvalidURI("http://authority:123/path/pa th?query&name=value");
317: verifyInvalidURI("http://authority:123/pa th/path?query&name=value");
318: verifyInvalidURI("http://authority:12 3/path/path?query&name=value");
319: verifyInvalidURI("http://autho rity:123/path/path?query&name=value");
320: verifyInvalidURI("htt p://authority:123/path/path?query&name=value");
321: }
322:
323: private void verifyInvalidURI(String uri) {
324: try {
325: new URI(uri, true);
326: fail("should have thrown URIException");
327: } catch (URIException e) {
328: /* expected */
329: }
330: }
331:
332: /**
333: * Verify proper handling of relative URIs which have a scheme.
334: * See bug http://issues.apache.org/jira/browse/HTTPCLIENT-587
335: *
336: * @throws Exception
337: */
338: public void testRelativeWithScheme() throws Exception {
339: URI base = new URI("http://www.example.com/some/path", true);
340: URI rel1 = new URI("http:", true);
341: URI rel2 = new URI("http:foo", true);
342: URI rel3 = new URI("http:../../bar", true);
343: URI derel1 = new URI(base, rel1);
344: assertEquals("http://www.example.com/some/path", derel1
345: .toString());
346: URI derel2 = new URI(base, rel2);
347: assertEquals("http://www.example.com/some/foo", derel2
348: .toString());
349: URI derel3 = new URI(base, rel3);
350: assertEquals("http://www.example.com/bar", derel3.toString());
351: }
352:
353: /**
354: * Verify proper handling of relative URIs with embedded double-slashes,
355: * like "foo//bar//baz".
356: * See bug http://issues.apache.org/jira/browse/HTTPCLIENT-588
357: *
358: * @throws Exception
359: */
360: public void testRelativeWithDoubleSlash() throws Exception {
361: URI rel = new URI("foo//bar//baz", true);
362: assertEquals("foo//bar//baz", rel.toString());
363: }
364:
365: }
|