diff --git a/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java b/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java index 9de1be6c9..c2d3091df 100644 --- a/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java +++ b/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java @@ -1,8 +1,10 @@ package com.google.api.client.http.apache.v5; +import com.google.common.annotations.VisibleForTesting; import java.io.IOException; import java.io.InputStream; import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.HttpResponse; /** * Class that wraps an {@link org.apache.hc.core5.http.HttpEntity}'s content {@link InputStream} @@ -65,4 +67,9 @@ public void close() throws IOException { public boolean markSupported() { return wrappedStream.markSupported(); } + + @VisibleForTesting + HttpResponse getResponse() { + return response; + } } diff --git a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpRequestTest.java b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpRequestTest.java index 327922dc0..3b7ca4a21 100644 --- a/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpRequestTest.java +++ b/google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpRequestTest.java @@ -21,19 +21,24 @@ import com.google.api.client.http.ByteArrayContent; import com.google.api.client.http.HttpContent; import com.google.api.client.http.InputStreamContent; +import com.google.api.client.http.LowLevelHttpResponse; import java.io.ByteArrayInputStream; +import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; import org.apache.hc.core5.http.ClassicHttpRequest; import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.io.entity.BasicHttpEntity; import org.apache.hc.core5.http.protocol.HttpContext; import org.junit.Test; public class Apache5HttpRequestTest { - @Test public void testContentLengthSet() throws Exception { HttpUriRequestBase base = new HttpPost("http://www.google.com"); @@ -79,4 +84,48 @@ public ClassicHttpResponse executeOpen( assertTrue(base.getEntity().isChunked()); assertEquals(-1, base.getEntity().getContentLength()); } + + @Test + public void testExecute_closeContent_closesResponse() throws Exception { + HttpUriRequestBase base = new HttpPost("http://www.google.com"); + final InputStream responseContentStream = new ByteArrayInputStream(new byte[] {1, 2, 3}); + BasicHttpEntity testEntity = + new BasicHttpEntity(responseContentStream, ContentType.DEFAULT_BINARY); + AtomicInteger closedResponseCounter = new AtomicInteger(0); + ClassicHttpResponse classicResponse = + new MockClassicHttpResponse() { + @Override + public HttpEntity getEntity() { + return testEntity; + } + + @Override + public void close() { + closedResponseCounter.incrementAndGet(); + } + }; + + Apache5HttpRequest request = + new Apache5HttpRequest( + new MockHttpClient() { + @Override + public ClassicHttpResponse executeOpen( + HttpHost target, ClassicHttpRequest request, HttpContext context) { + return classicResponse; + } + }, + base); + LowLevelHttpResponse response = request.execute(); + assertTrue(response instanceof Apache5HttpResponse); + + // we confirm that the classic response we prepared in this test is the same as the content's + // response + assertTrue(response.getContent() instanceof Apache5ResponseContent); + assertEquals(classicResponse, ((Apache5ResponseContent) response.getContent()).getResponse()); + + // we close the response's content stream and confirm the response is also closed + assertEquals(0, closedResponseCounter.get()); + response.getContent().close(); + assertEquals(1, closedResponseCounter.get()); + } }