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