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 package org.apache.http.client.utils;
28
29 import java.net.URI;
30 import java.net.URLEncoder;
31 import java.nio.charset.Charset;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35
36 import org.apache.http.Consts;
37 import org.apache.http.NameValuePair;
38 import org.apache.http.message.BasicNameValuePair;
39 import org.hamcrest.CoreMatchers;
40 import org.junit.Assert;
41 import org.junit.Test;
42
43 public class TestURIBuilder {
44
45 @Test
46 public void testHierarchicalUri() throws Exception {
47 final URI uri = new URI("http", "stuff", "localhost", 80, "/some stuff", "param=stuff", "fragment");
48 final URIBuilder uribuilder = new URIBuilder(uri);
49 final URI result = uribuilder.build();
50 Assert.assertEquals(new URI("http://stuff@localhost:80/some%20stuff?param=stuff#fragment"), result);
51 }
52
53 @Test
54 public void testMutationToRelativeUri() throws Exception {
55 final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
56 final URIBuilder uribuilder = new URIBuilder(uri).setHost(null);
57 final URI result = uribuilder.build();
58 Assert.assertEquals(new URI("http:///stuff?param=stuff#fragment"), result);
59 }
60
61 @Test
62 public void testMutationRemoveFragment() throws Exception {
63 final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
64 final URI result = new URIBuilder(uri).setFragment(null).build();
65 Assert.assertEquals(new URI("http://stuff@localhost:80/stuff?param=stuff"), result);
66 }
67
68 @Test
69 public void testMutationRemoveUserInfo() throws Exception {
70 final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
71 final URI result = new URIBuilder(uri).setUserInfo(null).build();
72 Assert.assertEquals(new URI("http://localhost:80/stuff?param=stuff#fragment"), result);
73 }
74
75 @Test
76 public void testMutationRemovePort() throws Exception {
77 final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
78 final URI result = new URIBuilder(uri).setPort(-1).build();
79 Assert.assertEquals(new URI("http://stuff@localhost/stuff?param=stuff#fragment"), result);
80 }
81
82 @Test
83 public void testOpaqueUri() throws Exception {
84 final URI uri = new URI("stuff", "some-stuff", "fragment");
85 final URIBuilder uribuilder = new URIBuilder(uri);
86 final URI result = uribuilder.build();
87 Assert.assertEquals(uri, result);
88 }
89
90 @Test
91 public void testOpaqueUriMutation() throws Exception {
92 final URI uri = new URI("stuff", "some-stuff", "fragment");
93 final URIBuilder uribuilder = new URIBuilder(uri).setCustomQuery("param1¶m2=stuff").setFragment(null);
94 Assert.assertEquals(new URI("stuff:?param1¶m2=stuff"), uribuilder.build());
95 }
96
97 @Test
98 public void testHierarchicalUriMutation() throws Exception {
99 final URIBuilder uribuilder = new URIBuilder("/").setScheme("http").setHost("localhost").setPort(80).setPath("/stuff");
100 Assert.assertEquals(new URI("http://localhost:80/stuff"), uribuilder.build());
101 }
102
103 @Test
104 public void testEmpty() throws Exception {
105 final URIBuilder uribuilder = new URIBuilder();
106 final URI result = uribuilder.build();
107 Assert.assertEquals(new URI(""), result);
108 }
109
110 @Test
111 public void testSetUserInfo() throws Exception {
112 final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
113 final URIBuilder uribuilder = new URIBuilder(uri).setUserInfo("user", "password");
114 final URI result = uribuilder.build();
115 Assert.assertEquals(new URI("http://user:password@localhost:80/?param=stuff"), result);
116 }
117
118 @Test
119 public void testRemoveParameters() throws Exception {
120 final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
121 final URIBuilder uribuilder = new URIBuilder(uri).removeQuery();
122 final URI result = uribuilder.build();
123 Assert.assertEquals(new URI("http://localhost:80/"), result);
124 }
125
126 @Test
127 public void testSetParameter() throws Exception {
128 final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
129 final URIBuilder uribuilder = new URIBuilder(uri).setParameter("param", "some other stuff")
130 .setParameter("blah", "blah");
131 final URI result = uribuilder.build();
132 Assert.assertEquals(new URI("http://localhost:80/?param=some+other+stuff&blah=blah"), result);
133 }
134
135 @Test
136 public void testSetParametersWithEmptyArg() throws Exception {
137 final URI uri = new URI("http", null, "localhost", 80, "/test", "param=test", null);
138 final URIBuilder uribuilder = new URIBuilder(uri).setParameters();
139 final URI result = uribuilder.build();
140 Assert.assertEquals(new URI("http://localhost:80/test"), result);
141 }
142
143 @Test
144 public void testSetParametersWithEmptyList() throws Exception {
145 final URI uri = new URI("http", null, "localhost", 80, "/test", "param=test", null);
146 final URIBuilder uribuilder = new URIBuilder(uri).setParameters(Arrays.<NameValuePair>asList());
147 final URI result = uribuilder.build();
148 Assert.assertEquals(new URI("http://localhost:80/test"), result);
149 }
150
151 @Test
152 public void testParameterWithSpecialChar() throws Exception {
153 final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
154 final URIBuilder uribuilder = new URIBuilder(uri).addParameter("param", "1 + 1 = 2")
155 .addParameter("param", "blah&blah");
156 final URI result = uribuilder.build();
157 Assert.assertEquals(new URI("http://localhost:80/?param=stuff¶m=1+%2B+1+%3D+2&" +
158 "param=blah%26blah"), result);
159 }
160
161 @Test
162 public void testAddParameter() throws Exception {
163 final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
164 final URIBuilder uribuilder = new URIBuilder(uri).addParameter("param", "some other stuff")
165 .addParameter("blah", "blah");
166 final URI result = uribuilder.build();
167 Assert.assertEquals(new URI("http://localhost:80/?param=stuff&blah&blah&" +
168 "param=some+other+stuff&blah=blah"), result);
169 }
170
171 @Test
172 public void testQueryEncoding() throws Exception {
173 final URI uri1 = new URI("https://somehost.com/stuff?client_id=1234567890" +
174 "&redirect_uri=https%3A%2F%2Fsomehost.com%2Fblah+blah%2F");
175 final URI uri2 = new URIBuilder("https://somehost.com/stuff")
176 .addParameter("client_id","1234567890")
177 .addParameter("redirect_uri","https://somehost.com/blah blah/").build();
178 Assert.assertEquals(uri1, uri2);
179 }
180
181 @Test
182 public void testQueryAndParameterEncoding() throws Exception {
183 final URI uri1 = new URI("https://somehost.com/stuff?param1=12345¶m2=67890");
184 final URI uri2 = new URIBuilder("https://somehost.com/stuff")
185 .setCustomQuery("this&that")
186 .addParameter("param1","12345")
187 .addParameter("param2","67890").build();
188 Assert.assertEquals(uri1, uri2);
189 }
190
191 @Test
192 public void testPathEncoding() throws Exception {
193 final URI uri1 = new URI("https://somehost.com/some%20path%20with%20blanks/");
194 final URI uri2 = new URIBuilder()
195 .setScheme("https")
196 .setHost("somehost.com")
197 .setPath("/some path with blanks/")
198 .build();
199 Assert.assertEquals(uri1, uri2);
200 }
201
202 @Test
203 public void testAgainstURI() throws Exception {
204
205 final String scheme="https";
206 final String host="localhost";
207 final String specials="/abcd!$&*()_-+.,=:;'~@[]?<>|#^%\"{}\\\u00a3`\u00ac\u00a6xyz";
208 final URI uri = new URI(scheme, specials, host, 80, specials, specials, specials);
209
210 final URI bld = new URIBuilder()
211 .setScheme(scheme)
212 .setHost(host)
213 .setUserInfo(specials)
214 .setPath(specials)
215 .setCustomQuery(specials)
216 .setFragment(specials)
217 .build();
218
219 Assert.assertEquals(uri.getHost(), bld.getHost());
220
221 Assert.assertEquals(uri.getUserInfo(), bld.getUserInfo());
222
223 Assert.assertEquals(uri.getPath(), bld.getPath());
224
225 Assert.assertEquals(uri.getQuery(), bld.getQuery());
226
227 Assert.assertEquals(uri.getFragment(), bld.getFragment());
228
229 }
230
231 @Test
232 public void testAgainstURIEncoded() throws Exception {
233
234 final String scheme="https";
235 final String host="localhost";
236 final String specials="/ abcd!$&*()_-+.,=:;'~<>/@[]|#^%\"{}\\`xyz";
237 final URI uri = new URI(scheme, specials, host, 80, specials, specials, specials);
238
239 final URI bld = new URIBuilder()
240 .setScheme(scheme)
241 .setHost(host)
242 .setUserInfo(specials)
243 .setPath(specials)
244 .setCustomQuery(specials)
245 .setFragment(specials)
246 .build();
247
248 Assert.assertEquals(uri.getHost(), bld.getHost());
249
250 Assert.assertEquals(uri.getRawUserInfo(), bld.getRawUserInfo());
251
252 Assert.assertEquals(uri.getRawPath(), bld.getRawPath());
253
254 Assert.assertEquals(uri.getRawQuery(), bld.getRawQuery());
255
256 Assert.assertEquals(uri.getRawFragment(), bld.getRawFragment());
257
258 }
259
260 @Test
261 public void testBuildAddParametersUTF8() throws Exception {
262 assertAddParameters(Consts.UTF_8);
263 }
264
265 @Test
266 public void testBuildAddParametersISO88591() throws Exception {
267 assertAddParameters(Consts.ISO_8859_1);
268 }
269
270 public void assertAddParameters(final Charset charset) throws Exception {
271 final URI uri = new URIBuilder("https://somehost.com/stuff")
272 .setCharset(charset)
273 .addParameters(createParameters()).build();
274
275 assertBuild(charset, uri);
276 }
277
278 @Test
279 public void testBuildSetParametersUTF8() throws Exception {
280 assertSetParameters(Consts.UTF_8);
281 }
282
283 @Test
284 public void testBuildSetParametersISO88591() throws Exception {
285 assertSetParameters(Consts.ISO_8859_1);
286 }
287
288 public void assertSetParameters(final Charset charset) throws Exception {
289 final URI uri = new URIBuilder("https://somehost.com/stuff")
290 .setCharset(charset)
291 .setParameters(createParameters()).build();
292
293 assertBuild(charset, uri);
294 }
295
296 public void assertBuild(final Charset charset, final URI uri) throws Exception {
297 final String encodedData1 = URLEncoder.encode("\"1\u00aa position\"", charset.displayName());
298 final String encodedData2 = URLEncoder.encode("Jos\u00e9 Abra\u00e3o", charset.displayName());
299
300 final String uriExpected = String.format("https://somehost.com/stuff?parameter1=value1¶meter2=%s¶meter3=%s", encodedData1, encodedData2);
301
302 Assert.assertEquals(uriExpected, uri.toString());
303 }
304
305 private List<NameValuePair> createParameters() {
306 final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
307 parameters.add(new BasicNameValuePair("parameter1", "value1"));
308 parameters.add(new BasicNameValuePair("parameter2", "\"1\u00aa position\""));
309 parameters.add(new BasicNameValuePair("parameter3", "Jos\u00e9 Abra\u00e3o"));
310 return parameters;
311 }
312
313 @Test
314 public void testMalformedPath() throws Exception {
315 final String path = "@notexample.com/mypath";
316 final URI uri = new URIBuilder(path).setHost("example.com").build();
317 Assert.assertEquals("example.com", uri.getHost());
318 }
319
320 @Test
321 public void testRelativePath() throws Exception {
322 final URI uri = new URIBuilder("./mypath").build();
323 Assert.assertEquals(new URI("./mypath"), uri);
324 }
325
326 @Test
327 public void testRelativePathWithAuthority() throws Exception {
328 final URI uri = new URIBuilder("./mypath").setHost("somehost").setScheme("http").build();
329 Assert.assertEquals(new URI("http://somehost/./mypath"), uri);
330 }
331
332 @Test
333 public void testMultipleLeadingPathSlashes() throws Exception {
334 final URI uri = new URIBuilder()
335 .setScheme("ftp")
336 .setHost("somehost")
337 .setPath("//blah//blah")
338 .build();
339 Assert.assertThat(uri, CoreMatchers.equalTo(URI.create("ftp://somehost//blah//blah")));
340 }
341
342 @Test
343 public void testPathNoLeadingSlash() throws Exception {
344 final URI uri = new URIBuilder()
345 .setScheme("ftp")
346 .setPath("blah")
347 .build();
348 Assert.assertThat(uri, CoreMatchers.equalTo(URI.create("ftp:/blah")));
349 }
350
351 @Test
352 public void testOpaque() throws Exception {
353 final URIBuilder uriBuilder = new URIBuilder("http://host.com");
354 final URI uri = uriBuilder.build();
355 Assert.assertThat(uriBuilder.isOpaque(), CoreMatchers.equalTo(uri.isOpaque()));
356 }
357
358 }