|
31 | 31 | import java.nio.charset.StandardCharsets;
|
32 | 32 |
|
33 | 33 | import org.apache.hc.client5.http.ClientProtocolException;
|
| 34 | +import org.apache.hc.client5.http.HttpResponseException; |
34 | 35 | import org.apache.hc.client5.http.fluent.Content;
|
35 | 36 | import org.apache.hc.client5.http.fluent.Request;
|
36 | 37 | import org.apache.hc.client5.testing.sync.extension.TestClientResources;
|
37 | 38 | import org.apache.hc.core5.http.ContentType;
|
38 | 39 | import org.apache.hc.core5.http.HttpEntity;
|
39 | 40 | import org.apache.hc.core5.http.HttpHost;
|
| 41 | +import org.apache.hc.core5.http.HttpStatus; |
40 | 42 | import org.apache.hc.core5.http.URIScheme;
|
41 | 43 | import org.apache.hc.core5.http.io.entity.EntityUtils;
|
42 | 44 | import org.apache.hc.core5.http.io.entity.StringEntity;
|
@@ -79,6 +81,20 @@ public void setUp() throws Exception {
|
79 | 81 | }
|
80 | 82 | response.setEntity(responseEntity);
|
81 | 83 | });
|
| 84 | + |
| 85 | + // Handler for large content large message |
| 86 | + server.registerHandler("/large-message", (request, response, context) -> { |
| 87 | + final String largeContent = generateLargeString(10000); // Large content string |
| 88 | + response.setEntity(new StringEntity(largeContent, ContentType.TEXT_PLAIN)); |
| 89 | + }); |
| 90 | + |
| 91 | + // Handler for large content large message with error |
| 92 | + server.registerHandler("/large-message-error", (request, response, context) -> { |
| 93 | + final String largeContent = generateLargeString(10000); // Large content string |
| 94 | + response.setCode(HttpStatus.SC_REDIRECTION); |
| 95 | + response.setEntity(new StringEntity(largeContent, ContentType.TEXT_PLAIN)); |
| 96 | + }); |
| 97 | + |
82 | 98 | }
|
83 | 99 |
|
84 | 100 | @Test
|
@@ -156,4 +172,38 @@ public void testConnectionRelease() throws Exception {
|
156 | 172 | }
|
157 | 173 | }
|
158 | 174 |
|
| 175 | + private String generateLargeString(final int size) { |
| 176 | + final StringBuilder sb = new StringBuilder(size); |
| 177 | + for (int i = 0; i < size; i++) { |
| 178 | + sb.append("x"); |
| 179 | + } |
| 180 | + return sb.toString(); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testLargeResponse() throws Exception { |
| 185 | + |
| 186 | + final HttpHost target = targetHost(); |
| 187 | + final String baseURL = "http://localhost:" + target.getPort(); |
| 188 | + |
| 189 | + final Content content = Request.get(baseURL + "/large-message").execute().returnContent(); |
| 190 | + Assertions.assertEquals(10000, content.asBytes().length); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testLargeResponseError() throws Exception { |
| 195 | + final HttpHost target = targetHost(); |
| 196 | + final String baseURL = "http://localhost:" + target.getPort(); |
| 197 | + |
| 198 | + try { |
| 199 | + Request.get(baseURL + "/large-message-error").execute().returnContent(); |
| 200 | + Assertions.fail("Expected an HttpResponseException to be thrown"); |
| 201 | + } catch (final HttpResponseException e) { |
| 202 | + // Check if the content of the exception is less than or equal to 256 bytes |
| 203 | + final byte[] contentBytes = e.getContentBytes(); |
| 204 | + Assertions.assertNotNull(contentBytes, "Content bytes should not be null"); |
| 205 | + Assertions.assertTrue(contentBytes.length <= 256, "Content length should be less or equal to 256 bytes"); |
| 206 | + } |
| 207 | + } |
| 208 | + |
159 | 209 | }
|
0 commit comments