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.List;
34  
35  import org.apache.http.Consts;
36  import org.apache.http.NameValuePair;
37  import org.apache.http.message.BasicNameValuePair;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  public class TestURIBuilder {
42  
43      @Test
44      public void testHierarchicalUri() throws Exception {
45          final URI uri = new URI("http", "stuff", "localhost", 80, "/some stuff", "param=stuff", "fragment");
46          final URIBuilder uribuilder = new URIBuilder(uri);
47          final URI result = uribuilder.build();
48          Assert.assertEquals(new URI("http://stuff@localhost:80/some%20stuff?param=stuff#fragment"), result);
49      }
50  
51      @Test
52      public void testMutationToRelativeUri() throws Exception {
53          final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
54          final URIBuilder uribuilder = new URIBuilder(uri).setHost(null);
55          final URI result = uribuilder.build();
56          Assert.assertEquals(new URI("http:///stuff?param=stuff#fragment"), result);
57      }
58  
59      @Test
60      public void testMutationRemoveFragment() throws Exception {
61          final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
62          final URI result = new URIBuilder(uri).setFragment(null).build();
63          Assert.assertEquals(new URI("http://stuff@localhost:80/stuff?param=stuff"), result);
64      }
65  
66      @Test
67      public void testMutationRemoveUserInfo() throws Exception {
68          final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
69          final URI result = new URIBuilder(uri).setUserInfo(null).build();
70          Assert.assertEquals(new URI("http://localhost:80/stuff?param=stuff#fragment"), result);
71      }
72  
73      @Test
74      public void testMutationRemovePort() throws Exception {
75          final URI uri = new URI("http://stuff@localhost:80/stuff?param=stuff#fragment");
76          final URI result = new URIBuilder(uri).setPort(-1).build();
77          Assert.assertEquals(new URI("http://stuff@localhost/stuff?param=stuff#fragment"), result);
78      }
79  
80      @Test
81      public void testOpaqueUri() throws Exception {
82          final URI uri = new URI("stuff", "some-stuff", "fragment");
83          final URIBuilder uribuilder = new URIBuilder(uri);
84          final URI result = uribuilder.build();
85          Assert.assertEquals(uri, result);
86      }
87  
88      @Test
89      public void testOpaqueUriMutation() throws Exception {
90          final URI uri = new URI("stuff", "some-stuff", "fragment");
91          final URIBuilder uribuilder = new URIBuilder(uri).setCustomQuery("param1&param2=stuff").setFragment(null);
92          Assert.assertEquals(new URI("stuff:?param1&param2=stuff"), uribuilder.build());
93      }
94  
95      @Test
96      public void testHierarchicalUriMutation() throws Exception {
97          final URIBuilder uribuilder = new URIBuilder("/").setScheme("http").setHost("localhost").setPort(80).setPath("/stuff");
98          Assert.assertEquals(new URI("http://localhost:80/stuff"), uribuilder.build());
99      }
100 
101     @Test
102     public void testEmpty() throws Exception {
103         final URIBuilder uribuilder = new URIBuilder();
104         final URI result = uribuilder.build();
105         Assert.assertEquals(new URI(""), result);
106     }
107 
108     @Test
109     public void testSetUserInfo() throws Exception {
110         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
111         final URIBuilder uribuilder = new URIBuilder(uri).setUserInfo("user", "password");
112         final URI result = uribuilder.build();
113         Assert.assertEquals(new URI("http://user:password@localhost:80/?param=stuff"), result);
114     }
115 
116     @Test
117     public void testRemoveParameters() throws Exception {
118         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
119         final URIBuilder uribuilder = new URIBuilder(uri).removeQuery();
120         final URI result = uribuilder.build();
121         Assert.assertEquals(new URI("http://localhost:80/"), result);
122     }
123 
124     @Test
125     public void testSetParameter() throws Exception {
126         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
127         final URIBuilder uribuilder = new URIBuilder(uri).setParameter("param", "some other stuff")
128             .setParameter("blah", "blah");
129         final URI result = uribuilder.build();
130         Assert.assertEquals(new URI("http://localhost:80/?param=some+other+stuff&blah=blah"), result);
131     }
132 
133     @Test
134     public void testParameterWithSpecialChar() throws Exception {
135         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
136         final URIBuilder uribuilder = new URIBuilder(uri).addParameter("param", "1 + 1 = 2")
137             .addParameter("param", "blah&blah");
138         final URI result = uribuilder.build();
139         Assert.assertEquals(new URI("http://localhost:80/?param=stuff&param=1+%2B+1+%3D+2&" +
140                 "param=blah%26blah"), result);
141     }
142 
143     @Test
144     public void testAddParameter() throws Exception {
145         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
146         final URIBuilder uribuilder = new URIBuilder(uri).addParameter("param", "some other stuff")
147             .addParameter("blah", "blah");
148         final URI result = uribuilder.build();
149         Assert.assertEquals(new URI("http://localhost:80/?param=stuff&blah&blah&" +
150                 "param=some+other+stuff&blah=blah"), result);
151     }
152 
153     @Test
154     public void testQueryEncoding() throws Exception {
155         final URI uri1 = new URI("https://somehost.com/stuff?client_id=1234567890" +
156                 "&redirect_uri=https%3A%2F%2Fsomehost.com%2Fblah+blah%2F");
157         final URI uri2 = new URIBuilder("https://somehost.com/stuff")
158             .addParameter("client_id","1234567890")
159             .addParameter("redirect_uri","https://somehost.com/blah blah/").build();
160         Assert.assertEquals(uri1, uri2);
161     }
162 
163     @Test
164     public void testQueryAndParameterEncoding() throws Exception {
165         final URI uri1 = new URI("https://somehost.com/stuff?param1=12345&param2=67890");
166         final URI uri2 = new URIBuilder("https://somehost.com/stuff")
167             .setCustomQuery("this&that")
168             .addParameter("param1","12345")
169             .addParameter("param2","67890").build();
170         Assert.assertEquals(uri1, uri2);
171     }
172 
173     @Test
174     public void testPathEncoding() throws Exception {
175         final URI uri1 = new URI("https://somehost.com/some%20path%20with%20blanks/");
176         final URI uri2 = new URIBuilder()
177             .setScheme("https")
178             .setHost("somehost.com")
179             .setPath("/some path with blanks/")
180             .build();
181         Assert.assertEquals(uri1, uri2);
182     }
183 
184     @Test
185     public void testAgainstURI() throws Exception {
186         // Check that the URI generated by URI builder agrees with that generated by using URI directly
187         final String scheme="https";
188         final String host="localhost";
189         final String specials="/abcd!$&*()_-+.,=:;'~@[]?<>|#^%\"{}\\\u00a3`\u00ac\u00a6xyz"; // N.B. excludes space
190         final URI uri = new URI(scheme, specials, host, 80, specials, specials, specials);
191 
192         final URI bld = new URIBuilder()
193                 .setScheme(scheme)
194                 .setHost(host)
195                 .setUserInfo(specials)
196                 .setPath(specials)
197                 .setCustomQuery(specials)
198                 .setFragment(specials)
199                 .build();
200 
201         Assert.assertEquals(uri.getHost(), bld.getHost());
202 
203         Assert.assertEquals(uri.getUserInfo(), bld.getUserInfo());
204 
205         Assert.assertEquals(uri.getPath(), bld.getPath());
206 
207         Assert.assertEquals(uri.getQuery(), bld.getQuery());
208 
209         Assert.assertEquals(uri.getFragment(), bld.getFragment());
210 
211     }
212 
213     @Test
214     public void testAgainstURIEncoded() throws Exception {
215         // Check that the encoded URI generated by URI builder agrees with that generated by using URI directly
216         final String scheme="https";
217         final String host="localhost";
218         final String specials="/ abcd!$&*()_-+.,=:;'~<>/@[]|#^%\"{}\\`xyz"; // N.B. excludes \u00a3\u00ac\u00a6
219         final URI uri = new URI(scheme, specials, host, 80, specials, specials, specials);
220 
221         final URI bld = new URIBuilder()
222                 .setScheme(scheme)
223                 .setHost(host)
224                 .setUserInfo(specials)
225                 .setPath(specials)
226                 .setCustomQuery(specials)
227                 .setFragment(specials)
228                 .build();
229 
230         Assert.assertEquals(uri.getHost(), bld.getHost());
231 
232         Assert.assertEquals(uri.getRawUserInfo(), bld.getRawUserInfo());
233 
234         Assert.assertEquals(uri.getRawPath(), bld.getRawPath());
235 
236         Assert.assertEquals(uri.getRawQuery(), bld.getRawQuery());
237 
238         Assert.assertEquals(uri.getRawFragment(), bld.getRawFragment());
239 
240     }
241 
242     @Test
243     public void testBuildAddParametersUTF8() throws Exception {
244         assertAddParameters(Consts.UTF_8);
245     }
246 
247     @Test
248     public void testBuildAddParametersISO88591() throws Exception {
249         assertAddParameters(Consts.ISO_8859_1);
250     }
251 
252     public void assertAddParameters(final Charset charset) throws Exception {
253         final URI uri = new URIBuilder("https://somehost.com/stuff")
254                 .setCharset(charset)
255                 .addParameters(createParameters()).build();
256 
257         assertBuild(charset, uri);
258     }
259 
260     @Test
261     public void testBuildSetParametersUTF8() throws Exception {
262         assertSetParameters(Consts.UTF_8);
263     }
264 
265     @Test
266     public void testBuildSetParametersISO88591() throws Exception {
267         assertSetParameters(Consts.ISO_8859_1);
268     }
269 
270     public void assertSetParameters(final Charset charset) throws Exception {
271         final URI uri = new URIBuilder("https://somehost.com/stuff")
272                 .setCharset(charset)
273                 .setParameters(createParameters()).build();
274 
275         assertBuild(charset, uri);
276     }
277 
278     public void assertBuild(final Charset charset, final URI uri) throws Exception {
279         final String encodedData1 = URLEncoder.encode("\"1\u00aa position\"", charset.displayName());
280         final String encodedData2 = URLEncoder.encode("Jos\u00e9 Abra\u00e3o", charset.displayName());
281 
282         final String uriExpected = String.format("https://somehost.com/stuff?parameter1=value1&parameter2=%s&parameter3=%s", encodedData1, encodedData2);
283 
284         Assert.assertEquals(uriExpected, uri.toString());
285     }
286 
287     private List<NameValuePair> createParameters() {
288         final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
289         parameters.add(new BasicNameValuePair("parameter1", "value1"));
290         parameters.add(new BasicNameValuePair("parameter2", "\"1\u00aa position\""));
291         parameters.add(new BasicNameValuePair("parameter3", "Jos\u00e9 Abra\u00e3o"));
292         return parameters;
293     }
294 
295 }