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
31
32 package org.apache.commons.httpclient;
33
34 import org.apache.commons.httpclient.methods.GetMethod;
35
36 import junit.framework.Test;
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 TestNoHost {
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 * @see URI#URI(URI, String)
100 */
101 public void testRelativeURIConstructor() {
102
103 URI baseURI = null;
104
105 try {
106 baseURI = new URI("http://a/b/c/d;p?q", false);
107 } catch ( URIException e ) {
108 fail( "unable to create base URI: " + e );
109 }
110
111
112
113
114
115 String[][] testRelativeURIs = {
116 { "g:h", "g", null, "h", null, null, "g:h" },
117 { "g", "http", "a", "/b/c/g", null, null, "http://a/b/c/g" },
118 { "./g", "http", "a", "/b/c/g", null, null, "http://a/b/c/g" },
119 { "g/", "http", "a", "/b/c/g/", null, null, "http://a/b/c/g/" },
120 { "/g", "http", "a", "/g", null, null, "http://a/g" },
121 { "//g", "http", "g", null, null, null, "http://g" },
122 { "?y", "http", "a", "/b/c/", "y", null, "http://a/b/c/?y" },
123 { "g?y", "http", "a", "/b/c/g", "y", null, "http://a/b/c/g?y" },
124 { "#s", "http", "a", "/b/c/d;p", "q", "s", "http://a/b/c/d;p?q#s" },
125 { "#", "http", "a", "/b/c/d;p", "q", "", "http://a/b/c/d;p?q#" },
126 { "", "http", "a", "/b/c/d;p", "q", null, "http://a/b/c/d;p?q" },
127 { "g#s", "http", "a", "/b/c/g", null, "s", "http://a/b/c/g#s" },
128 { "g?y#s","http", "a", "/b/c/g", "y", "s", "http://a/b/c/g?y#s" },
129 { ";x", "http", "a", "/b/c/;x", null, null, "http://a/b/c/;x" },
130 { "g;x", "http", "a", "/b/c/g;x", null, null, "http://a/b/c/g;x" },
131 { "g;x?y#s", "http", "a", "/b/c/g;x", "y", "s", "http://a/b/c/g;x?y#s" },
132 { ".", "http", "a", "/b/c/", null, null, "http://a/b/c/" },
133 { "./", "http", "a", "/b/c/", null, null, "http://a/b/c/" },
134 { "..", "http", "a", "/b/", null, null, "http://a/b/" },
135 { "../", "http", "a", "/b/", null, null, "http://a/b/" },
136 { "../g", "http", "a", "/b/g", null, null, "http://a/b/g" },
137 { "../..", "http", "a", "/", null, null, "http://a/" },
138 { "../../", "http", "a", "/", null, null, "http://a/" },
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", "/g", null, null, "http://a/g" },
143 { "/../g", "http", "a", "/g", null, null, "http://a/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/c/g..", null, null, "http://a/b/c/g.." },
147 { "..g", "http", "a", "/b/c/..g", null, null, "http://a/b/c/..g" },
148 { "./../g", "http", "a", "/b/g", null, null, "http://a/b/g" },
149 { "./g/.", "http", "a", "/b/c/g/", null, null, "http://a/b/c/g/" },
150 { "g/./h", "http", "a", "/b/c/g/h", null, null, "http://a/b/c/g/h" },
151 { "g/../h", "http", "a", "/b/c/h", null, null, "http://a/b/c/h" },
152 { "g;x=1/./y", "http", "a", "/b/c/g;x=1/y", null, null, "http://a/b/c/g;x=1/y" },
153 { "g;x=1/../y", "http", "a", "/b/c/y", null, null, "http://a/b/c/y" },
154 { "g?y/./x", "http", "a", "/b/c/g", "y/./x", null, "http://a/b/c/g?y/./x" },
155 { "g?y/../x", "http", "a", "/b/c/g", "y/../x", null, "http://a/b/c/g?y/../x" },
156 { "g#s/./x", "http", "a", "/b/c/g", null, "s/./x", "http://a/b/c/g#s/./x" },
157 { "g#s/../x", "http", "a", "/b/c/g", null, "s/../x", "http://a/b/c/g#s/../x" }
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( testURI.getScheme(), testRelativeURIs[i][1] );
174 assertEquals( testURI.getAuthority(), testRelativeURIs[i][2] );
175 assertEquals( testURI.getPath(), testRelativeURIs[i][3] );
176 assertEquals( testURI.getQuery(), testRelativeURIs[i][4] );
177 assertEquals( testURI.getFragment(), testRelativeURIs[i][5] );
178 assertEquals( testURI.getURIReference(), testRelativeURIs[i][6] );
179 } catch ( URIException e ) {
180 fail( "error getting URI property: " + e );
181 }
182 }
183
184 }
185
186 public void testTestHttpUrlAuthorityString() throws Exception {
187 HttpURL url = new HttpURL("localhost", -1, "/");
188 assertEquals("http://localhost/", url.toString());
189 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
190 assertEquals("http://localhost/", url.toString());
191 assertEquals("user:password@localhost", url.getAuthority());
192
193 url = new HttpURL("user#@", "pass#@", "localhost", 8080, "/");
194 assertEquals("http://localhost:8080/", url.toString());
195 assertEquals("user#@:pass#@", url.getUserinfo());
196 assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
197
198 url = new HttpURL("user%23%40:pass%23%40", "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("localhost", 8080, "/");
204 assertEquals("http://localhost:8080/", url.toString());
205 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
206 assertEquals("http://localhost:8080/", url.toString());
207 assertEquals("user:password@localhost:8080", url.getAuthority());
208 }
209
210 public void testTestHttpsUrlAuthorityString() throws Exception {
211 HttpsURL url = new HttpsURL("localhost", -1, "/");
212 assertEquals("https://localhost/", url.toString());
213 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
214 assertEquals("https://localhost/", url.toString());
215 assertEquals("user:password@localhost", url.getAuthority());
216
217 url = new HttpsURL("user#@", "pass#@", "localhost", 8080, "/");
218 assertEquals("https://localhost:8080/", url.toString());
219 assertEquals("user#@:pass#@", url.getUserinfo());
220 assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
221
222 url = new HttpsURL("user%23%40:pass%23%40", "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("localhost", 8080, "/");
228 assertEquals("https://localhost:8080/", url.toString());
229 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
230 assertEquals("https://localhost:8080/", url.toString());
231 assertEquals("user:password@localhost:8080", url.getAuthority());
232
233 }
234
235 public void testURIEscaping() throws Exception {
236 String escaped = "http://some.host.com/%41.html";
237 String unescaped = "http://some.host.com/A.html";
238 URI u1 = new URI(escaped, true);
239 GetMethod method = new GetMethod();
240 method.setURI(u1);
241 URI u2 = method.getURI();
242
243 assertEquals(escaped, u1.toString());
244 assertEquals(escaped, new String(u1.getRawURI()));
245 assertEquals(unescaped, u1.getURI());
246 assertEquals(escaped, u2.toString());
247 assertEquals(escaped, new String(u2.getRawURI()));
248 assertEquals(unescaped, u2.getURI());
249 }
250
251 }