1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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")
43 class TestResponseEntityWrapper {
44
45 private InputStream inStream;
46 private HttpEntity entity;
47 private ExecRuntime execRuntime;
48 private ResponseEntityProxy wrapper;
49
50 @BeforeEach
51 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 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 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 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 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 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 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 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 }