1717
1818import org .junit .Assert ;
1919import org .junit .Assume ;
20+ import org .junit .Before ;
2021import org .junit .Test ;
2122import software .amazon .awssdk .crt .CRT ;
2223import software .amazon .awssdk .crt .CrtResource ;
4344import software .amazon .awssdk .crt .Log ;
4445
4546public class Http2ClientLocalHostTest extends HttpClientTestFixture {
46-
47+ // crt/aws-c-http/tests/mock_server includes a readme on how the server can be run locally for testing.
4748 private static final int LOCAL_HTTPS_PORT = 3443 ;
4849 private static final int LOCAL_HTTP_PORT = 3280 ;
4950
@@ -182,7 +183,7 @@ public void onResponseComplete(HttpStreamBase stream, int errorCode) {
182183 public void testParallelRequestsStressWithBody () throws Exception {
183184 skipIfAndroid ();
184185 skipIfLocalhostUnavailable ();
185- URI uri = new URI (String .format ("https://localhost:%d/uploadTest " , LOCAL_HTTPS_PORT ));
186+ URI uri = new URI (String .format ("https://localhost:%d/echo " , LOCAL_HTTPS_PORT ));
186187 try (Http2StreamManager streamManager = createStreamManager (uri , 100 )) {
187188 int numberToAcquire = 500 * 100 ;
188189 if (CRT .getOSIdentifier () == "linux" ) {
@@ -201,6 +202,7 @@ public void testParallelRequestsStressWithBody() throws Exception {
201202 final AtomicInteger numStreamsFailures = new AtomicInteger (0 );
202203 for (int i = 0 ; i < numberToAcquire ; i ++) {
203204 Http2Request request = createHttp2Request ("PUT" , uri , bodyLength );
205+ request .addHeader (new HttpHeader ("x-upload-test" , "true" ));
204206
205207 final CompletableFuture <Void > requestCompleteFuture = new CompletableFuture <Void >();
206208 final int expectedLength = bodyLength ;
@@ -217,12 +219,11 @@ public void onResponseHeaders(HttpStreamBase stream, int responseStatusCode, int
217219 @ Override
218220 public int onResponseBody (HttpStreamBase stream , byte [] bodyBytesIn ){
219221 String bodyString = new String (bodyBytesIn );
220- int receivedLength = Integer .parseInt (bodyString );
221-
222- Assert .assertTrue (receivedLength == expectedLength );
223- if (receivedLength !=expectedLength ) {
224- numStreamsFailures .incrementAndGet ();
225- }
222+ // Parse {"bytes": 123456} manually
223+ int start = bodyString .indexOf ("\" bytes\" :" ) + 8 ;
224+ int end = bodyString .indexOf ("}" , start );
225+ String bytesStr = bodyString .substring (start , end ).trim ();
226+ long receivedLength = Long .parseLong (bytesStr );
226227 return bodyString .length ();
227228 }
228229
@@ -252,10 +253,9 @@ public void onResponseComplete(HttpStreamBase stream, int errorCode) {
252253 @ Test
253254 public void testRequestsUploadStress () throws Exception {
254255 skipIfAndroid ();
255- /* Test that upload a 2.5GB data from local server (0.25GB for linux) */
256256 skipIfLocalhostUnavailable ();
257-
258- URI uri = new URI (String .format ("https://localhost:%d/uploadTest " , LOCAL_HTTPS_PORT ));
257+ /* Test that upload a 2.5GB data from local server (0.25GB for linux) */
258+ URI uri = new URI (String .format ("https://localhost:%d/echo " , LOCAL_HTTPS_PORT ));
259259 try (Http2StreamManager streamManager = createStreamManager (uri , 100 )) {
260260 long bodyLength = 2500000000L ;
261261 if (CRT .getOSIdentifier () == "linux" ) {
@@ -269,21 +269,26 @@ public void testRequestsUploadStress() throws Exception {
269269 }
270270
271271 Http2Request request = createHttp2Request ("PUT" , uri , bodyLength );
272+ request .addHeader (new HttpHeader ("x-upload-test" , "true" ));
272273
273274 final CompletableFuture <Void > requestCompleteFuture = new CompletableFuture <Void >();
274275 final long expectedLength = bodyLength ;
275276 CompletableFuture <Http2Stream > acquireCompleteFuture = streamManager .acquireStream (request , new HttpStreamBaseResponseHandler () {
276277 @ Override
277278 public void onResponseHeaders (HttpStreamBase stream , int responseStatusCode , int blockType ,
278- HttpHeader [] nextHeaders ) {
279+ HttpHeader [] nextHeaders ) {
279280
280281 Assert .assertTrue (responseStatusCode == 200 );
281282 }
282283
283284 @ Override
284285 public int onResponseBody (HttpStreamBase stream , byte [] bodyBytesIn ){
285286 String bodyString = new String (bodyBytesIn );
286- long receivedLength = Long .parseLong (bodyString );
287+ // Parse {"bytes": 123456} manually
288+ int start = bodyString .indexOf ("\" bytes\" :" ) + 8 ;
289+ int end = bodyString .indexOf ("}" , start );
290+ String bytesStr = bodyString .substring (start , end ).trim ();
291+ long receivedLength = Long .parseLong (bytesStr );
287292 Assert .assertTrue (receivedLength == expectedLength );
288293 return bodyString .length ();
289294 }
@@ -307,20 +312,21 @@ public void onResponseComplete(HttpStreamBase stream, int errorCode) {
307312 @ Test
308313 public void testRequestsDownloadStress () throws Exception {
309314 skipIfAndroid ();
310- /* Test that download a 2.5GB data from local server */
311315 skipIfLocalhostUnavailable ();
312- URI uri = new URI (String .format ("https://localhost:%d/downloadTest" , LOCAL_HTTPS_PORT ));
316+ /* Test that download a 2.5GB data from local server */
317+ URI uri = new URI (String .format ("https://localhost:%d/echo" , LOCAL_HTTPS_PORT ));
313318 try (Http2StreamManager streamManager = createStreamManager (uri , 100 )) {
314319 long bodyLength = 2500000000L ;
315320
316321 Http2Request request = createHttp2Request ("GET" , uri , 0 );
322+ request .addHeader (new HttpHeader ("x-repeat-data" , String .valueOf (bodyLength )));
317323
318324 final CompletableFuture <Void > requestCompleteFuture = new CompletableFuture <Void >();
319325 final AtomicLong receivedLength = new AtomicLong (0 );
320326 CompletableFuture <Http2Stream > acquireCompleteFuture = streamManager .acquireStream (request , new HttpStreamBaseResponseHandler () {
321327 @ Override
322328 public void onResponseHeaders (HttpStreamBase stream , int responseStatusCode , int blockType ,
323- HttpHeader [] nextHeaders ) {
329+ HttpHeader [] nextHeaders ) {
324330
325331 Assert .assertTrue (responseStatusCode == 200 );
326332 }
0 commit comments