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.hc.core5.http.impl.io;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.net.InetAddress;
33  import java.net.InetSocketAddress;
34  import java.net.Socket;
35  import java.net.SocketException;
36  import java.net.SocketTimeoutException;
37  
38  import org.apache.hc.core5.http.ClassicHttpResponse;
39  import org.apache.hc.core5.http.ContentLengthStrategy;
40  import org.apache.hc.core5.http.HttpEntity;
41  import org.apache.hc.core5.http.config.H1Config;
42  import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
43  import org.apache.hc.core5.io.ShutdownType;
44  import org.junit.Assert;
45  import org.junit.Before;
46  import org.junit.Test;
47  import org.mockito.Mock;
48  import org.mockito.Mockito;
49  import org.mockito.MockitoAnnotations;
50  
51  public class TestBHttpConnectionBase {
52  
53      @Mock
54      private Socket socket;
55  
56      private BHttpConnectionBase conn;
57  
58      @Before
59      public void setUp() throws Exception {
60          MockitoAnnotations.initMocks(this);
61          conn = new BHttpConnectionBase(H1Config.DEFAULT, null, null);
62      }
63  
64      @Test
65      public void testBasics() throws Exception {
66          Assert.assertFalse(conn.isOpen());
67          Assert.assertEquals(null, conn.getLocalAddress());
68          Assert.assertEquals(null, conn.getRemoteAddress());
69          Assert.assertEquals("[Not bound]", conn.toString());
70      }
71  
72      @Test
73      public void testSocketBind() throws Exception {
74          final InetAddress localAddress = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
75          final int localPort = 8888;
76          final InetAddress remoteAddress = InetAddress.getByAddress(new byte[] {10, 0, 0, 2});
77          final int remotePort = 80;
78          final InetSocketAddress localSockAddress = new InetSocketAddress(localAddress, localPort);
79          final InetSocketAddress remoteSockAddress = new InetSocketAddress(remoteAddress, remotePort);
80          Mockito.when(socket.getLocalSocketAddress()).thenReturn(localSockAddress);
81          Mockito.when(socket.getRemoteSocketAddress()).thenReturn(remoteSockAddress);
82          Mockito.when(socket.getLocalAddress()).thenReturn(localAddress);
83          Mockito.when(socket.getLocalPort()).thenReturn(localPort);
84          Mockito.when(socket.getInetAddress()).thenReturn(remoteAddress);
85          Mockito.when(socket.getPort()).thenReturn(remotePort);
86          conn.bind(socket);
87  
88          Assert.assertEquals("127.0.0.1:8888<->10.0.0.2:80", conn.toString());
89          Assert.assertTrue(conn.isOpen());
90  
91          Assert.assertEquals(new InetSocketAddress(
92                  InetAddress.getByAddress(new byte[] {127, 0, 0, 1}), 8888), conn.getLocalAddress());
93          Assert.assertEquals(new InetSocketAddress(
94                  InetAddress.getByAddress(new byte[] {10, 0, 0, 2}), 80), conn.getRemoteAddress());
95      }
96  
97      @Test
98      public void testConnectionClose() throws Exception {
99          final InputStream instream = Mockito.mock(InputStream.class);
100         final OutputStream outstream = Mockito.mock(OutputStream.class);
101 
102         Mockito.when(socket.getInputStream()).thenReturn(instream);
103         Mockito.when(socket.getOutputStream()).thenReturn(outstream);
104 
105         conn.bind(socket);
106         conn.ensureOpen();
107         conn.outbuffer.write(0, outstream);
108 
109         Assert.assertTrue(conn.isOpen());
110 
111         conn.close();
112 
113         Assert.assertFalse(conn.isOpen());
114 
115         Mockito.verify(outstream, Mockito.times(1)).write(
116                 Mockito.<byte[]>any(), Mockito.anyInt(), Mockito.anyInt());
117         Mockito.verify(socket, Mockito.times(1)).shutdownInput();
118         Mockito.verify(socket, Mockito.times(1)).shutdownOutput();
119         Mockito.verify(socket, Mockito.times(1)).close();
120 
121         conn.close();
122         Mockito.verify(socket, Mockito.times(1)).close();
123         Mockito.verify(outstream, Mockito.times(1)).write(
124                 Mockito.<byte[]>any(), Mockito.anyInt(), Mockito.anyInt());
125     }
126 
127     @Test
128     public void testConnectionShutdown() throws Exception {
129         final InputStream instream = Mockito.mock(InputStream.class);
130         final OutputStream outstream = Mockito.mock(OutputStream.class);
131         Mockito.when(socket.getInputStream()).thenReturn(instream);
132         Mockito.when(socket.getOutputStream()).thenReturn(outstream);
133 
134         conn.bind(socket);
135         conn.ensureOpen();
136         conn.outbuffer.write(0, outstream);
137 
138         Assert.assertTrue(conn.isOpen());
139 
140         conn.shutdown(ShutdownType.GRACEFUL);
141 
142         Assert.assertFalse(conn.isOpen());
143 
144         Mockito.verify(outstream, Mockito.never()).write(
145                 Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt());
146         Mockito.verify(socket, Mockito.never()).shutdownInput();
147         Mockito.verify(socket, Mockito.never()).shutdownOutput();
148         Mockito.verify(socket, Mockito.times(1)).close();
149 
150         conn.close();
151         Mockito.verify(socket, Mockito.times(1)).close();
152 
153         conn.shutdown(ShutdownType.GRACEFUL);
154         Mockito.verify(socket, Mockito.times(1)).close();
155     }
156 
157     @Test
158     public void testCreateEntityLengthDelimited() throws Exception {
159         final InputStream instream = Mockito.mock(InputStream.class);
160         final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK");
161         message.addHeader("Content-Length", "10");
162         message.addHeader("Content-Type", "stuff");
163         message.addHeader("Content-Encoding", "chunked");
164         final HttpEntity entity = conn.createIncomingEntity(message, conn.inbuffer, instream, 10);
165         Assert.assertNotNull(entity);
166         Assert.assertFalse(entity.isChunked());
167         Assert.assertEquals(10, entity.getContentLength());
168         Assert.assertEquals("stuff", entity.getContentType());
169         Assert.assertEquals("chunked", entity.getContentEncoding());
170         final InputStream content = entity.getContent();
171         Assert.assertNotNull(content);
172         Assert.assertTrue((content instanceof ContentLengthInputStream));
173     }
174 
175     @Test
176     public void testCreateEntityInputChunked() throws Exception {
177         final InputStream instream = Mockito.mock(InputStream.class);
178         final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK");
179         final HttpEntity entity = conn.createIncomingEntity(message, conn.inbuffer, instream, ContentLengthStrategy.CHUNKED);
180         Assert.assertNotNull(entity);
181         Assert.assertTrue(entity.isChunked());
182         Assert.assertEquals(-1, entity.getContentLength());
183         final InputStream content = entity.getContent();
184         Assert.assertNotNull(content);
185         Assert.assertTrue((content instanceof ChunkedInputStream));
186     }
187 
188     @Test
189     public void testCreateEntityInputUndefined() throws Exception {
190         final InputStream instream = Mockito.mock(InputStream.class);
191         final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK");
192         final HttpEntity entity = conn.createIncomingEntity(message, conn.inbuffer, instream, ContentLengthStrategy.UNDEFINED);
193         Assert.assertNotNull(entity);
194         Assert.assertFalse(entity.isChunked());
195         Assert.assertEquals(-1, entity.getContentLength());
196         final InputStream content = entity.getContent();
197         Assert.assertNotNull(content);
198         Assert.assertTrue((content instanceof IdentityInputStream));
199     }
200 
201     @Test
202     public void testSetSocketTimeout() throws Exception {
203         conn.bind(socket);
204 
205         conn.setSocketTimeout(123);
206 
207         Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123);
208     }
209 
210     @Test
211     public void testSetSocketTimeoutException() throws Exception {
212         conn.bind(socket);
213 
214         Mockito.doThrow(new SocketException()).when(socket).setSoTimeout(Mockito.anyInt());
215 
216         conn.setSocketTimeout(123);
217 
218         Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123);
219     }
220 
221     @Test
222     public void testGetSocketTimeout() throws Exception {
223         Assert.assertEquals(-1, conn.getSocketTimeout());
224 
225         Mockito.when(socket.getSoTimeout()).thenReturn(345);
226         conn.bind(socket);
227 
228         Assert.assertEquals(345, conn.getSocketTimeout());
229     }
230 
231     @Test
232     public void testGetSocketTimeoutException() throws Exception {
233         Assert.assertEquals(-1, conn.getSocketTimeout());
234 
235         Mockito.when(socket.getSoTimeout()).thenThrow(new SocketException());
236         conn.bind(socket);
237 
238         Assert.assertEquals(-1, conn.getSocketTimeout());
239     }
240 
241     @Test
242     public void testAwaitInputInBuffer() throws Exception {
243         final ByteArrayInputStream instream = Mockito.spy(new ByteArrayInputStream(
244                 new byte[] {1, 2, 3, 4, 5}));
245         Mockito.when(socket.getInputStream()).thenReturn(instream);
246 
247         conn.bind(socket);
248         conn.ensureOpen();
249         conn.inbuffer.read(instream);
250 
251         Assert.assertTrue(conn.awaitInput(432));
252 
253         Mockito.verify(socket, Mockito.never()).setSoTimeout(Mockito.anyInt());
254         Mockito.verify(instream, Mockito.times(1)).read(
255                 Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt());
256     }
257 
258     @Test
259     public void testAwaitInputInSocket() throws Exception {
260         final ByteArrayInputStream instream = Mockito.spy(new ByteArrayInputStream(
261                 new byte[] {1, 2, 3, 4, 5}));
262         Mockito.when(socket.getInputStream()).thenReturn(instream);
263         Mockito.when(socket.getSoTimeout()).thenReturn(345);
264 
265         conn.bind(socket);
266         conn.ensureOpen();
267 
268         Assert.assertTrue(conn.awaitInput(432));
269 
270         Mockito.verify(socket, Mockito.times(1)).setSoTimeout(432);
271         Mockito.verify(socket, Mockito.times(1)).setSoTimeout(345);
272         Mockito.verify(instream, Mockito.times(1)).read(
273                 Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt());
274     }
275 
276     @Test
277     public void testAwaitInputNoData() throws Exception {
278         final InputStream instream = Mockito.mock(InputStream.class);
279         Mockito.when(socket.getInputStream()).thenReturn(instream);
280         Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
281             .thenReturn(-1);
282 
283         conn.bind(socket);
284         conn.ensureOpen();
285 
286         Assert.assertFalse(conn.awaitInput(432));
287     }
288 
289     @Test
290     public void testStaleWhenClosed() throws Exception {
291         final OutputStream outstream = Mockito.mock(OutputStream.class);
292 
293         Mockito.when(socket.getOutputStream()).thenReturn(outstream);
294 
295         conn.bind(socket);
296         conn.ensureOpen();
297         conn.close();
298         Assert.assertTrue(conn.isStale());
299     }
300 
301     @Test
302     public void testNotStaleWhenHasData() throws Exception {
303         final ByteArrayInputStream instream = Mockito.spy(new ByteArrayInputStream(
304                 new byte[] {1, 2, 3, 4, 5}));
305         Mockito.when(socket.getInputStream()).thenReturn(instream);
306 
307         conn.bind(socket);
308         conn.ensureOpen();
309 
310         Assert.assertFalse(conn.isStale());
311     }
312 
313     @Test
314     public void testStaleWhenEndOfStream() throws Exception {
315         final InputStream instream = Mockito.mock(InputStream.class);
316         Mockito.when(socket.getInputStream()).thenReturn(instream);
317         Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
318             .thenReturn(-1);
319 
320         conn.bind(socket);
321         conn.ensureOpen();
322 
323         Assert.assertTrue(conn.isStale());
324     }
325 
326     @Test
327     public void testNotStaleWhenTimeout() throws Exception {
328         final InputStream instream = Mockito.mock(InputStream.class);
329         Mockito.when(socket.getInputStream()).thenReturn(instream);
330         Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
331             .thenThrow(new SocketTimeoutException());
332 
333         conn.bind(socket);
334         conn.ensureOpen();
335 
336         Assert.assertFalse(conn.isStale());
337     }
338 
339     @Test
340     public void testStaleWhenIOError() throws Exception {
341         final InputStream instream = Mockito.mock(InputStream.class);
342         Mockito.when(socket.getInputStream()).thenReturn(instream);
343         Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
344             .thenThrow(new SocketException());
345 
346         conn.bind(socket);
347         conn.ensureOpen();
348 
349         Assert.assertTrue(conn.isStale());
350     }
351 
352 }