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.client5.http.impl.classic;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.net.SocketException;
33  
34  import org.apache.hc.client5.http.classic.ExecRuntime;
35  import org.apache.hc.core5.http.HttpEntity;
36  import org.apache.hc.core5.http.io.entity.EntityUtils;
37  import org.junit.Assert;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.mockito.Mockito;
41  
42  @SuppressWarnings("boxing") // test code
43  public class TestResponseEntityWrapper {
44  
45      private InputStream inStream;
46      private HttpEntity entity;
47      private ExecRuntime execRuntime;
48      private ResponseEntityProxy wrapper;
49  
50      @Before
51      public void setup() throws Exception {
52          inStream = Mockito.mock(InputStream.class);
53          entity = Mockito.mock(HttpEntity.class);
54          Mockito.when(entity.getContent()).thenReturn(inStream);
55          execRuntime = Mockito.mock(ExecRuntime.class);
56          wrapper = new ResponseEntityProxy(entity, execRuntime);
57      }
58  
59      @Test
60      public void testReusableEntityStreamClosed() throws Exception {
61          Mockito.when(entity.isStreaming()).thenReturn(true);
62          Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
63          EntityUtils.consume(wrapper);
64  
65          Mockito.verify(inStream, Mockito.times(1)).close();
66          Mockito.verify(execRuntime).releaseEndpoint();
67      }
68  
69      @Test
70      public void testReusableEntityStreamClosedIOError() throws Exception {
71          Mockito.when(entity.isStreaming()).thenReturn(true);
72          Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
73          Mockito.doThrow(new IOException()).when(inStream).close();
74          try {
75              EntityUtils.consume(wrapper);
76              Assert.fail("IOException expected");
77          } catch (final IOException ex) {
78          }
79          Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint();
80      }
81  
82      @Test
83      public void testEntityStreamClosedIOErrorAlreadyReleased() throws Exception {
84          Mockito.when(entity.isStreaming()).thenReturn(true);
85          Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
86          Mockito.when(execRuntime.isEndpointAcquired()).thenReturn(false);
87          Mockito.doThrow(new SocketException()).when(inStream).close();
88          EntityUtils.consume(wrapper);
89          Mockito.verify(execRuntime).discardEndpoint();
90      }
91  
92      @Test
93      public void testReusableEntityWriteTo() throws Exception {
94          final OutputStream outStream = Mockito.mock(OutputStream.class);
95          Mockito.when(entity.isStreaming()).thenReturn(true);
96          Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
97          wrapper.writeTo(outStream);
98          Mockito.verify(execRuntime).releaseEndpoint();
99      }
100 
101     @Test
102     public void testReusableEntityWriteToIOError() throws Exception {
103         final OutputStream outStream = Mockito.mock(OutputStream.class);
104         Mockito.when(entity.isStreaming()).thenReturn(true);
105         Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
106         Mockito.doThrow(new IOException()).when(entity).writeTo(outStream);
107         try {
108             wrapper.writeTo(outStream);
109             Assert.fail("IOException expected");
110         } catch (final IOException ex) {
111         }
112         Mockito.verify(execRuntime, Mockito.never()).releaseEndpoint();
113         Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint();
114     }
115 
116     @Test
117     public void testReusableEntityEndOfStream() throws Exception {
118         Mockito.when(inStream.read()).thenReturn(-1);
119         Mockito.when(entity.isStreaming()).thenReturn(true);
120         Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
121         final InputStream content = wrapper.getContent();
122         Assert.assertEquals(-1, content.read());
123         Mockito.verify(inStream).close();
124         Mockito.verify(execRuntime).releaseEndpoint();
125     }
126 
127     @Test
128     public void testReusableEntityEndOfStreamIOError() throws Exception {
129         Mockito.when(inStream.read()).thenReturn(-1);
130         Mockito.when(entity.isStreaming()).thenReturn(true);
131         Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
132         Mockito.doThrow(new IOException()).when(inStream).close();
133         final InputStream content = wrapper.getContent();
134         try {
135             content.read();
136             Assert.fail("IOException expected");
137         } catch (final IOException ex) {
138         }
139         Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint();
140     }
141 
142 }