Skip to content

Commit 35b4efc

Browse files
committed
add test and the right pre-emptive check
1 parent 97257be commit 35b4efc

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

src/main/java/software/amazon/awssdk/crt/http/HttpClientConnection.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,25 @@ public HttpStream makeRequest(HttpRequest request, HttpStreamResponseHandler str
6363
* requests in the meantime.
6464
*
6565
* <p>If the request was created with an {@link HttpRequestBodyStream} and this
66-
* parameter is {@code true}, the first call to {@link HttpStreamBase#writeData}
67-
* will throw an {@link IllegalStateException}.
66+
* parameter is {@code true}, an {@link IllegalStateException} is thrown
67+
* immediately.
6868
* @throws CrtRuntimeException if stream creation fails
6969
* @return The HttpStream that represents this Request/Response Pair. It can be closed at any time during the
7070
* request/response, but must be closed by the user thread making this request when it's done.
7171
*/
7272
public HttpStream makeRequest(HttpRequest request, HttpStreamResponseHandler streamHandler,
73-
boolean useManualDataWrites) throws CrtRuntimeException {
73+
boolean useManualDataWrites) throws CrtRuntimeException, IllegalStateException {
7474
if (isNull()) {
7575
throw new IllegalStateException("HttpClientConnection has been closed, can't make requests on it.");
7676
}
7777
if (getVersion() == HttpVersion.HTTP_2) {
7878
throw new IllegalArgumentException("HTTP/1 only method called on an HTTP/2 connection.");
7979
}
80+
if (useManualDataWrites && request.getBodyStream() != null) {
81+
throw new IllegalStateException(
82+
"Cannot use manual data writes with a body stream on an HTTP/1.1 request. "
83+
+ "Either remove the body stream or set useManualDataWrites to false.");
84+
}
8085
HttpStreamBase stream = httpClientConnectionMakeRequest(getNativeHandle(),
8186
request.marshalForJni(),
8287
request.getBodyStream(),

src/test/java/software/amazon/awssdk/crt/test/WriteDataTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import software.amazon.awssdk.crt.http.*;
1313

1414
import java.net.URI;
15+
import java.nio.ByteBuffer;
1516
import java.nio.charset.StandardCharsets;
1617
import java.util.Arrays;
1718
import java.util.concurrent.CompletableFuture;
@@ -278,4 +279,71 @@ public void onResponseComplete(HttpStreamBase stream, int errorCode) {
278279
shutdownComplete.get(60, TimeUnit.SECONDS);
279280
CrtResource.waitForNoResources();
280281
}
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+
}
281349
}

0 commit comments

Comments
 (0)