Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPCLIENT-2312Fix Response Body Truncation Issue in ContentResponseHandler #521

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Content handleEntity(final HttpEntity entity) throws IOException {
public Content handleResponse(final ClassicHttpResponse response) throws IOException {
final int statusCode = response.getCode();
final HttpEntity entity = response.getEntity();
final byte[] contentBytes = (entity != null) ? EntityUtils.toByteArray(entity, MAX_MESSAGE_LENGTH) : new byte[0];
final byte[] contentBytes = (entity != null) ? ((statusCode >= 300) ? EntityUtils.toByteArray(entity, MAX_MESSAGE_LENGTH) : EntityUtils.toByteArray(entity)) : new byte[0];
final ContentType contentType = (entity != null && entity.getContentType() != null) ? ContentType.parse(entity.getContentType()) : ContentType.DEFAULT_BINARY;
final Content content = new Content(contentBytes, contentType);
if (statusCode >= 300) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import java.nio.charset.StandardCharsets;

import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.HttpResponseException;
import org.apache.hc.client5.http.fluent.Content;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.client5.testing.sync.extension.TestClientResources;
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.HttpStatus;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
Expand Down Expand Up @@ -79,6 +81,20 @@ public void setUp() throws Exception {
}
response.setEntity(responseEntity);
});

// Handler for large content large message
server.registerHandler("/large-message", (request, response, context) -> {
final String largeContent = generateLargeString(10000); // Large content string
response.setEntity(new StringEntity(largeContent, ContentType.TEXT_PLAIN));
});

// Handler for large content large message with error
server.registerHandler("/large-message-error", (request, response, context) -> {
final String largeContent = generateLargeString(10000); // Large content string
response.setCode(HttpStatus.SC_REDIRECTION);
response.setEntity(new StringEntity(largeContent, ContentType.TEXT_PLAIN));
});

}

@Test
Expand Down Expand Up @@ -156,4 +172,38 @@ public void testConnectionRelease() throws Exception {
}
}

private String generateLargeString(final int size) {
final StringBuilder sb = new StringBuilder(size);
for (int i = 0; i < size; i++) {
sb.append("x");
}
return sb.toString();
}

@Test
public void testLargeResponse() throws Exception {

final HttpHost target = targetHost();
final String baseURL = "http://localhost:" + target.getPort();

final Content content = Request.get(baseURL + "/large-message").execute().returnContent();
Assertions.assertEquals(10000, content.asBytes().length);
}

@Test
public void testLargeResponseError() throws Exception {
final HttpHost target = targetHost();
final String baseURL = "http://localhost:" + target.getPort();

try {
Request.get(baseURL + "/large-message-error").execute().returnContent();
Assertions.fail("Expected an HttpResponseException to be thrown");
} catch (final HttpResponseException e) {
// Check if the content of the exception is less than or equal to 256 bytes
final byte[] contentBytes = e.getContentBytes();
Assertions.assertNotNull(contentBytes, "Content bytes should not be null");
Assertions.assertTrue(contentBytes.length <= 256, "Content length should be less or equal to 256 bytes");
}
}

}