8378600: Refactor tests under test/jdk/java/net/httpclient/http2 from TestNG to JUnit

Reviewed-by: vyazici
This commit is contained in:
Daniel Fuchs 2026-03-05 11:12:58 +00:00
parent fc77e3e9a2
commit a0c0a36179
14 changed files with 196 additions and 191 deletions

View File

@ -45,7 +45,7 @@
* java.net.http/jdk.internal.net.http.qpack.writers
* java.security.jgss
* @modules java.base/jdk.internal.util
* @run testng/othervm/timeout=60 -Djavax.net.debug=ssl -Djdk.httpclient.HttpClient.log=all ErrorTest
* @run junit/othervm/timeout=60 -Djavax.net.debug=ssl -Djdk.httpclient.HttpClient.log=all ErrorTest
* @summary check exception thrown when bad TLS parameters selected
*/
@ -67,7 +67,7 @@ import jdk.httpclient.test.lib.http2.Http2TestServer;
import jdk.test.lib.net.SimpleSSLContext;
import static java.net.http.HttpClient.Version.HTTP_2;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
/**
* When selecting an unacceptable cipher suite the TLS handshake will fail.

View File

@ -30,7 +30,7 @@
* jdk.test.lib.Asserts
* jdk.test.lib.Utils
* jdk.test.lib.net.SimpleSSLContext
* @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors FixedThreadPoolTest
* @run junit/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors FixedThreadPoolTest
*/
import java.net.*;
@ -51,7 +51,7 @@ import static jdk.test.lib.Asserts.assertFileContentsEqual;
import static jdk.test.lib.Utils.createTempFile;
import static jdk.test.lib.Utils.createTempFileOfSize;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
public class FixedThreadPoolTest implements HttpServerAdapters {
@ -92,7 +92,7 @@ public class FixedThreadPoolTest implements HttpServerAdapters {
}
@Test
public static void test() throws Exception {
public void test() throws Exception {
try {
initialize();
simpleTest(false);

View File

@ -38,7 +38,7 @@ import jdk.test.lib.net.SimpleSSLContext;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static java.net.http.HttpClient.Builder.NO_PROXY;
import static java.net.http.HttpClient.Version.HTTP_2;
@ -166,21 +166,21 @@ class H2SelectorVTTest implements HttpServerAdapters {
final HttpRequest req1 = reqBuilder.copy().GET().build();
System.out.println("\nIssuing request: " + req1);
final HttpResponse<?> resp1 = client.send(req1, BodyHandlers.ofString());
Assertions.assertEquals(200, resp1.statusCode(), "unexpected response code for GET request");
assertEquals(200, resp1.statusCode(), "unexpected response code for GET request");
assertSelectorThread(client);
// POST
final HttpRequest req2 = reqBuilder.copy().POST(BodyPublishers.ofString("foo")).build();
System.out.println("\nIssuing request: " + req2);
final HttpResponse<?> resp2 = client.send(req2, BodyHandlers.ofString());
Assertions.assertEquals(200, resp2.statusCode(), "unexpected response code for POST request");
assertEquals(200, resp2.statusCode(), "unexpected response code for POST request");
assertSelectorThread(client);
// HEAD
final HttpRequest req3 = reqBuilder.copy().HEAD().build();
System.out.println("\nIssuing request: " + req3);
final HttpResponse<?> resp3 = client.send(req3, BodyHandlers.ofString());
Assertions.assertEquals(200, resp3.statusCode(), "unexpected response code for HEAD request");
assertEquals(200, resp3.statusCode(), "unexpected response code for HEAD request");
assertSelectorThread(client);
}
}
@ -219,6 +219,6 @@ class H2SelectorVTTest implements HttpServerAdapters {
msg = "%s not found in %s".formatted(name, threads);
System.out.printf("%s: %s%n", status, msg);
}
Assertions.assertEquals(!isTCPSelectorThreadVirtual(), found, msg);
assertEquals(!isTCPSelectorThreadVirtual(), found, msg);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,7 +25,7 @@
* @test
* @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.http2.Http2TestServer
* @run testng/othervm
* @run junit/othervm
* -Djdk.internal.httpclient.debug=true
* -Djdk.httpclient.HttpClient.log=errors,requests,responses,trace
* ImplicitPushCancel
@ -51,15 +51,16 @@ import java.util.concurrent.ConcurrentMap;
import jdk.httpclient.test.lib.http2.Http2TestServer;
import jdk.httpclient.test.lib.http2.Http2TestExchange;
import jdk.httpclient.test.lib.http2.Http2Handler;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ImplicitPushCancel {
static Map<String,String> PUSH_PROMISES = Map.of(
static final Map<String,String> PUSH_PROMISES = Map.of(
"/x/y/z/1", "the first push promise body",
"/x/y/z/2", "the second push promise body",
"/x/y/z/3", "the third push promise body",
@ -72,11 +73,11 @@ public class ImplicitPushCancel {
);
static final String MAIN_RESPONSE_BODY = "the main response body";
Http2TestServer server;
URI uri;
private static Http2TestServer server;
private static URI uri;
@BeforeTest
public void setup() throws Exception {
@BeforeAll
public static void setup() throws Exception {
server = new Http2TestServer(false, 0);
Http2Handler handler = new ServerPushHandler(MAIN_RESPONSE_BODY,
PUSH_PROMISES);
@ -87,13 +88,13 @@ public class ImplicitPushCancel {
uri = new URI("http://localhost:" + port + "/foo/a/b/c");
}
@AfterTest
public void teardown() {
@AfterAll
public static void teardown() {
server.stop();
}
static final <T> HttpResponse<T> assert200ResponseCode(HttpResponse<T> response) {
assertEquals(response.statusCode(), 200);
assertEquals(200, response.statusCode());
return response;
}
@ -128,11 +129,11 @@ public class ImplicitPushCancel {
promises.entrySet().stream().forEach(entry -> {
HttpRequest request = entry.getKey();
HttpResponse<String> response = entry.getValue().join();
assertEquals(response.statusCode(), 200);
assertEquals(200, response.statusCode());
if (PUSH_PROMISES.containsKey(request.uri().getPath())) {
assertEquals(response.body(), PUSH_PROMISES.get(request.uri().getPath()));
assertEquals(PUSH_PROMISES.get(request.uri().getPath()), response.body());
} else {
assertEquals(response.body(), MAIN_RESPONSE_BODY);
assertEquals(MAIN_RESPONSE_BODY, response.body());
}
} );

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,7 +26,7 @@
* @bug 8087112
* @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.http2.Http2TestServer
* @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors
* @run junit/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors
* -Djdk.internal.httpclient.debug=true
* NoBodyTest
*/
@ -47,10 +47,10 @@ import jdk.httpclient.test.lib.http2.Http2TestServer;
import jdk.httpclient.test.lib.http2.Http2TestExchange;
import jdk.httpclient.test.lib.http2.Http2Handler;
import jdk.test.lib.net.SimpleSSLContext;
import org.testng.annotations.Test;
import static java.net.http.HttpClient.Version.HTTP_2;
@Test
import org.junit.jupiter.api.Test;
public class NoBodyTest {
static int httpPort, httpsPort;
static Http2TestServer httpServer, httpsServer;
@ -86,7 +86,7 @@ public class NoBodyTest {
}
@Test
public static void runtest() throws Exception {
public void runtest() throws Exception {
try {
initialize();
warmup(false);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,7 +28,7 @@
* does not process any data. The client should read all data from the server and close the connection.
* @library /test/jdk/java/net/httpclient/lib
* @build jdk.httpclient.test.lib.http2.Http2TestServer
* @run testng/othervm/timeout=50 -Djdk.httpclient.HttpClient.log=all
* @run junit/othervm/timeout=50 -Djdk.httpclient.HttpClient.log=all
* PostPutTest
*/
@ -36,11 +36,6 @@ import jdk.httpclient.test.lib.http2.Http2Handler;
import jdk.httpclient.test.lib.http2.Http2TestExchange;
import jdk.httpclient.test.lib.http2.Http2TestServer;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
@ -51,19 +46,24 @@ import java.net.http.HttpResponse;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.net.http.HttpRequest.BodyPublishers.ofByteArray;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class PostPutTest {
Http2TestServer http2TestServer;
URI warmupURI, testHandlerBasicURI, testHandlerCloseBosURI, testHandleNegativeContentLengthURI;
private static Http2TestServer http2TestServer;
private static URI warmupURI, testHandlerBasicURI, testHandlerCloseBosURI, testHandleNegativeContentLengthURI;
static PrintStream testLog = System.err;
// As per jdk.internal.net.http.WindowController.DEFAULT_INITIAL_WINDOW_SIZE
final int DEFAULT_INITIAL_WINDOW_SIZE = (64 * 1024) - 1;
private static final int DEFAULT_INITIAL_WINDOW_SIZE = (64 * 1024) - 1;
// Add on a small amount of arbitrary bytes to see if client hangs when receiving RST_STREAM
byte[] data = new byte[DEFAULT_INITIAL_WINDOW_SIZE + 10];
private static byte[] data = new byte[DEFAULT_INITIAL_WINDOW_SIZE + 10];
@BeforeTest
public void setup() throws Exception {
@BeforeAll
public static void setup() throws Exception {
http2TestServer = new Http2TestServer(false, 0);
http2TestServer.addHandler(new WarmupHandler(), "/Warmup");
http2TestServer.addHandler(new TestHandlerBasic(), "/TestHandlerBasic");
@ -81,15 +81,14 @@ public class PostPutTest {
testLog.println("PostPutTest.setup(): testHandleNegativeContentLengthURI: " + testHandleNegativeContentLengthURI);
}
@AfterTest
public void teardown() {
@AfterAll
public static void teardown() {
testLog.println("PostPutTest.teardown(): Stopping server");
http2TestServer.stop();
data = null;
}
@DataProvider(name = "variants")
public Object[][] variants() {
public static Object[][] variants() {
HttpRequest over64kPost, over64kPut, over64kPostCloseBos, over64kPutCloseBos, over64kPostNegativeContentLength, over64kPutNegativeContentLength;
over64kPost = HttpRequest.newBuilder().version(HTTP_2).POST(ofByteArray(data)).uri(testHandlerBasicURI).build();
over64kPut = HttpRequest.newBuilder().version(HTTP_2).PUT(ofByteArray(data)).uri(testHandlerBasicURI).build();
@ -117,7 +116,8 @@ public class PostPutTest {
.build();
}
@Test(dataProvider = "variants")
@ParameterizedTest
@MethodSource("variants")
public void testOver64kPUT(HttpRequest req, String testMessage) {
testLog.println("PostPutTest: Performing test: " + testMessage);
HttpClient hc = HttpClient.newBuilder().version(HTTP_2).build();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -31,10 +31,9 @@
* @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.http2.Http2TestServer
* jdk.httpclient.test.lib.http2.BodyOutputStream
* jdk.httpclient.test.lib.http2.OutgoingPushPromise
* @run testng/othervm PushPromiseContinuation
* @run junit/othervm PushPromiseContinuation
*/
import javax.net.ssl.SSLSession;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -68,14 +67,13 @@ import jdk.internal.net.http.common.HttpHeadersBuilder;
import jdk.internal.net.http.frame.ContinuationFrame;
import jdk.internal.net.http.frame.HeaderFrame;
import org.testng.TestException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class PushPromiseContinuation {
@ -85,8 +83,8 @@ public class PushPromiseContinuation {
static volatile int continuationCount;
static final String mainPromiseBody = "Main Promise Body";
static final String mainResponseBody = "Main Response Body";
Http2TestServer server;
URI uri;
private static Http2TestServer server;
private static URI uri;
// Set up simple client-side push promise handler
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>> pushPromiseMap = new ConcurrentHashMap<>();
@ -95,13 +93,13 @@ public class PushPromiseContinuation {
pushPromiseMap.put(pushRequest, acceptor.apply(s));
};
@BeforeMethod
@BeforeEach
public void beforeMethod() {
pushPromiseMap = new ConcurrentHashMap<>();
}
@BeforeTest
public void setup() throws Exception {
@BeforeAll
public static void setup() throws Exception {
server = new Http2TestServer(false, 0);
server.addHandler(new ServerPushHandler(), "/");
@ -115,9 +113,8 @@ public class PushPromiseContinuation {
uri = new URI("http://localhost:" + port + "/");
}
@AfterTest
public void teardown() {
pushPromiseMap = null;
@AfterAll
public static void teardown() {
server.stop();
}
@ -195,29 +192,29 @@ public class PushPromiseContinuation {
CompletableFuture<HttpResponse<String>> cf =
client.sendAsync(hreq, HttpResponse.BodyHandlers.ofString(UTF_8), pph);
CompletionException t = expectThrows(CompletionException.class, () -> cf.join());
assertEquals(t.getCause().getClass(), ProtocolException.class,
CompletionException t = assertThrows(CompletionException.class, () -> cf.join());
assertEquals(ProtocolException.class, t.getCause().getClass(),
"Expected a ProtocolException but got " + t.getCause());
System.err.println("Client received the following expected exception: " + t.getCause());
faultyServer.stop();
}
private void verify(HttpResponse<String> resp) {
assertEquals(resp.statusCode(), 200);
assertEquals(resp.body(), mainResponseBody);
assertEquals(200, resp.statusCode());
assertEquals(mainResponseBody, resp.body());
if (pushPromiseMap.size() > 1) {
System.err.println(pushPromiseMap.entrySet());
throw new TestException("Results map size is greater than 1");
fail("Results map size is greater than 1");
} else {
// This will only iterate once
for (HttpRequest r : pushPromiseMap.keySet()) {
HttpResponse<String> serverPushResp = pushPromiseMap.get(r).join();
// Received headers should be the same as the combined PushPromise
// frame headers combined with the Continuation frame headers
assertEquals(testHeaders, r.headers());
assertEquals(r.headers(), testHeaders);
// Check status code and push promise body are as expected
assertEquals(serverPushResp.statusCode(), 200);
assertEquals(serverPushResp.body(), mainPromiseBody);
assertEquals(200, serverPushResp.statusCode());
assertEquals(mainPromiseBody, serverPushResp.body());
}
}
}

View File

@ -30,7 +30,7 @@
* jdk.httpclient.test.lib.http2.Http2RedirectHandler
* jdk.test.lib.Asserts
* jdk.test.lib.net.SimpleSSLContext
* @run testng/othervm
* @run junit/othervm
* -Djdk.httpclient.HttpClient.log=frames,ssl,requests,responses,errors
* -Djdk.internal.httpclient.debug=true
* RedirectTest
@ -52,9 +52,10 @@ import jdk.httpclient.test.lib.common.HttpServerAdapters;
import jdk.httpclient.test.lib.http2.Http2TestServer;
import jdk.httpclient.test.lib.http2.Http2TestExchange;
import jdk.httpclient.test.lib.http2.Http2RedirectHandler;
import org.testng.annotations.Test;
import static java.net.http.HttpClient.Version.HTTP_2;
import org.junit.jupiter.api.Test;
public class RedirectTest implements HttpServerAdapters {
static int httpPort;
static HttpTestServer httpServer;
@ -135,7 +136,7 @@ public class RedirectTest implements HttpServerAdapters {
}
@Test
public static void test() throws Exception {
public void test() throws Exception {
try {
initialize();
simpleTest();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -29,7 +29,7 @@
* @build jdk.httpclient.test.lib.http2.Http2TestServer
* jdk.httpclient.test.lib.http2.PushHandler
* jdk.test.lib.Utils
* @run testng/othervm
* @run junit/othervm
* -Djdk.httpclient.HttpClient.log=errors,requests,responses
* ServerPush
*/
@ -48,13 +48,14 @@ import java.util.concurrent.*;
import java.util.function.Consumer;
import jdk.httpclient.test.lib.http2.Http2TestServer;
import jdk.httpclient.test.lib.http2.PushHandler;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static java.nio.charset.StandardCharsets.UTF_8;
import static jdk.test.lib.Utils.createTempFileOfSize;
import static org.testng.Assert.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ServerPush {
@ -66,11 +67,11 @@ public class ServerPush {
static Path tempFile;
Http2TestServer server;
URI uri;
private static Http2TestServer server;
private static URI uri;
@BeforeTest
public void setup() throws Exception {
@BeforeAll
public static void setup() throws Exception {
tempFile = createTempFileOfSize(TEMP_FILE_PREFIX, null, FILE_SIZE);
server = new Http2TestServer(false, 0);
server.addHandler(new PushHandler(tempFile, LOOPS), "/");
@ -82,8 +83,8 @@ public class ServerPush {
uri = new URI("http://localhost:" + port + "/foo/a/b/c");
}
@AfterTest
public void teardown() {
@AfterAll
public static void teardown() {
server.stop();
}
@ -109,10 +110,10 @@ public class ServerPush {
System.err.println("results.size: " + resultMap.size());
for (HttpRequest r : resultMap.keySet()) {
HttpResponse<String> response = resultMap.get(r).join();
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), tempFileAsString);
assertEquals(200, response.statusCode());
assertEquals(tempFileAsString, response.body());
}
assertEquals(resultMap.size(), LOOPS + 1);
assertEquals(LOOPS + 1, resultMap.size());
}
// Test 2 - of(...) populating the given Map, everything as a String
@ -135,10 +136,10 @@ public class ServerPush {
System.err.println("results.size: " + resultMap.size());
for (HttpRequest r : resultMap.keySet()) {
HttpResponse<String> response = resultMap.get(r).join();
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), tempFileAsString);
assertEquals(200, response.statusCode());
assertEquals(tempFileAsString, response.body());
}
assertEquals(resultMap.size(), LOOPS + 1);
assertEquals(LOOPS + 1, resultMap.size());
}
// --- Path ---
@ -177,11 +178,11 @@ public class ServerPush {
for (HttpRequest r : resultsMap.keySet()) {
HttpResponse<Path> response = resultsMap.get(r).join();
assertEquals(response.statusCode(), 200);
assertEquals(200, response.statusCode());
String fileAsString = new String(Files.readAllBytes(response.body()), UTF_8);
assertEquals(fileAsString, tempFileAsString);
assertEquals(tempFileAsString, fileAsString);
}
assertEquals(resultsMap.size(), LOOPS + 1);
assertEquals(LOOPS + 1, resultsMap.size());
}
// Test 4 - of(...) populating the given Map, everything as a Path
@ -204,11 +205,11 @@ public class ServerPush {
for (HttpRequest r : resultsMap.keySet()) {
HttpResponse<Path> response = resultsMap.get(r).join();
assertEquals(response.statusCode(), 200);
assertEquals(200, response.statusCode());
String fileAsString = new String(Files.readAllBytes(response.body()), UTF_8);
assertEquals(fileAsString, tempFileAsString);
assertEquals(tempFileAsString, fileAsString);
}
assertEquals(resultsMap.size(), LOOPS + 1);
assertEquals(LOOPS + 1, resultsMap.size());
}
// --- Consumer<byte[]> ---
@ -263,12 +264,12 @@ public class ServerPush {
for (HttpRequest r : resultsMap.keySet()) {
HttpResponse<Void> response = resultsMap.get(r).join();
assertEquals(response.statusCode(), 200);
assertEquals(200, response.statusCode());
byte[] ba = byteArrayConsumerMap.get(r).getAccumulatedBytes();
String result = new String(ba, UTF_8);
assertEquals(result, tempFileAsString);
assertEquals(tempFileAsString, result);
}
assertEquals(resultsMap.size(), LOOPS + 1);
assertEquals(LOOPS + 1, resultsMap.size());
}
// Test 6 - of(...) populating the given Map, everything as a consumer of optional byte[]
@ -301,11 +302,11 @@ public class ServerPush {
for (HttpRequest r : resultsMap.keySet()) {
HttpResponse<Void> response = resultsMap.get(r).join();
assertEquals(response.statusCode(), 200);
assertEquals(200, response.statusCode());
byte[] ba = byteArrayConsumerMap.get(r).getAccumulatedBytes();
String result = new String(ba, UTF_8);
assertEquals(result, tempFileAsString);
assertEquals(tempFileAsString, result);
}
assertEquals(resultsMap.size(), LOOPS + 1);
assertEquals(LOOPS + 1, resultsMap.size());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,7 +25,7 @@
* @test
* @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.http2.Http2TestServer
* @run testng/othervm
* @run junit/othervm
* -Djdk.internal.httpclient.debug=true
* -Djdk.httpclient.HttpClient.log=errors,requests,responses
* ServerPushWithDiffTypes
@ -47,9 +47,10 @@ import jdk.httpclient.test.lib.http2.Http2TestServer;
import jdk.httpclient.test.lib.http2.Http2TestExchange;
import jdk.httpclient.test.lib.http2.Http2Handler;
import org.testng.annotations.Test;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ServerPushWithDiffTypes {
@ -66,7 +67,7 @@ public class ServerPushWithDiffTypes {
);
@Test
public static void test() throws Exception {
public void test() throws Exception {
Http2TestServer server = null;
try {
server = new Http2TestServer(false, 0);
@ -93,7 +94,7 @@ public class ServerPushWithDiffTypes {
results.put(request, cf);
cf.join();
assertEquals(results.size(), PUSH_PROMISES.size() + 1);
assertEquals(PUSH_PROMISES.size() + 1, results.size());
for (HttpRequest r : results.keySet()) {
URI u = r.uri();
@ -116,7 +117,7 @@ public class ServerPushWithDiffTypes {
String expected = PUSH_PROMISES.get(r.uri().getPath());
if (expected == null)
expected = "the main response body";
assertEquals(result, expected);
assertEquals(expected, result);
}
} finally {
server.stop();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,11 +27,11 @@
* @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.common.TestUtil
* jdk.httpclient.test.lib.http2.Http2TestServer
* @run testng/othervm -XX:+CrashOnOutOfMemoryError SimpleGet
* @run testng/othervm -XX:+CrashOnOutOfMemoryError
* @run junit/othervm -XX:+CrashOnOutOfMemoryError SimpleGet
* @run junit/othervm -XX:+CrashOnOutOfMemoryError
* -Dsimpleget.repeat=1 -Dsimpleget.chunks=1 -Dsimpleget.requests=1000
* SimpleGet
* @run testng/othervm -Dsimpleget.requests=150
* @run junit/othervm -Dsimpleget.requests=150
* -Dsimpleget.chunks=16384
* -Djdk.httpclient.redirects.retrylimit=5
* -Djdk.httpclient.HttpClient.log=errors
@ -62,11 +62,12 @@ import javax.net.ssl.SSLContext;
import jdk.httpclient.test.lib.common.HttpServerAdapters;
import jdk.test.lib.net.SimpleSSLContext;
import org.testng.Assert;
import org.testng.annotations.Test;
import static java.net.http.HttpClient.Version.HTTP_2;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SimpleGet implements HttpServerAdapters {
static HttpTestServer httpsServer;
static HttpClient client = null;
@ -117,11 +118,11 @@ public class SimpleGet implements HttpServerAdapters {
}
public static void main(String[] args) throws Exception {
test();
new SimpleGet().test();
}
@Test
public static void test() throws Exception {
public void test() throws Exception {
try {
long prestart = System.nanoTime();
initialize();
@ -132,7 +133,7 @@ public class SimpleGet implements HttpServerAdapters {
.GET().build();
long start = System.nanoTime();
var resp = client.send(request, BodyHandlers.ofByteArrayConsumer(b -> {}));
Assert.assertEquals(resp.statusCode(), 200);
assertEquals(200, resp.statusCode());
long elapsed = System.nanoTime() - start;
System.out.println("Stat: First request took: " + elapsed + " nanos (" + TimeUnit.NANOSECONDS.toMillis(elapsed) + " ms)");
final int max = property("simpleget.requests", 50);
@ -163,7 +164,7 @@ public class SimpleGet implements HttpServerAdapters {
+ connections.size() + " connections");
}
}
list.forEach((cf) -> Assert.assertEquals(cf.join().statusCode(), 200));
list.forEach((cf) -> assertEquals(200, cf.join().statusCode()));
} catch (Throwable tt) {
System.err.println("tt caught");
tt.printStackTrace();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,7 +26,7 @@
* @bug 8342075 8343855
* @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext
* @run testng/othervm -Djdk.internal.httpclient.debug=true
* @run junit/othervm -Djdk.internal.httpclient.debug=true
* -Djdk.httpclient.connectionWindowSize=65535
* -Djdk.httpclient.windowsize=16384
* StreamFlowControlTest
@ -65,30 +65,30 @@ import jdk.internal.net.http.common.HttpHeadersBuilder;
import jdk.internal.net.http.frame.SettingsFrame;
import jdk.test.lib.Utils;
import jdk.test.lib.net.SimpleSSLContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
public class StreamFlowControlTest {
private static final SSLContext sslContext = SimpleSSLContext.findSSLContext();
HttpTestServer http2TestServer; // HTTP/2 ( h2c )
HttpTestServer https2TestServer; // HTTP/2 ( h2 )
String http2URI;
String https2URI;
final AtomicInteger reqid = new AtomicInteger();
private static HttpTestServer http2TestServer; // HTTP/2 ( h2c )
private static HttpTestServer https2TestServer; // HTTP/2 ( h2 )
private static String http2URI;
private static String https2URI;
private static final AtomicInteger reqid = new AtomicInteger();
final static int WINDOW =
Integer.getInteger("jdk.httpclient.windowsize", 2 * 16 * 1024);
@DataProvider(name = "variants")
public Object[][] variants() {
public static Object[][] variants() {
return new Object[][] {
{ http2URI, false },
{ https2URI, false },
@ -111,7 +111,8 @@ public class StreamFlowControlTest {
}
@Test(dataProvider = "variants")
@ParameterizedTest
@MethodSource("variants")
void test(String uri,
boolean sameClient)
throws Exception
@ -142,7 +143,7 @@ public class StreamFlowControlTest {
if (sameClient) {
String key = response.headers().firstValue("X-Connection-Key").get();
if (label == null) label = key;
assertEquals(key, label, "Unexpected key for " + query);
assertEquals(label, key, "Unexpected key for " + query);
}
sent.join();
// we have to pull to get the exception, but slow enough
@ -175,7 +176,8 @@ public class StreamFlowControlTest {
}
@Test(dataProvider = "variants")
@ParameterizedTest
@MethodSource("variants")
void testAsync(String uri,
boolean sameClient)
{
@ -207,7 +209,7 @@ public class StreamFlowControlTest {
if (sameClient) {
String key = response.headers().firstValue("X-Connection-Key").get();
if (label == null) label = key;
assertEquals(key, label, "Unexpected key for " + query);
assertEquals(label, key, "Unexpected key for " + query);
}
sent.join();
long wait = uri.startsWith("https://") ? 800 : 350;
@ -264,38 +266,38 @@ public class StreamFlowControlTest {
}
}
@BeforeTest
public void setup() throws Exception {
var http2TestServer = new Http2TestServer("localhost", false, 0);
http2TestServer.addHandler(new Http2TestHandler(), "/http2/");
this.http2TestServer = HttpTestServer.of(http2TestServer);
http2URI = "http://" + this.http2TestServer.serverAuthority() + "/http2/x";
@BeforeAll
public static void setup() throws Exception {
var http2TestServerImpl = new Http2TestServer("localhost", false, 0);
http2TestServerImpl.addHandler(new Http2TestHandler(), "/http2/");
http2TestServer = HttpTestServer.of(http2TestServerImpl);
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/x";
var https2TestServer = new Http2TestServer("localhost", true, sslContext);
https2TestServer.addHandler(new Http2TestHandler(), "/https2/");
this.https2TestServer = HttpTestServer.of(https2TestServer);
this.https2TestServer.addHandler(new HttpHeadOrGetHandler(), "/https2/head/");
https2URI = "https://" + this.https2TestServer.serverAuthority() + "/https2/x";
String h2Head = "https://" + this.https2TestServer.serverAuthority() + "/https2/head/z";
var https2TestServerImpl = new Http2TestServer("localhost", true, sslContext);
https2TestServerImpl.addHandler(new Http2TestHandler(), "/https2/");
https2TestServer = HttpTestServer.of(https2TestServerImpl);
https2TestServer.addHandler(new HttpHeadOrGetHandler(), "/https2/head/");
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/x";
String h2Head = "https://" + https2TestServer.serverAuthority() + "/https2/head/z";
// Override the default exchange supplier with a custom one to enable
// particular test scenarios
http2TestServer.setExchangeSupplier(FCHttp2TestExchange::new);
https2TestServer.setExchangeSupplier(FCHttp2TestExchange::new);
http2TestServerImpl.setExchangeSupplier(FCHttp2TestExchange::new);
https2TestServerImpl.setExchangeSupplier(FCHttp2TestExchange::new);
this.http2TestServer.start();
this.https2TestServer.start();
http2TestServer.start();
https2TestServer.start();
// warmup to eliminate delay due to SSL class loading and initialization.
try (var client = HttpClient.newBuilder().sslContext(sslContext).build()) {
var request = HttpRequest.newBuilder(URI.create(h2Head)).HEAD().build();
var resp = client.send(request, BodyHandlers.discarding());
assertEquals(resp.statusCode(), 200);
assertEquals(200, resp.statusCode());
}
}
@AfterTest
public void teardown() throws Exception {
@AfterAll
public static void teardown() throws Exception {
http2TestServer.stop();
https2TestServer.stop();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,7 +28,7 @@
* @bug 8296410
* @library /test/jdk/java/net/httpclient/lib
* @build jdk.httpclient.test.lib.http2.Http2TestServer
* @run testng/othervm -Djdk.httpclient.HttpClient.log=all TrailingHeadersTest
* @run junit/othervm -Djdk.httpclient.HttpClient.log=all TrailingHeadersTest
*/
import jdk.httpclient.test.lib.http2.OutgoingPushPromise;
@ -36,11 +36,6 @@ import jdk.internal.net.http.common.HttpHeadersBuilder;
import jdk.internal.net.http.frame.DataFrame;
import jdk.internal.net.http.frame.HeaderFrame;
import jdk.internal.net.http.frame.HeadersFrame;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import javax.net.ssl.SSLSession;
import java.io.ByteArrayInputStream;
@ -74,24 +69,30 @@ import jdk.httpclient.test.lib.http2.BodyOutputStream;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TrailingHeadersTest {
Http2TestServer http2TestServer;
URI trailingURI, trailng1xxURI, trailingPushPromiseURI, warmupURI;
private static Http2TestServer http2TestServer;
private static URI trailingURI, trailng1xxURI, trailingPushPromiseURI, warmupURI;
static PrintStream testLog = System.err;
// Set up simple client-side push promise handler
ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>> pushPromiseMap = new ConcurrentHashMap<>();
private static ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>> pushPromiseMap = new ConcurrentHashMap<>();
@BeforeMethod
@BeforeEach
public void beforeMethod() {
pushPromiseMap = new ConcurrentHashMap<>();
}
@BeforeTest
public void setup() throws Exception {
@BeforeAll
public static void setup() throws Exception {
Properties props = new Properties();
// For triggering trailing headers to send after Push Promise Response headers are sent
props.setProperty("sendTrailingHeadersAfterPushPromise", "1");
@ -119,12 +120,13 @@ public class TrailingHeadersTest {
warmupURI = URI.create("http://" + http2TestServer.serverAuthority() + "/WarmupHandler");
}
@AfterTest
public void teardown() {
@AfterAll
public static void teardown() {
http2TestServer.stop();
}
@Test(dataProvider = "httpRequests")
@ParameterizedTest
@MethodSource("uris")
public void testTrailingHeaders(String description, HttpRequest hRequest, HttpResponse.PushPromiseHandler<String> pph) {
testLog.println("testTrailingHeaders(): " + description);
HttpClient httpClient = HttpClient.newBuilder().build();
@ -134,7 +136,7 @@ public class TrailingHeadersTest {
testLog.println("testTrailingHeaders(): Performing request: " + hRequest);
HttpResponse<String> resp = cf.join();
assertEquals(resp.statusCode(), 200, "Status code of response should be 200");
assertEquals(200, resp.statusCode(), "Status code of response should be 200");
// Verify Push Promise was successful if necessary
if (pph != null)
@ -144,14 +146,14 @@ public class TrailingHeadersTest {
}
private void verifyPushPromise() {
assertEquals(pushPromiseMap.size(), 1, "Push Promise should not be greater than 1");
assertEquals(1, pushPromiseMap.size(), "Push Promise should not be greater than 1");
// This will only iterate once
for (HttpRequest r : pushPromiseMap.keySet()) {
CompletableFuture<HttpResponse<String>> serverPushResp = pushPromiseMap.get(r);
// Get the push promise HttpResponse result if present
HttpResponse<String> resp = serverPushResp.join();
assertEquals(resp.body(), "Sample_Push_Data", "Unexpected Push Promise response body");
assertEquals(resp.statusCode(), 200, "Status code of Push Promise response should be 200");
assertEquals("Sample_Push_Data", resp.body(), "Unexpected Push Promise response body");
assertEquals(200, resp.statusCode(), "Status code of Push Promise response should be 200");
}
}
@ -162,11 +164,10 @@ public class TrailingHeadersTest {
httpClient.sendAsync(warmupReq, BodyHandlers.discarding()).join();
}
@DataProvider(name = "httpRequests")
public Object[][] uris() {
public static Object[][] uris() {
HttpResponse.PushPromiseHandler<String> pph = (initial, pushRequest, acceptor) -> {
HttpResponse.BodyHandler<String> s = HttpResponse.BodyHandlers.ofString(UTF_8);
pushPromiseMap.put(pushRequest, acceptor.apply(s));
TrailingHeadersTest.pushPromiseMap.put(pushRequest, acceptor.apply(s));
};
HttpRequest httpGetTrailing = HttpRequest.newBuilder(trailingURI).version(HTTP_2)

View File

@ -29,9 +29,9 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class HeaderTableTest extends SimpleHeaderTableTest {
@ -76,7 +76,7 @@ public class HeaderTableTest extends SimpleHeaderTableTest {
Set<Integer> expectedIndexes = indexes.get(hName);
int actualMinimalIndex = table.indexOf(hName, "blah-blah");
Assertions.assertTrue(expectedIndexes.contains(-actualMinimalIndex));
assertTrue(expectedIndexes.contains(-actualMinimalIndex));
});
}