|
12 | 12 | import software.amazon.awssdk.crt.http.*; |
13 | 13 |
|
14 | 14 | import java.net.URI; |
| 15 | +import java.nio.ByteBuffer; |
15 | 16 | import java.nio.charset.StandardCharsets; |
16 | 17 | import java.util.Arrays; |
17 | 18 | import java.util.concurrent.CompletableFuture; |
@@ -278,4 +279,71 @@ public void onResponseComplete(HttpStreamBase stream, int errorCode) { |
278 | 279 | shutdownComplete.get(60, TimeUnit.SECONDS); |
279 | 280 | CrtResource.waitForNoResources(); |
280 | 281 | } |
| 282 | + |
| 283 | + /** |
| 284 | + * Tests that makeRequest throws IllegalStateException when called with both |
| 285 | + * a body stream and useManualDataWrites=true on an HTTP/1.1 connection. |
| 286 | + */ |
| 287 | + @Test(expected = IllegalStateException.class) |
| 288 | + public void testHttp1MakeRequestWithBodyStreamAndManualWrites() throws Exception { |
| 289 | + skipIfAndroid(); |
| 290 | + skipIfLocalhostUnavailable(); |
| 291 | + |
| 292 | + URI uri = new URI(HOST + ":" + H1_TLS_PORT); |
| 293 | + |
| 294 | + HttpRequestBodyStream bodyStream = new HttpRequestBodyStream() { |
| 295 | + @Override |
| 296 | + public boolean sendRequestBody(ByteBuffer bodyBytesOut) { |
| 297 | + return true; |
| 298 | + } |
| 299 | + }; |
| 300 | + |
| 301 | + HttpHeader[] headers = new HttpHeader[]{ |
| 302 | + new HttpHeader("Host", uri.getHost()), |
| 303 | + }; |
| 304 | + // Create request WITH body stream AND useManualDataWrites=true |
| 305 | + HttpRequest request = new HttpRequest("PUT", "/echo", headers, bodyStream); |
| 306 | + |
| 307 | + CompletableFuture<Void> reqCompleted = new CompletableFuture<>(); |
| 308 | + TestHttpResponse response = new TestHttpResponse(); |
| 309 | + |
| 310 | + CompletableFuture<Void> shutdownComplete; |
| 311 | + try (HttpClientConnectionManager connPool = createConnectionPoolManager(uri, HttpVersion.HTTP_1_1)) { |
| 312 | + shutdownComplete = connPool.getShutdownCompleteFuture(); |
| 313 | + try (HttpClientConnection conn = connPool.acquireConnection().get(60, TimeUnit.SECONDS)) { |
| 314 | + |
| 315 | + HttpStreamResponseHandler streamHandler = new HttpStreamResponseHandler() { |
| 316 | + @Override |
| 317 | + public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, |
| 318 | + HttpHeader[] nextHeaders) { |
| 319 | + response.statusCode = responseStatusCode; |
| 320 | + response.headers.addAll(Arrays.asList(nextHeaders)); |
| 321 | + } |
| 322 | + |
| 323 | + @Override |
| 324 | + public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) { |
| 325 | + response.bodyBuffer.put(bodyBytesIn); |
| 326 | + return bodyBytesIn.length; |
| 327 | + } |
| 328 | + |
| 329 | + @Override |
| 330 | + public void onResponseComplete(HttpStream stream, int errorCode) { |
| 331 | + response.onCompleteErrorCode = errorCode; |
| 332 | + reqCompleted.complete(null); |
| 333 | + } |
| 334 | + }; |
| 335 | + |
| 336 | + HttpStream stream = conn.makeRequest(request, streamHandler, true); |
| 337 | + stream.activate(); |
| 338 | + |
| 339 | + stream.writeData("hello".getBytes(StandardCharsets.UTF_8), true, null); |
| 340 | + |
| 341 | + reqCompleted.get(60, TimeUnit.SECONDS); |
| 342 | + stream.close(); |
| 343 | + } |
| 344 | + } |
| 345 | + |
| 346 | + shutdownComplete.get(60, TimeUnit.SECONDS); |
| 347 | + CrtResource.waitForNoResources(); |
| 348 | + } |
281 | 349 | } |
0 commit comments