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.jupiter.api.Assertions;
38  import org.junit.jupiter.api.BeforeEach;
39  import org.junit.jupiter.api.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      @BeforeEach
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          Assertions.assertThrows(IOException.class, () -> EntityUtils.consume(wrapper));
75          Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint();
76      }
77  
78      @Test
79      public void testEntityStreamClosedIOErrorAlreadyReleased() throws Exception {
80          Mockito.when(entity.isStreaming()).thenReturn(true);
81          Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
82          Mockito.when(execRuntime.isEndpointAcquired()).thenReturn(false);
83          Mockito.doThrow(new SocketException()).when(inStream).close();
84          EntityUtils.consume(wrapper);
85          Mockito.verify(execRuntime).discardEndpoint();
86      }
87  
88      @Test
89      public void testReusableEntityWriteTo() throws Exception {
90          final OutputStream outStream = Mockito.mock(OutputStream.class);
91          Mockito.when(entity.isStreaming()).thenReturn(true);
92          Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
93          wrapper.writeTo(outStream);
94          Mockito.verify(execRuntime).releaseEndpoint();
95      }
96  
97      @Test
98      public void testReusableEntityWriteToIOError() throws Exception {
99          final OutputStream outStream = Mockito.mock(OutputStream.class);
100         Mockito.when(entity.isStreaming()).thenReturn(true);
101         Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
102         Mockito.doThrow(new IOException()).when(entity).writeTo(outStream);
103         Assertions.assertThrows(IOException.class, () -> wrapper.writeTo(outStream));
104         Mockito.verify(execRuntime, Mockito.never()).releaseEndpoint();
105         Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint();
106     }
107 
108     @Test
109     public void testReusableEntityEndOfStream() throws Exception {
110         Mockito.when(inStream.read()).thenReturn(-1);
111         Mockito.when(entity.isStreaming()).thenReturn(true);
112         Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
113         final InputStream content = wrapper.getContent();
114         Assertions.assertEquals(-1, content.read());
115         Mockito.verify(inStream).close();
116         Mockito.verify(execRuntime).releaseEndpoint();
117     }
118 
119     @Test
120     public void testReusableEntityEndOfStreamIOError() throws Exception {
121         Mockito.when(inStream.read()).thenReturn(-1);
122         Mockito.when(entity.isStreaming()).thenReturn(true);
123         Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
124         Mockito.doThrow(new IOException()).when(inStream).close();
125         final InputStream content = wrapper.getContent();
126         Assertions.assertThrows(IOException.class, () -> content.read());
127         Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint();
128     }
129 
130 }