View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.junit.Assert;
40  import org.junit.Test;
41  
42  public class TestURIBuilder {
43  
44      @Test
45      public void testHierarchicalUri() throws Exception {
46          final URI uri = new URI("http", "stuff", "localhost", 80, "/some stuff", "param=stuff", "fragment");
47          final URIBuilder uribuilder = new URIBuilder(uri);
48          final URI result = uribuilder.build();
49          Assert.assertEquals(new URI("http://stuff@localhost:80/some%20stuff?param=stuff#fragment"), result);
50      }
51  
52      @Test
53      public void testMutationToRelativeUri() throws Exception {
54          final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
55          final URIBuilder uribuilder = new URIBuilder(uri).setHost(null);
56          final URI result = uribuilder.build();
57          Assert.assertEquals(new URI("http:///stuff?param=stuff#fragment"), result);
58      }
59  
60      @Test
61      public void testMutationRemoveFragment() throws Exception {
62          final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
63          final URI result = new URIBuilder(uri).setFragment(null).build();
64          Assert.assertEquals(new URI("http://stuff@localhost:80/stuff?param=stuff"), result);
65      }
66  
67      @Test
68      public void testMutationRemoveUserInfo() throws Exception {
69          final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
70          final URI result = new URIBuilder(uri).setUserInfo(null).build();
71          Assert.assertEquals(new URI("http://localhost:80/stuff?param=stuff#fragment"), result);
72      }
73  
74      @Test
75      public void testMutationRemovePort() throws Exception {
76          final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
77          final URI result = new URIBuilder(uri).setPort(-1).build();
78          Assert.assertEquals(new URI("http://stuff@localhost/stuff?param=stuff#fragment"), result);
79      }
80  
81      @Test
82      public void testOpaqueUri() throws Exception {
83          final URI uri = new URI("stuff", "some-stuff", "fragment");
84          final URIBuilder uribuilder = new URIBuilder(uri);
85          final URI result = uribuilder.build();
86          Assert.assertEquals(uri, result);
87      }
88  
89      @Test
90      public void testOpaqueUriMutation() throws Exception {
91          final URI uri = new URI("stuff", "some-stuff", "fragment");
92          final URIBuilder uribuilder = new URIBuilder(uri).setCustomQuery("param1&param2=stuff").setFragment(null);
93          Assert.assertEquals(new URI("stuff:?param1&param2=stuff"), uribuilder.build());
94      }
95  
96      @Test
97      public void testHierarchicalUriMutation() throws Exception {
98          final URIBuilder uribuilder = new URIBuilder("/").setScheme("http").setHost("localhost").setPort(80).setPath("/stuff");
99          Assert.assertEquals(new URI("http://localhost:80/stuff"), uribuilder.build());
100     }
101 
102     @Test
103     public void testEmpty() throws Exception {
104         final URIBuilder uribuilder = new URIBuilder();
105         final URI result = uribuilder.build();
106         Assert.assertEquals(new URI(""), result);
107     }
108 
109     @Test
110     public void testSetUserInfo() throws Exception {
111         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
112         final URIBuilder uribuilder = new URIBuilder(uri).setUserInfo("user", "password");
113         final URI result = uribuilder.build();
114         Assert.assertEquals(new URI("http://user:password@localhost:80/?param=stuff"), result);
115     }
116 
117     @Test
118     public void testRemoveParameters() throws Exception {
119         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
120         final URIBuilder uribuilder = new URIBuilder(uri).removeQuery();
121         final URI result = uribuilder.build();
122         Assert.assertEquals(new URI("http://localhost:80/"), result);
123     }
124 
125     @Test
126     public void testSetParameter() throws Exception {
127         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
128         final URIBuilder uribuilder = new URIBuilder(uri).setParameter("param", "some other stuff")
129             .setParameter("blah", "blah");
130         final URI result = uribuilder.build();
131         Assert.assertEquals(new URI("http://localhost:80/?param=some+other+stuff&blah=blah"), result);
132     }
133 
134     @Test
135     public void testSetParametersWithEmptyArg() throws Exception {
136         final URI uri = new URI("http", null, "localhost", 80, "/test", "param=test", null);
137         final URIBuilder uribuilder = new URIBuilder(uri).setParameters();
138         final URI result = uribuilder.build();
139         Assert.assertEquals(new URI("http://localhost:80/test"), result);
140     }
141 
142     @Test
143     public void testSetParametersWithEmptyList() throws Exception {
144         final URI uri = new URI("http", null, "localhost", 80, "/test", "param=test", null);
145         final URIBuilder uribuilder = new URIBuilder(uri).setParameters(Arrays.<NameValuePair>asList());
146         final URI result = uribuilder.build();
147         Assert.assertEquals(new URI("http://localhost:80/test"), result);
148     }
149 
150     @Test
151     public void testParameterWithSpecialChar() throws Exception {
152         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
153         final URIBuilder uribuilder = new URIBuilder(uri).addParameter("param", "1 + 1 = 2")
154             .addParameter("param", "blah&blah");
155         final URI result = uribuilder.build();
156         Assert.assertEquals(new URI("http://localhost:80/?param=stuff&param=1+%2B+1+%3D+2&" +
157                 "param=blah%26blah"), result);
158     }
159 
160     @Test
161     public void testAddParameter() throws Exception {
162         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
163         final URIBuilder uribuilder = new URIBuilder(uri).addParameter("param", "some other stuff")
164             .addParameter("blah", "blah");
165         final URI result = uribuilder.build();
166         Assert.assertEquals(new URI("http://localhost:80/?param=stuff&blah&blah&" +
167                 "param=some+other+stuff&blah=blah"), result);
168     }
169 
170     @Test
171     public void testQueryEncoding() throws Exception {
172         final URI uri1 = new URI("https://somehost.com/stuff?client_id=1234567890" +
173                 "&redirect_uri=https%3A%2F%2Fsomehost.com%2Fblah+blah%2F");
174         final URI uri2 = new URIBuilder("https://somehost.com/stuff")
175             .addParameter("client_id","1234567890")
176             .addParameter("redirect_uri","https://somehost.com/blah blah/").build();
177         Assert.assertEquals(uri1, uri2);
178     }
179 
180     @Test
181     public void testQueryAndParameterEncoding() throws Exception {
182         final URI uri1 = new URI("https://somehost.com/stuff?param1=12345&param2=67890");
183         final URI uri2 = new URIBuilder("https://somehost.com/stuff")
184             .setCustomQuery("this&that")
185             .addParameter("param1","12345")
186             .addParameter("param2","67890").build();
187         Assert.assertEquals(uri1, uri2);
188     }
189 
190     @Test
191     public void testPathEncoding() throws Exception {
192         final URI uri1 = new URI("https://somehost.com/some%20path%20with%20blanks/");
193         final URI uri2 = new URIBuilder()
194             .setScheme("https")
195             .setHost("somehost.com")
196             .setPath("/some path with blanks/")
197             .build();
198         Assert.assertEquals(uri1, uri2);
199     }
200 
201     @Test
202     public void testAgainstURI() throws Exception {
203         // Check that the URI generated by URI builder agrees with that generated by using URI directly
204         final String scheme="https";
205         final String host="localhost";
206         final String specials="/abcd!$&*()_-+.,=:;'~@[]?<>|#^%\"{}\\\u00a3`\u00ac\u00a6xyz"; // N.B. excludes space
207         final URI uri = new URI(scheme, specials, host, 80, specials, specials, specials);
208 
209         final URI bld = new URIBuilder()
210                 .setScheme(scheme)
211                 .setHost(host)
212                 .setUserInfo(specials)
213                 .setPath(specials)
214                 .setCustomQuery(specials)
215                 .setFragment(specials)
216                 .build();
217 
218         Assert.assertEquals(uri.getHost(), bld.getHost());
219 
220         Assert.assertEquals(uri.getUserInfo(), bld.getUserInfo());
221 
222         Assert.assertEquals(uri.getPath(), bld.getPath());
223 
224         Assert.assertEquals(uri.getQuery(), bld.getQuery());
225 
226         Assert.assertEquals(uri.getFragment(), bld.getFragment());
227 
228     }
229 
230     @Test
231     public void testAgainstURIEncoded() throws Exception {
232         // Check that the encoded URI generated by URI builder agrees with that generated by using URI directly
233         final String scheme="https";
234         final String host="localhost";
235         final String specials="/ abcd!$&*()_-+.,=:;'~<>/@[]|#^%\"{}\\`xyz"; // N.B. excludes \u00a3\u00ac\u00a6
236         final URI uri = new URI(scheme, specials, host, 80, specials, specials, specials);
237 
238         final URI bld = new URIBuilder()
239                 .setScheme(scheme)
240                 .setHost(host)
241                 .setUserInfo(specials)
242                 .setPath(specials)
243                 .setCustomQuery(specials)
244                 .setFragment(specials)
245                 .build();
246 
247         Assert.assertEquals(uri.getHost(), bld.getHost());
248 
249         Assert.assertEquals(uri.getRawUserInfo(), bld.getRawUserInfo());
250 
251         Assert.assertEquals(uri.getRawPath(), bld.getRawPath());
252 
253         Assert.assertEquals(uri.getRawQuery(), bld.getRawQuery());
254 
255         Assert.assertEquals(uri.getRawFragment(), bld.getRawFragment());
256 
257     }
258 
259     @Test
260     public void testBuildAddParametersUTF8() throws Exception {
261         assertAddParameters(Consts.UTF_8);
262     }
263 
264     @Test
265     public void testBuildAddParametersISO88591() throws Exception {
266         assertAddParameters(Consts.ISO_8859_1);
267     }
268 
269     public void assertAddParameters(final Charset charset) throws Exception {
270         final URI uri = new URIBuilder("https://somehost.com/stuff")
271                 .setCharset(charset)
272                 .addParameters(createParameters()).build();
273 
274         assertBuild(charset, uri);
275     }
276 
277     @Test
278     public void testBuildSetParametersUTF8() throws Exception {
279         assertSetParameters(Consts.UTF_8);
280     }
281 
282     @Test
283     public void testBuildSetParametersISO88591() throws Exception {
284         assertSetParameters(Consts.ISO_8859_1);
285     }
286 
287     public void assertSetParameters(final Charset charset) throws Exception {
288         final URI uri = new URIBuilder("https://somehost.com/stuff")
289                 .setCharset(charset)
290                 .setParameters(createParameters()).build();
291 
292         assertBuild(charset, uri);
293     }
294 
295     public void assertBuild(final Charset charset, final URI uri) throws Exception {
296         final String encodedData1 = URLEncoder.encode("\"1\u00aa position\"", charset.displayName());
297         final String encodedData2 = URLEncoder.encode("Jos\u00e9 Abra\u00e3o", charset.displayName());
298 
299         final String uriExpected = String.format("https://somehost.com/stuff?parameter1=value1&parameter2=%s&parameter3=%s", encodedData1, encodedData2);
300 
301         Assert.assertEquals(uriExpected, uri.toString());
302     }
303 
304     private List<NameValuePair> createParameters() {
305         final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
306         parameters.add(new BasicNameValuePair("parameter1", "value1"));
307         parameters.add(new BasicNameValuePair("parameter2", "\"1\u00aa position\""));
308         parameters.add(new BasicNameValuePair("parameter3", "Jos\u00e9 Abra\u00e3o"));
309         return parameters;
310     }
311 
312     @Test
313     public void testMalformedPath() throws Exception {
314         final String path = "@notexample.com/mypath";
315         final URI uri = new URIBuilder(path).setHost("example.com").build();
316         Assert.assertEquals("example.com", uri.getHost());
317     }
318 
319     @Test
320     public void testRelativePath() throws Exception {
321         final URI uri = new URIBuilder("./mypath").build();
322         Assert.assertEquals(new URI("./mypath"), uri);
323     }
324 
325     @Test
326     public void testRelativePathWithAuthority() throws Exception {
327         final URI uri = new URIBuilder("./mypath").setHost("somehost").setScheme("http").build();
328         Assert.assertEquals(new URI("http://somehost/./mypath"), uri);
329     }
330 
331 }