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 };
157 for (int i = 0; i < testRelativeURIs.length; i++) {
158 URI testURI = null;
159
160 try {
161 testURI = new URI( baseURI, testRelativeURIs[i][0], false );
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("array index "+i, testRelativeURIs[i][1], testURI.getScheme());
172 assertEquals("array index "+i, testRelativeURIs[i][2], testURI.getAuthority());
173 assertEquals("array index "+i, testRelativeURIs[i][3], testURI.getPath());
174 assertEquals("array index "+i, testRelativeURIs[i][4], testURI.getQuery());
175 assertEquals("array index "+i, testRelativeURIs[i][5], testURI.getFragment());
176 assertEquals("array index "+i, testRelativeURIs[i][6], testURI.getURIReference());
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%23%40:pass%23%40", url.getEscapedUserinfo());
195
196 url = new HttpURL("user%23%40:pass%23%40", "localhost", 8080, "/");
197 assertEquals("http://localhost:8080/", url.toString());
198 assertEquals("user#@:pass#@", url.getUserinfo());
199 assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
200
201 url = new HttpURL("localhost", 8080, "/");
202 assertEquals("http://localhost:8080/", url.toString());
203 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
204 assertEquals("http://localhost:8080/", url.toString());
205 assertEquals("user:password@localhost:8080", url.getAuthority());
206 }
207
208 public void testTestHttpsUrlAuthorityString() throws Exception {
209 HttpsURL url = new HttpsURL("localhost", -1, "/");
210 assertEquals("https://localhost/", url.toString());
211 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
212 assertEquals("https://localhost/", url.toString());
213 assertEquals("user:password@localhost", url.getAuthority());
214
215 url = new HttpsURL("user#@", "pass#@", "localhost", 8080, "/");
216 assertEquals("https://localhost:8080/", url.toString());
217 assertEquals("user#@:pass#@", url.getUserinfo());
218 assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
219
220 url = new HttpsURL("user%23%40:pass%23%40", "localhost", 8080, "/");
221 assertEquals("https://localhost:8080/", url.toString());
222 assertEquals("user#@:pass#@", url.getUserinfo());
223 assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
224
225 url = new HttpsURL("localhost", 8080, "/");
226 assertEquals("https://localhost:8080/", url.toString());
227 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
228 assertEquals("https://localhost:8080/", url.toString());
229 assertEquals("user:password@localhost:8080", url.getAuthority());
230
231 }
232
233 public void testURIEscaping() throws Exception {
234 String escaped = "http://some.host.com/%41.html";
235 String unescaped = "http://some.host.com/A.html";
236 URI u1 = new URI(escaped, true);
237 GetMethod method = new GetMethod();
238 method.setURI(u1);
239 URI u2 = method.getURI();
240
241 assertEquals(escaped, u1.toString());
242 assertEquals(escaped, new String(u1.getRawURI()));
243 assertEquals(unescaped, u1.getURI());
244 assertEquals(escaped, u2.toString());
245 assertEquals(escaped, new String(u2.getRawURI()));
246 assertEquals(unescaped, u2.getURI());
247 }
248
249 }