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.Assert;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41
42 @SuppressWarnings("boxing")
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 }