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 { ":g", "http", "a", "/b/c/:g", null, null, "http://a/b/c/:g" } // see issue #35148
159 };
160 for (int i = 0; i < testRelativeURIs.length; i++) {
161 URI testURI = null;
162
163 try {
164 testURI = new URI( baseURI, testRelativeURIs[i][0], false );
165 } catch ( URIException e ) {
166 e.printStackTrace();
167 fail(
168 "unable to create URI with relative value("
169 + testRelativeURIs[i][0] + "): " + e
170 );
171 }
172
173 try {
174 assertEquals("array index "+i, testRelativeURIs[i][1], testURI.getScheme());
175 assertEquals("array index "+i, testRelativeURIs[i][2], testURI.getAuthority());
176 assertEquals("array index "+i, testRelativeURIs[i][3], testURI.getPath());
177 assertEquals("array index "+i, testRelativeURIs[i][4], testURI.getQuery());
178 assertEquals("array index "+i, testRelativeURIs[i][5], testURI.getFragment());
179 assertEquals("array index "+i, testRelativeURIs[i][6], testURI.getURIReference());
180 } catch ( URIException e ) {
181 fail( "error getting URI property: " + e );
182 }
183 }
184
185 }
186
187 public void testTestHttpUrlAuthorityString() throws Exception {
188 HttpURL url = new HttpURL("localhost", -1, "/");
189 assertEquals("http://localhost/", url.toString());
190 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
191 assertEquals("http://localhost/", url.toString());
192 assertEquals("user:password@localhost", url.getAuthority());
193
194 url = new HttpURL("user#@", "pass#@", "localhost", 8080, "/");
195 assertEquals("http://localhost:8080/", url.toString());
196 assertEquals("user#@:pass#@", url.getUserinfo());
197 assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
198
199 url = new HttpURL("user%23%40:pass%23%40", "localhost", 8080, "/");
200 assertEquals("http://localhost:8080/", url.toString());
201 assertEquals("user#@:pass#@", url.getUserinfo());
202 assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
203
204 url = new HttpURL("localhost", 8080, "/");
205 assertEquals("http://localhost:8080/", url.toString());
206 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
207 assertEquals("http://localhost:8080/", url.toString());
208 assertEquals("user:password@localhost:8080", url.getAuthority());
209 }
210
211 public void testTestHttpsUrlAuthorityString() throws Exception {
212 HttpsURL url = new HttpsURL("localhost", -1, "/");
213 assertEquals("https://localhost/", url.toString());
214 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
215 assertEquals("https://localhost/", url.toString());
216 assertEquals("user:password@localhost", url.getAuthority());
217
218 url = new HttpsURL("user#@", "pass#@", "localhost", 8080, "/");
219 assertEquals("https://localhost:8080/", url.toString());
220 assertEquals("user#@:pass#@", url.getUserinfo());
221 assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
222
223 url = new HttpsURL("user%23%40:pass%23%40", "localhost", 8080, "/");
224 assertEquals("https://localhost:8080/", url.toString());
225 assertEquals("user#@:pass#@", url.getUserinfo());
226 assertEquals("user%23%40:pass%23%40", url.getEscapedUserinfo());
227
228 url = new HttpsURL("localhost", 8080, "/");
229 assertEquals("https://localhost:8080/", url.toString());
230 url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
231 assertEquals("https://localhost:8080/", url.toString());
232 assertEquals("user:password@localhost:8080", url.getAuthority());
233
234 }
235
236 public void testURIEscaping() throws Exception {
237 String escaped = "http://some.host.com/%41.html";
238 String unescaped = "http://some.host.com/A.html";
239 URI u1 = new URI(escaped, true);
240 GetMethod method = new GetMethod();
241 method.setURI(u1);
242 URI u2 = method.getURI();
243
244 assertEquals(escaped, u1.toString());
245 assertEquals(escaped, new String(u1.getRawURI()));
246 assertEquals(unescaped, u1.getURI());
247 assertEquals(escaped, u2.toString());
248 assertEquals(escaped, new String(u2.getRawURI()));
249 assertEquals(unescaped, u2.getURI());
250 }
251
252 }