|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | +package software.amazon.awssdk.crt.test; |
| 6 | + |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import software.amazon.awssdk.crt.CRT; |
| 11 | +import software.amazon.awssdk.crt.CrtResource; |
| 12 | +import software.amazon.awssdk.crt.http.*; |
| 13 | + |
| 14 | +import java.net.URI; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +public class WriteDataTest extends HttpRequestResponseFixture { |
| 21 | + private final static String HOST = "https://localhost:3443"; |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testHttp2WriteData() throws Exception { |
| 25 | + skipIfAndroid(); |
| 26 | + skipIfLocalhostUnavailable(); |
| 27 | + |
| 28 | + URI uri = new URI(HOST); |
| 29 | + byte[] payload = "hello from writeData".getBytes(StandardCharsets.UTF_8); |
| 30 | + |
| 31 | + HttpHeader[] headers = new HttpHeader[]{ |
| 32 | + new HttpHeader(":method", "PUT"), |
| 33 | + new HttpHeader(":path", "/echo"), |
| 34 | + new HttpHeader(":scheme", uri.getScheme()), |
| 35 | + new HttpHeader(":authority", uri.getHost()), |
| 36 | + new HttpHeader("content-length", Integer.toString(payload.length)), |
| 37 | + }; |
| 38 | + Http2Request request = new Http2Request(headers, null); |
| 39 | + |
| 40 | + CompletableFuture<Void> reqCompleted = new CompletableFuture<>(); |
| 41 | + TestHttpResponse response = new TestHttpResponse(); |
| 42 | + |
| 43 | + CompletableFuture<Void> shutdownComplete; |
| 44 | + try (HttpClientConnectionManager connPool = createConnectionPoolManager(uri, HttpVersion.HTTP_2)) { |
| 45 | + shutdownComplete = connPool.getShutdownCompleteFuture(); |
| 46 | + try (Http2ClientConnection conn = (Http2ClientConnection) connPool.acquireConnection() |
| 47 | + .get(60, TimeUnit.SECONDS)) { |
| 48 | + |
| 49 | + HttpStreamBaseResponseHandler streamHandler = new HttpStreamBaseResponseHandler() { |
| 50 | + @Override |
| 51 | + public void onResponseHeaders(HttpStreamBase stream, int responseStatusCode, int blockType, |
| 52 | + HttpHeader[] nextHeaders) { |
| 53 | + response.statusCode = responseStatusCode; |
| 54 | + response.headers.addAll(Arrays.asList(nextHeaders)); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public int onResponseBody(HttpStreamBase stream, byte[] bodyBytesIn) { |
| 59 | + response.bodyBuffer.put(bodyBytesIn); |
| 60 | + return bodyBytesIn.length; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onResponseComplete(HttpStreamBase stream, int errorCode) { |
| 65 | + response.onCompleteErrorCode = errorCode; |
| 66 | + reqCompleted.complete(null); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + try (Http2Stream stream = conn.makeRequest(request, streamHandler, true)) { |
| 71 | + stream.activate(); |
| 72 | + stream.writeData(payload, true).get(5, TimeUnit.SECONDS); |
| 73 | + reqCompleted.get(60, TimeUnit.SECONDS); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Assert.assertEquals(CRT.AWS_CRT_SUCCESS, response.onCompleteErrorCode); |
| 79 | + Assert.assertEquals(200, response.statusCode); |
| 80 | + String body = response.getBody(); |
| 81 | + Assert.assertEquals("hello from writeData", body); |
| 82 | + |
| 83 | + shutdownComplete.get(60, TimeUnit.SECONDS); |
| 84 | + CrtResource.waitForNoResources(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testHttp2WriteDataEndStreamOnly() throws Exception { |
| 89 | + skipIfAndroid(); |
| 90 | + skipIfLocalhostUnavailable(); |
| 91 | + |
| 92 | + URI uri = new URI(HOST); |
| 93 | + |
| 94 | + HttpHeader[] headers = new HttpHeader[]{ |
| 95 | + new HttpHeader(":method", "GET"), |
| 96 | + new HttpHeader(":path", "/echo"), |
| 97 | + new HttpHeader(":scheme", uri.getScheme()), |
| 98 | + new HttpHeader(":authority", uri.getHost()), |
| 99 | + }; |
| 100 | + Http2Request request = new Http2Request(headers, null); |
| 101 | + |
| 102 | + CompletableFuture<Void> reqCompleted = new CompletableFuture<>(); |
| 103 | + TestHttpResponse response = new TestHttpResponse(); |
| 104 | + |
| 105 | + CompletableFuture<Void> shutdownComplete; |
| 106 | + try (HttpClientConnectionManager connPool = createConnectionPoolManager(uri, HttpVersion.HTTP_2)) { |
| 107 | + shutdownComplete = connPool.getShutdownCompleteFuture(); |
| 108 | + try (Http2ClientConnection conn = (Http2ClientConnection) connPool.acquireConnection() |
| 109 | + .get(60, TimeUnit.SECONDS)) { |
| 110 | + |
| 111 | + HttpStreamBaseResponseHandler streamHandler = new HttpStreamBaseResponseHandler() { |
| 112 | + @Override |
| 113 | + public void onResponseHeaders(HttpStreamBase stream, int responseStatusCode, int blockType, |
| 114 | + HttpHeader[] nextHeaders) { |
| 115 | + response.statusCode = responseStatusCode; |
| 116 | + response.headers.addAll(Arrays.asList(nextHeaders)); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public int onResponseBody(HttpStreamBase stream, byte[] bodyBytesIn) { |
| 121 | + response.bodyBuffer.put(bodyBytesIn); |
| 122 | + return bodyBytesIn.length; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void onResponseComplete(HttpStreamBase stream, int errorCode) { |
| 127 | + response.onCompleteErrorCode = errorCode; |
| 128 | + reqCompleted.complete(null); |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + // Use manual writes but send null data with endStream=true (zero-byte body) |
| 133 | + try (Http2Stream stream = conn.makeRequest(request, streamHandler, true)) { |
| 134 | + stream.activate(); |
| 135 | + stream.writeData(null, true).get(5, TimeUnit.SECONDS); |
| 136 | + reqCompleted.get(60, TimeUnit.SECONDS); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + Assert.assertEquals(CRT.AWS_CRT_SUCCESS, response.onCompleteErrorCode); |
| 142 | + Assert.assertEquals(200, response.statusCode); |
| 143 | + |
| 144 | + shutdownComplete.get(60, TimeUnit.SECONDS); |
| 145 | + CrtResource.waitForNoResources(); |
| 146 | + } |
| 147 | +} |
0 commit comments