1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }