|
21 | 21 | import com.google.api.client.http.ByteArrayContent;
|
22 | 22 | import com.google.api.client.http.HttpContent;
|
23 | 23 | import com.google.api.client.http.InputStreamContent;
|
| 24 | +import com.google.api.client.http.LowLevelHttpResponse; |
24 | 25 | import java.io.ByteArrayInputStream;
|
| 26 | +import java.io.InputStream; |
25 | 27 | import java.nio.charset.StandardCharsets;
|
26 | 28 | import java.util.Arrays;
|
| 29 | +import java.util.concurrent.atomic.AtomicInteger; |
27 | 30 | import org.apache.hc.client5.http.classic.methods.HttpPost;
|
28 | 31 | import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
|
29 | 32 | import org.apache.hc.core5.http.ClassicHttpRequest;
|
30 | 33 | import org.apache.hc.core5.http.ClassicHttpResponse;
|
| 34 | +import org.apache.hc.core5.http.ContentType; |
| 35 | +import org.apache.hc.core5.http.HttpEntity; |
31 | 36 | import org.apache.hc.core5.http.HttpHost;
|
| 37 | +import org.apache.hc.core5.http.io.entity.BasicHttpEntity; |
32 | 38 | import org.apache.hc.core5.http.protocol.HttpContext;
|
33 | 39 | import org.junit.Test;
|
34 | 40 |
|
35 | 41 | public class Apache5HttpRequestTest {
|
36 |
| - |
37 | 42 | @Test
|
38 | 43 | public void testContentLengthSet() throws Exception {
|
39 | 44 | HttpUriRequestBase base = new HttpPost("http://www.google.com");
|
@@ -79,4 +84,48 @@ public ClassicHttpResponse executeOpen(
|
79 | 84 | assertTrue(base.getEntity().isChunked());
|
80 | 85 | assertEquals(-1, base.getEntity().getContentLength());
|
81 | 86 | }
|
| 87 | + |
| 88 | + @Test |
| 89 | + public void testExecute_closeContent_closesResponse() throws Exception { |
| 90 | + HttpUriRequestBase base = new HttpPost("http://www.google.com"); |
| 91 | + final InputStream responseContentStream = new ByteArrayInputStream(new byte[] {1, 2, 3}); |
| 92 | + BasicHttpEntity testEntity = |
| 93 | + new BasicHttpEntity(responseContentStream, ContentType.DEFAULT_BINARY); |
| 94 | + AtomicInteger closedResponseCounter = new AtomicInteger(0); |
| 95 | + ClassicHttpResponse classicResponse = |
| 96 | + new MockClassicHttpResponse() { |
| 97 | + @Override |
| 98 | + public HttpEntity getEntity() { |
| 99 | + return testEntity; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void close() { |
| 104 | + closedResponseCounter.incrementAndGet(); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + Apache5HttpRequest request = |
| 109 | + new Apache5HttpRequest( |
| 110 | + new MockHttpClient() { |
| 111 | + @Override |
| 112 | + public ClassicHttpResponse executeOpen( |
| 113 | + HttpHost target, ClassicHttpRequest request, HttpContext context) { |
| 114 | + return classicResponse; |
| 115 | + } |
| 116 | + }, |
| 117 | + base); |
| 118 | + LowLevelHttpResponse response = request.execute(); |
| 119 | + assertTrue(response instanceof Apache5HttpResponse); |
| 120 | + |
| 121 | + // we confirm that the classic response we prepared in this test is the same as the content's |
| 122 | + // response |
| 123 | + assertTrue(response.getContent() instanceof Apache5ResponseContent); |
| 124 | + assertEquals(classicResponse, ((Apache5ResponseContent) response.getContent()).getResponse()); |
| 125 | + |
| 126 | + // we close the response's content stream and confirm the response is also closed |
| 127 | + assertEquals(0, closedResponseCounter.get()); |
| 128 | + response.getContent().close(); |
| 129 | + assertEquals(1, closedResponseCounter.get()); |
| 130 | + } |
82 | 131 | }
|
0 commit comments