use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.
the class HttpRequestResponseTest method testHttpRequestUnActivated.
@Test
public void testHttpRequestUnActivated() throws Exception {
skipIfNetworkUnavailable();
URI uri = new URI("https://httpbin.org");
HttpHeader[] requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()) };
HttpRequest request = new HttpRequest("GET", "/get", requestHeaders, null);
CompletableFuture<Void> shutdownComplete = null;
try (HttpClientConnectionManager connPool = createConnectionPoolManager(uri)) {
shutdownComplete = connPool.getShutdownCompleteFuture();
try (HttpClientConnection conn = connPool.acquireConnection().get(60, TimeUnit.SECONDS)) {
HttpStreamResponseHandler streamHandler = new HttpStreamResponseHandler() {
@Override
public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
// do nothing
}
@Override
public void onResponseHeadersDone(HttpStream stream, int blockType) {
// do nothing
}
@Override
public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
// do nothing
return bodyBytesIn.length;
}
@Override
public void onResponseComplete(HttpStream stream, int errorCode) {
// do nothing.
}
};
HttpStream stream = conn.makeRequest(request, streamHandler);
stream.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
if (shutdownComplete != null) {
shutdownComplete.get();
}
CrtResource.waitForNoResources();
}
use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.
the class HttpRequestResponseTest method testRequest.
public TestHttpResponse testRequest(String method, String endpoint, String path, String requestBody, boolean useChunkedEncoding, int expectedStatus) throws Exception {
URI uri = new URI(endpoint);
HttpHeader[] requestHeaders = null;
if (!useChunkedEncoding) {
requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()), new HttpHeader("Content-Length", Integer.toString(requestBody.getBytes(UTF8).length)) };
} else {
requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()), new HttpHeader("Transfer-Encoding", "chunked") };
}
HttpRequestBodyStream bodyStream = null;
if (!useChunkedEncoding) {
final ByteBuffer bodyBytesIn = ByteBuffer.wrap(requestBody.getBytes(UTF8));
bodyStream = new HttpRequestBodyStream() {
@Override
public boolean sendRequestBody(ByteBuffer bodyBytesOut) {
transferData(bodyBytesIn, bodyBytesOut);
return bodyBytesIn.remaining() == 0;
}
@Override
public boolean resetPosition() {
bodyBytesIn.position(0);
return true;
}
};
}
HttpRequest request = new HttpRequest(method, path, requestHeaders, bodyStream);
TestHttpResponse response = null;
int numAttempts = 0;
do {
if (request.getBodyStream() != null) {
request.getBodyStream().resetPosition();
}
numAttempts++;
response = null;
try {
if (useChunkedEncoding) {
response = getResponse(uri, request, requestBody.getBytes(UTF8));
} else {
response = getResponse(uri, request, null);
}
} catch (Exception ex) {
// do nothing just let it retry
}
} while ((response == null || shouldRetry(response)) && numAttempts < 3);
Assert.assertNotEquals(-1, response.blockType);
boolean hasContentLengthHeader = false;
for (HttpHeader h : response.headers) {
if (h.getName().equals("Content-Length")) {
hasContentLengthHeader = true;
}
}
Assert.assertTrue(hasContentLengthHeader);
if (response.statusCode < 500) {
// if the server errored, not our fault
Assert.assertEquals("Expected and Actual Status Codes don't match", expectedStatus, response.statusCode);
}
return response;
}
use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.
the class HttpClientConnectionManagerTest method testParallelRequests.
public void testParallelRequests(int numThreads, int numRequests) throws Exception {
skipIfNetworkUnavailable();
URI uri = new URI(endpoint);
try (HttpClientConnectionManager connectionPool = createConnectionManager(uri, numThreads, NUM_CONNECTIONS)) {
HttpRequest request = createHttpRequest("GET", endpoint, path, EMPTY_BODY);
testParallelConnections(connectionPool, request, 1, numRequests);
}
CrtResource.logNativeResources();
CrtResource.waitForNoResources();
}
Aggregations