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.impl;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.ByteArrayOutputStream;
31  import java.net.Socket;
32  
33  import org.apache.http.Consts;
34  import org.apache.http.HttpEntity;
35  import org.apache.http.HttpEntityEnclosingRequest;
36  import org.apache.http.HttpRequest;
37  import org.apache.http.HttpResponse;
38  import org.apache.http.HttpVersion;
39  import org.apache.http.config.MessageConstraints;
40  import org.apache.http.entity.ContentType;
41  import org.apache.http.entity.StringEntity;
42  import org.apache.http.impl.entity.LaxContentLengthStrategy;
43  import org.apache.http.impl.entity.StrictContentLengthStrategy;
44  import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;
45  import org.apache.http.impl.io.DefaultHttpResponseParserFactory;
46  import org.apache.http.message.BasicHttpEntityEnclosingRequest;
47  import org.apache.http.message.BasicHttpRequest;
48  import org.junit.Before;
49  import org.junit.Test;
50  import org.mockito.Mock;
51  import org.mockito.Mockito;
52  import org.mockito.MockitoAnnotations;
53  
54  import org.junit.Assert;
55  
56  public class TestDefaultBHttpClientConnection {
57  
58      @Mock
59      private Socket socket;
60  
61      private DefaultBHttpClientConnection conn;
62  
63      @Before
64      public void setUp() throws Exception {
65          MockitoAnnotations.initMocks(this);
66          conn = new DefaultBHttpClientConnection(1024, 1024,
67              null, null,
68              MessageConstraints.DEFAULT,
69              LaxContentLengthStrategy.INSTANCE,
70              StrictContentLengthStrategy.INSTANCE,
71              DefaultHttpRequestWriterFactory.INSTANCE,
72              DefaultHttpResponseParserFactory.INSTANCE);
73      }
74  
75      @Test
76      public void testBasics() throws Exception {
77          Assert.assertFalse(conn.isOpen());
78          Assert.assertEquals("[Not bound]", conn.toString());
79      }
80  
81      @Test
82      public void testReadRequestHead() throws Exception {
83          final String s = "HTTP/1.1 200 OK\r\nUser-Agent: test\r\n\r\n";
84          final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(Consts.ASCII));
85          Mockito.when(socket.getInputStream()).thenReturn(inStream);
86  
87          conn.bind(socket);
88  
89          Assert.assertEquals(0, conn.getMetrics().getResponseCount());
90  
91          final HttpResponse response = conn.receiveResponseHeader();
92          Assert.assertNotNull(response);
93          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
94          Assert.assertEquals(200, response.getStatusLine().getStatusCode());
95          Assert.assertTrue(response.containsHeader("User-Agent"));
96          Assert.assertEquals(1, conn.getMetrics().getResponseCount());
97      }
98  
99      @Test
100     public void testReadRequestEntity() throws Exception {
101         final String s = "HTTP/1.1 200 OK\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123";
102         final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(Consts.ASCII));
103         Mockito.when(socket.getInputStream()).thenReturn(inStream);
104 
105         conn.bind(socket);
106 
107         Assert.assertEquals(0, conn.getMetrics().getResponseCount());
108 
109         final HttpResponse response = conn.receiveResponseHeader();
110 
111         Assert.assertNotNull(response);
112         Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
113         Assert.assertEquals(200, response.getStatusLine().getStatusCode());
114         Assert.assertTrue(response.containsHeader("User-Agent"));
115         Assert.assertEquals(1, conn.getMetrics().getResponseCount());
116 
117         conn.receiveResponseEntity(response);
118 
119         final HttpEntity entity = response.getEntity();
120         Assert.assertNotNull(entity);
121         Assert.assertEquals(3, entity.getContentLength());
122         Assert.assertEquals(1, conn.getMetrics().getResponseCount());
123     }
124 
125     @Test
126     public void testWriteResponseHead() throws Exception {
127         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
128         Mockito.when(socket.getOutputStream()).thenReturn(outStream);
129 
130         conn.bind(socket);
131 
132         Assert.assertEquals(0, conn.getMetrics().getRequestCount());
133 
134         final HttpRequest request = new BasicHttpRequest("GET", "/stuff", HttpVersion.HTTP_1_1);
135         request.addHeader("User-Agent", "test");
136 
137         conn.sendRequestHeader(request);
138         conn.flush();
139 
140         Assert.assertEquals(1, conn.getMetrics().getRequestCount());
141         final String s = new String(outStream.toByteArray(), "ASCII");
142         Assert.assertEquals("GET /stuff HTTP/1.1\r\nUser-Agent: test\r\n\r\n", s);
143     }
144 
145     @Test
146     public void testWriteResponseEntity() throws Exception {
147         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
148         Mockito.when(socket.getOutputStream()).thenReturn(outStream);
149 
150         conn.bind(socket);
151 
152         Assert.assertEquals(0, conn.getMetrics().getRequestCount());
153 
154         final HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
155                 "/stuff", HttpVersion.HTTP_1_1);
156         request.addHeader("User-Agent", "test");
157         request.addHeader("Content-Length", "3");
158         request.setEntity(new StringEntity("123", ContentType.TEXT_PLAIN));
159 
160         conn.sendRequestHeader(request);
161         conn.sendRequestEntity(request);
162         conn.flush();
163 
164         Assert.assertEquals(1, conn.getMetrics().getRequestCount());
165         final String s = new String(outStream.toByteArray(), "ASCII");
166         Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123", s);
167     }
168 
169 }