Search in sources :

Example 41 with HttpRequest

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();
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) HttpClientConnection(software.amazon.awssdk.crt.http.HttpClientConnection) HttpStreamResponseHandler(software.amazon.awssdk.crt.http.HttpStreamResponseHandler) HttpClientConnectionManager(software.amazon.awssdk.crt.http.HttpClientConnectionManager) URI(java.net.URI) HttpStream(software.amazon.awssdk.crt.http.HttpStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 42 with HttpRequest

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;
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 43 with HttpRequest

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();
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpClientConnectionManager(software.amazon.awssdk.crt.http.HttpClientConnectionManager) URI(java.net.URI)

Aggregations

HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)43 HttpHeader (software.amazon.awssdk.crt.http.HttpHeader)25 Test (org.junit.Test)21 URI (java.net.URI)13 ByteBuffer (java.nio.ByteBuffer)13 HttpRequestBodyStream (software.amazon.awssdk.crt.http.HttpRequestBodyStream)13 AwsSigningConfig (software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)10 StaticCredentialsProvider (software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 List (java.util.List)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AwsSigningResult (software.amazon.awssdk.crt.auth.signing.AwsSigningResult)6 ArrayList (java.util.ArrayList)5 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)5 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 StandardCharsets (java.nio.charset.StandardCharsets)3 Arrays (java.util.Arrays)3 Optional (java.util.Optional)3 HttpClientConnection (software.amazon.awssdk.crt.http.HttpClientConnection)3