Skip to content

Commit

Permalink
add test to confirm the response is close()d
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomarquezp committed Aug 20, 2024
1 parent 86e70aa commit e21f993
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -65,4 +67,9 @@ public void close() throws IOException {
public boolean markSupported() {
return wrappedStream.markSupported();
}

@VisibleForTesting
HttpResponse getResponse() {
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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());
}
}

0 comments on commit e21f993

Please sign in to comment.