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  
28  package org.apache.http.client.methods;
29  
30  import java.net.URI;
31  import java.util.List;
32  
33  import org.apache.http.Header;
34  import org.apache.http.HttpEntity;
35  import org.apache.http.HttpEntityEnclosingRequest;
36  import org.apache.http.HttpRequest;
37  import org.apache.http.HttpVersion;
38  import org.apache.http.NameValuePair;
39  import org.apache.http.client.config.RequestConfig;
40  import org.apache.http.entity.BasicHttpEntity;
41  import org.apache.http.entity.ContentType;
42  import org.apache.http.entity.StringEntity;
43  import org.apache.http.message.BasicHeader;
44  import org.apache.http.message.BasicNameValuePair;
45  import org.apache.http.util.EntityUtils;
46  import org.junit.Assert;
47  import org.junit.Test;
48  
49  public class TestRequestBuilder {
50  
51      @Test
52      public void testBasicGet() throws Exception {
53          final HttpUriRequest request = RequestBuilder.get().build();
54          Assert.assertNotNull(request);
55          Assert.assertEquals("GET", request.getMethod());
56          Assert.assertEquals(URI.create("/"), request.getURI());
57          Assert.assertEquals(HttpVersion.HTTP_1_1, request.getProtocolVersion());
58          Assert.assertFalse(request instanceof HttpEntityEnclosingRequest);
59      }
60  
61      @Test
62      public void testArbitraryMethod() throws Exception {
63          final HttpUriRequest request = RequestBuilder.create("Whatever").build();
64          Assert.assertNotNull(request);
65          Assert.assertEquals("Whatever", request.getMethod());
66          Assert.assertEquals(URI.create("/"), request.getURI());
67          Assert.assertEquals(HttpVersion.HTTP_1_1, request.getProtocolVersion());
68      }
69  
70      @Test
71      public void testBasicWithEntity() throws Exception {
72          final HttpEntity entity = new BasicHttpEntity();
73          final HttpUriRequest request = RequestBuilder.post().setEntity(entity).build();
74          Assert.assertNotNull(request);
75          Assert.assertEquals("POST", request.getMethod());
76          Assert.assertEquals(URI.create("/"), request.getURI());
77          Assert.assertEquals(HttpVersion.HTTP_1_1, request.getProtocolVersion());
78          Assert.assertTrue(request instanceof HttpEntityEnclosingRequest);
79          Assert.assertSame(entity, ((HttpEntityEnclosingRequest) request).getEntity());
80      }
81  
82      @Test
83      public void testGetWithEntity() throws Exception {
84          final HttpEntity entity = new BasicHttpEntity();
85          final HttpUriRequest request = RequestBuilder.get().setEntity(entity).build();
86          Assert.assertNotNull(request);
87          Assert.assertEquals("GET", request.getMethod());
88          Assert.assertEquals(URI.create("/"), request.getURI());
89          Assert.assertEquals(HttpVersion.HTTP_1_1, request.getProtocolVersion());
90          Assert.assertTrue(request instanceof HttpEntityEnclosingRequest);
91          Assert.assertSame(entity, ((HttpEntityEnclosingRequest) request).getEntity());
92      }
93  
94      @Test
95      public void testAddParameters1() throws Exception {
96          final HttpUriRequest request = RequestBuilder.get()
97                  .addParameter("p1", "this")
98                  .addParameter("p2", "that")
99                  .build();
100         Assert.assertFalse(request instanceof HttpEntityEnclosingRequest);
101         Assert.assertEquals(new URI("/?p1=this&p2=that"), request.getURI());
102     }
103 
104     @Test
105     public void testAddParameters2() throws Exception {
106         final HttpUriRequest request = RequestBuilder.get()
107                 .addParameter("p1", "this")
108                 .addParameters(new BasicNameValuePair("p2", "that"))
109                 .build();
110         Assert.assertFalse(request instanceof HttpEntityEnclosingRequest);
111         Assert.assertEquals(new URI("/?p1=this&p2=that"), request.getURI());
112     }
113 
114     @Test
115     public void testAddParameters3() throws Exception {
116         final HttpUriRequest request = RequestBuilder.post()
117                 .addParameter("p1", "this")
118                 .addParameter("p2", "that")
119                 .build();
120         Assert.assertTrue(request instanceof HttpEntityEnclosingRequest);
121         final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
122         Assert.assertNotNull(entity);
123         Assert.assertEquals(new URI("/"), request.getURI());
124         Assert.assertEquals("p1=this&p2=that", EntityUtils.toString(entity));
125     }
126 
127     @Test
128     public void testAddParameters4() throws Exception {
129         final HttpUriRequest request = RequestBuilder.post()
130                 .setUri("http://targethost/?blah")
131                 .addParameter("p1", "this")
132                 .addParameter("p2", "that")
133                 .setEntity(new StringEntity("blah"))
134                 .build();
135         Assert.assertTrue(request instanceof HttpEntityEnclosingRequest);
136         Assert.assertEquals(new URI("http://targethost/?blah&p1=this&p2=that"), request.getURI());
137     }
138 
139     @Test
140     public void testCopy() throws Exception {
141         final HttpEntity entity = new StringEntity("stuff");
142         final RequestConfig config = RequestConfig.custom().build();
143         final HttpUriRequest request = RequestBuilder.put()
144             .setUri(URI.create("/stuff"))
145             .setVersion(HttpVersion.HTTP_1_0)
146             .addHeader("header1", "stuff")
147             .setHeader("header2", "more stuff")
148             .setEntity(entity)
149             .setConfig(config)
150             .build();
151         Assert.assertNotNull(request);
152         Assert.assertEquals("PUT", request.getMethod());
153         Assert.assertEquals(URI.create("/stuff"), request.getURI());
154         Assert.assertEquals(HttpVersion.HTTP_1_0, request.getProtocolVersion());
155         Assert.assertTrue(request instanceof HttpEntityEnclosingRequest);
156 
157         final HttpUriRequest copy = RequestBuilder.copy(request).setUri("/other-stuff").build();
158         Assert.assertEquals("PUT", copy.getMethod());
159         Assert.assertEquals(URI.create("/other-stuff"), copy.getURI());
160         Assert.assertTrue(copy instanceof HttpEntityEnclosingRequest);
161         Assert.assertSame(entity, ((HttpEntityEnclosingRequest) copy).getEntity());
162         Assert.assertTrue(copy instanceof Configurable);
163         Assert.assertSame(config, ((Configurable) copy).getConfig());
164     }
165 
166     @Test
167     public void testCopyWithQueryParams() throws Exception {
168         final HttpGet get = new HttpGet("/stuff?p1=this&p2=that");
169         final RequestBuilder builder = RequestBuilder.copy(get);
170         final List<NameValuePair> parameters = builder.getParameters();
171         Assert.assertNotNull(parameters);
172         Assert.assertEquals(0, parameters.size());
173         Assert.assertEquals(new URI("/stuff?p1=this&p2=that"), builder.getUri());
174     }
175 
176     @Test
177     public void testCopyWithFormParams() throws Exception {
178         final HttpPost post = new HttpPost("/stuff?p1=wtf");
179         post.setEntity(new StringEntity("p1=this&p2=that", ContentType.APPLICATION_FORM_URLENCODED));
180         final RequestBuilder builder = RequestBuilder.copy(post);
181         final List<NameValuePair> parameters = builder.getParameters();
182         Assert.assertNotNull(parameters);
183         Assert.assertEquals(2, parameters.size());
184         Assert.assertEquals(new BasicNameValuePair("p1", "this"), parameters.get(0));
185         Assert.assertEquals(new BasicNameValuePair("p2", "that"), parameters.get(1));
186         Assert.assertEquals(new URI("/stuff?p1=wtf"), builder.getUri());
187         Assert.assertNull(builder.getEntity());
188     }
189 
190     @Test
191     public void testCopyWithStringEntity() throws Exception {
192         final HttpPost post = new HttpPost("/stuff?p1=wtf");
193         final HttpEntity entity = new StringEntity("p1=this&p2=that", ContentType.TEXT_PLAIN);
194         post.setEntity(entity);
195         final RequestBuilder builder = RequestBuilder.copy(post);
196         final List<NameValuePair> parameters = builder.getParameters();
197         Assert.assertNotNull(parameters);
198         Assert.assertEquals(0, parameters.size());
199         Assert.assertEquals(new URI("/stuff?p1=wtf"), builder.getUri());
200         Assert.assertSame(entity, builder.getEntity());
201     }
202 
203     @Test
204     public void testCopyAndSetUri() throws Exception {
205         final URI uri1 = URI.create("http://host1.com/path?param=something");
206         final URI uri2 = URI.create("http://host2.com/path?param=somethingdifferent");
207         final HttpRequest request1 = new HttpGet(uri1);
208         final HttpUriRequest request2 = RequestBuilder.copy(request1).setUri(uri2).build();
209         Assert.assertEquals(request2.getURI(), uri2);
210     }
211 
212     @Test
213     public void testGettersAndMutators() throws Exception {
214         final HttpEntity entity = new StringEntity("stuff");
215         final RequestConfig config = RequestConfig.custom().build();
216         final Header h1 = new BasicHeader("header1", "stuff");
217         final Header h2 = new BasicHeader("header1", "more-stuff");
218         final RequestBuilder builder = RequestBuilder.put()
219             .setUri("/stuff")
220             .setVersion(HttpVersion.HTTP_1_0)
221             .addHeader(h1)
222             .addHeader(h2)
223             .setEntity(entity)
224             .setConfig(config);
225         Assert.assertEquals("PUT", builder.getMethod());
226         Assert.assertEquals(URI.create("/stuff"), builder.getUri());
227         Assert.assertEquals(HttpVersion.HTTP_1_0, builder.getVersion());
228         Assert.assertSame(h1, builder.getFirstHeader("header1"));
229         Assert.assertSame(h2, builder.getLastHeader("header1"));
230         Assert.assertEquals(2, builder.getHeaders("header1").length);
231         Assert.assertSame(entity, builder.getEntity());
232         Assert.assertSame(config, builder.getConfig());
233 
234         builder.setUri((String) null)
235             .setVersion(null)
236             .removeHeader(h1)
237             .removeHeaders("header1")
238             .removeHeader(h2)
239             .setEntity(null)
240             .setConfig(null);
241         Assert.assertEquals(null, builder.getUri());
242         Assert.assertEquals(null, builder.getVersion());
243         Assert.assertSame(null, builder.getFirstHeader("header1"));
244         Assert.assertSame(null, builder.getLastHeader("header1"));
245         Assert.assertEquals(0, builder.getHeaders("header1").length);
246         Assert.assertSame(null, builder.getEntity());
247         Assert.assertSame(null, builder.getConfig());
248 
249         builder.setHeader(h2)
250             .setHeader("header1", "a-lot-more-stuff");
251         Assert.assertSame("a-lot-more-stuff", builder.getLastHeader("header1").getValue());
252         Assert.assertEquals(1, builder.getHeaders("header1").length);
253     }
254 
255     @Test(expected=IllegalArgumentException.class)
256     public void testCopyNull() throws Exception {
257         RequestBuilder.copy(null);
258     }
259 
260 }