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.io.ByteArrayInputStream;
31  
32  import org.apache.http.Header;
33  import org.apache.http.HttpVersion;
34  import org.apache.http.entity.InputStreamEntity;
35  import org.apache.http.entity.StringEntity;
36  import org.apache.http.util.LangUtils;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  public class TestHttpRequestBase {
41  
42      @Test
43      public void testBasicProperties() throws Exception {
44          final HttpGet httpget = new HttpGet("http://host/path");
45          Assert.assertEquals("GET", httpget.getRequestLine().getMethod());
46          Assert.assertEquals("http://host/path", httpget.getRequestLine().getUri());
47          Assert.assertEquals(HttpVersion.HTTP_1_1, httpget.getRequestLine().getProtocolVersion());
48      }
49  
50      @Test
51      public void testEmptyURI() throws Exception {
52          final HttpGet httpget = new HttpGet("");
53          Assert.assertEquals("/", httpget.getRequestLine().getUri());
54      }
55  
56      @Test
57      public void testCloneBasicRequests() throws Exception {
58          final HttpGet httpget = new HttpGet("http://host/path");
59          httpget.addHeader("h1", "this header");
60          httpget.addHeader("h2", "that header");
61          httpget.addHeader("h3", "all sorts of headers");
62          final HttpGet clone = (HttpGet) httpget.clone();
63  
64          Assert.assertEquals(httpget.getMethod(), clone.getMethod());
65          Assert.assertEquals(httpget.getURI(), clone.getURI());
66  
67          final Header[] headers1 = httpget.getAllHeaders();
68          final Header[] headers2 = clone.getAllHeaders();
69  
70          Assert.assertTrue(LangUtils.equals(headers1, headers2));
71      }
72  
73      @Test
74      public void testCloneEntityEnclosingRequests() throws Exception {
75          final HttpPost httppost = new HttpPost("http://host/path");
76          httppost.addHeader("h1", "this header");
77          httppost.addHeader("h2", "that header");
78          httppost.addHeader("h3", "all sorts of headers");
79          HttpPost clone = (HttpPost) httppost.clone();
80  
81          Assert.assertEquals(httppost.getMethod(), clone.getMethod());
82          Assert.assertEquals(httppost.getURI(), clone.getURI());
83  
84          final Header[] headers1 = httppost.getAllHeaders();
85          final Header[] headers2 = clone.getAllHeaders();
86  
87          Assert.assertTrue(LangUtils.equals(headers1, headers2));
88  
89          Assert.assertNull(clone.getEntity());
90  
91          final StringEntity e1 = new StringEntity("stuff");
92          httppost.setEntity(e1);
93          clone = (HttpPost) httppost.clone();
94          Assert.assertTrue(clone.getEntity() instanceof StringEntity);
95          Assert.assertFalse(clone.getEntity().equals(e1));
96      }
97  
98      @Test(expected=CloneNotSupportedException.class)
99      public void testCloneStreamingEntityEnclosingRequests() throws Exception {
100         final ByteArrayInputStream instream = new ByteArrayInputStream(new byte[] {});
101         final InputStreamEntity e2 = new InputStreamEntity(instream, -1);
102         final HttpPost httppost = new HttpPost("http://host/path");
103         httppost.setEntity(e2);
104         httppost.clone();
105     }
106 
107 }