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.DefaultHttpRequestParserFactory;
45  import org.apache.http.impl.io.DefaultHttpResponseWriterFactory;
46  import org.apache.http.message.BasicHttpResponse;
47  import org.junit.Before;
48  import org.junit.Test;
49  import org.mockito.Mock;
50  import org.mockito.Mockito;
51  import org.mockito.MockitoAnnotations;
52  
53  import org.junit.Assert;
54  
55  public class TestDefaultBHttpServerConnection {
56  
57      @Mock
58      private Socket socket;
59  
60      private DefaultBHttpServerConnection conn;
61  
62      @Before
63      public void setUp() throws Exception {
64          MockitoAnnotations.initMocks(this);
65          conn = new DefaultBHttpServerConnection(1024, 1024,
66              null, null,
67              MessageConstraints.DEFAULT,
68              LaxContentLengthStrategy.INSTANCE,
69              StrictContentLengthStrategy.INSTANCE,
70              DefaultHttpRequestParserFactory.INSTANCE,
71              DefaultHttpResponseWriterFactory.INSTANCE);
72      }
73  
74      @Test
75      public void testBasics() throws Exception {
76          Assert.assertFalse(conn.isOpen());
77          Assert.assertEquals("[Not bound]", conn.toString());
78      }
79  
80      @Test
81      public void testReadRequestHead() throws Exception {
82          final String s = "GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n";
83          final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(Consts.ASCII));
84          Mockito.when(socket.getInputStream()).thenReturn(inStream);
85  
86          conn.bind(socket);
87  
88          Assert.assertEquals(0, conn.getMetrics().getRequestCount());
89  
90          final HttpRequest request = conn.receiveRequestHeader();
91          Assert.assertNotNull(request);
92          Assert.assertEquals(HttpVersion.HTTP_1_1, request.getProtocolVersion());
93          Assert.assertEquals("/", request.getRequestLine().getUri());
94          Assert.assertEquals("GET", request.getRequestLine().getMethod());
95          Assert.assertTrue(request.containsHeader("User-Agent"));
96          Assert.assertEquals(1, conn.getMetrics().getRequestCount());
97      }
98  
99      @Test
100     public void testReadRequestEntity() throws Exception {
101         final String s = "POST / HTTP/1.1\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().getRequestCount());
108 
109         final HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) conn.receiveRequestHeader();
110 
111         Assert.assertNotNull(request);
112         Assert.assertEquals(HttpVersion.HTTP_1_1, request.getProtocolVersion());
113         Assert.assertEquals("/", request.getRequestLine().getUri());
114         Assert.assertEquals("POST", request.getRequestLine().getMethod());
115         Assert.assertTrue(request.containsHeader("User-Agent"));
116         Assert.assertNull(request.getEntity());
117         Assert.assertEquals(1, conn.getMetrics().getRequestCount());
118 
119         conn.receiveRequestEntity(request);
120 
121         final HttpEntity entity = request.getEntity();
122         Assert.assertNotNull(entity);
123         Assert.assertEquals(3, entity.getContentLength());
124         Assert.assertEquals(1, conn.getMetrics().getRequestCount());
125     }
126 
127     @Test
128     public void testWriteResponseHead() throws Exception {
129         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
130         Mockito.when(socket.getOutputStream()).thenReturn(outStream);
131 
132         conn.bind(socket);
133 
134         Assert.assertEquals(0, conn.getMetrics().getResponseCount());
135 
136         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
137         response.addHeader("User-Agent", "test");
138 
139         conn.sendResponseHeader(response);
140         conn.flush();
141 
142         Assert.assertEquals(1, conn.getMetrics().getResponseCount());
143         final String s = new String(outStream.toByteArray(), "ASCII");
144         Assert.assertEquals("HTTP/1.1 200 OK\r\nUser-Agent: test\r\n\r\n", s);
145     }
146 
147     @Test
148     public void testWriteResponse100Head() throws Exception {
149         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
150         Mockito.when(socket.getOutputStream()).thenReturn(outStream);
151 
152         conn.bind(socket);
153 
154         Assert.assertEquals(0, conn.getMetrics().getResponseCount());
155 
156         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Go on");
157 
158         conn.sendResponseHeader(response);
159         conn.flush();
160 
161         Assert.assertEquals(0, conn.getMetrics().getResponseCount());
162         final String s = new String(outStream.toByteArray(), "ASCII");
163         Assert.assertEquals("HTTP/1.1 100 Go on\r\n\r\n", s);
164     }
165 
166     @Test
167     public void testWriteResponseEntity() throws Exception {
168         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
169         Mockito.when(socket.getOutputStream()).thenReturn(outStream);
170 
171         conn.bind(socket);
172 
173         Assert.assertEquals(0, conn.getMetrics().getResponseCount());
174 
175         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
176         response.addHeader("User-Agent", "test");
177         response.addHeader("Content-Length", "3");
178         response.setEntity(new StringEntity("123", ContentType.TEXT_PLAIN));
179 
180         conn.sendResponseHeader(response);
181         conn.sendResponseEntity(response);
182         conn.flush();
183 
184         Assert.assertEquals(1, conn.getMetrics().getResponseCount());
185         final String s = new String(outStream.toByteArray(), "ASCII");
186         Assert.assertEquals("HTTP/1.1 200 OK\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123", s);
187     }
188 
189 }