Skip to content

Commit e21f993

Browse files
committed
add test to confirm the response is close()d
1 parent 86e70aa commit e21f993

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5ResponseContent.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.google.api.client.http.apache.v5;
22

3+
import com.google.common.annotations.VisibleForTesting;
34
import java.io.IOException;
45
import java.io.InputStream;
56
import org.apache.hc.core5.http.ClassicHttpResponse;
7+
import org.apache.hc.core5.http.HttpResponse;
68

79
/**
810
* Class that wraps an {@link org.apache.hc.core5.http.HttpEntity}'s content {@link InputStream}
@@ -65,4 +67,9 @@ public void close() throws IOException {
6567
public boolean markSupported() {
6668
return wrappedStream.markSupported();
6769
}
70+
71+
@VisibleForTesting
72+
HttpResponse getResponse() {
73+
return response;
74+
}
6875
}

google-http-client-apache-v5/src/test/java/com/google/api/client/http/apache/v5/Apache5HttpRequestTest.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,24 @@
2121
import com.google.api.client.http.ByteArrayContent;
2222
import com.google.api.client.http.HttpContent;
2323
import com.google.api.client.http.InputStreamContent;
24+
import com.google.api.client.http.LowLevelHttpResponse;
2425
import java.io.ByteArrayInputStream;
26+
import java.io.InputStream;
2527
import java.nio.charset.StandardCharsets;
2628
import java.util.Arrays;
29+
import java.util.concurrent.atomic.AtomicInteger;
2730
import org.apache.hc.client5.http.classic.methods.HttpPost;
2831
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
2932
import org.apache.hc.core5.http.ClassicHttpRequest;
3033
import org.apache.hc.core5.http.ClassicHttpResponse;
34+
import org.apache.hc.core5.http.ContentType;
35+
import org.apache.hc.core5.http.HttpEntity;
3136
import org.apache.hc.core5.http.HttpHost;
37+
import org.apache.hc.core5.http.io.entity.BasicHttpEntity;
3238
import org.apache.hc.core5.http.protocol.HttpContext;
3339
import org.junit.Test;
3440

3541
public class Apache5HttpRequestTest {
36-
3742
@Test
3843
public void testContentLengthSet() throws Exception {
3944
HttpUriRequestBase base = new HttpPost("http://www.google.com");
@@ -79,4 +84,48 @@ public ClassicHttpResponse executeOpen(
7984
assertTrue(base.getEntity().isChunked());
8085
assertEquals(-1, base.getEntity().getContentLength());
8186
}
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+
}
82131
}

0 commit comments

Comments
 (0)