Skip to content

Commit

Permalink
8303457: Introduce convenience test library APIs for creating test se…
Browse files Browse the repository at this point in the history
…rvers for tests in test/jdk/java/net/httpclient

Reviewed-by: lucy
Backport-of: 72de24e59a80a38ea4ea6a8a3f966f555987ac86
  • Loading branch information
GoeLin committed Apr 4, 2024
1 parent 7a47adf commit e913fa7
Show file tree
Hide file tree
Showing 41 changed files with 348 additions and 326 deletions.
13 changes: 6 additions & 7 deletions test/jdk/java/net/httpclient/AbstractThrowingPublishers.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@

import static java.lang.String.format;
import static java.lang.System.out;
import static java.net.http.HttpClient.Version.HTTP_1_1;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -692,16 +694,13 @@ public void setup() throws Exception {
// HTTP/1.1
HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();
HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler();
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
httpTestServer = HttpTestServer.create(HTTP_1_1);
httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed");
httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");
httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x";
httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x";

HttpsServer httpsServer = HttpsServer.create(sa, 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
httpsTestServer = HttpTestServer.of(httpsServer);
httpsTestServer = HttpTestServer.create(HTTP_1_1, sslContext);
httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");
httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x";
Expand All @@ -711,13 +710,13 @@ public void setup() throws Exception {
HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();

http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
http2TestServer = HttpTestServer.create(HTTP_2);
http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";

https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
https2TestServer = HttpTestServer.create(HTTP_2, sslContext);
https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import static java.lang.System.out;
import static java.lang.System.err;
import static java.lang.String.format;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -705,13 +706,13 @@ public void setup() throws Exception {
HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();

http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
http2TestServer = HttpTestServer.create(HTTP_2);
http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";

https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
https2TestServer = HttpTestServer.create(HTTP_2, sslContext);
https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
Expand Down
13 changes: 6 additions & 7 deletions test/jdk/java/net/httpclient/AbstractThrowingSubscribers.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@

import static java.lang.System.out;
import static java.lang.String.format;
import static java.net.http.HttpClient.Version.HTTP_1_1;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -683,16 +685,13 @@ public void setup() throws Exception {
// HTTP/1.1
HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();
HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler();
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
httpTestServer = HttpTestServer.create(HTTP_1_1);
httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed");
httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");
httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x";
httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x";

HttpsServer httpsServer = HttpsServer.create(sa, 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
httpsTestServer = HttpTestServer.of(httpsServer);
httpsTestServer = HttpTestServer.create(HTTP_1_1, sslContext);
httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");
httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x";
Expand All @@ -702,13 +701,13 @@ public void setup() throws Exception {
HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();

http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
http2TestServer = HttpTestServer.create(HTTP_2);
http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";

https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
https2TestServer = HttpTestServer.create(HTTP_2, sslContext);
https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
Expand Down
15 changes: 6 additions & 9 deletions test/jdk/java/net/httpclient/AggregateRequestBodyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
import org.testng.annotations.Test;

import static java.lang.System.out;
import static java.net.http.HttpClient.Version.HTTP_1_1;
import static java.net.http.HttpClient.Version.HTTP_2;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -817,25 +819,20 @@ public void setup() throws Exception {
throw new AssertionError("Unexpected null sslContext");

HttpTestHandler handler = new HttpTestEchoHandler();
InetSocketAddress loopback = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);

HttpServer http1 = HttpServer.create(loopback, 0);
http1TestServer = HttpTestServer.of(http1);
http1TestServer = HttpTestServer.create(HTTP_1_1);
http1TestServer.addHandler(handler, "/http1/echo/");
http1URI = "http://" + http1TestServer.serverAuthority() + "/http1/echo/x";

HttpsServer https1 = HttpsServer.create(loopback, 0);
https1.setHttpsConfigurator(new HttpsConfigurator(sslContext));
https1TestServer = HttpTestServer.of(https1);
https1TestServer = HttpTestServer.create(HTTP_1_1, sslContext);
https1TestServer.addHandler(handler, "/https1/echo/");
https1URI = "https://" + https1TestServer.serverAuthority() + "/https1/echo/x";

// HTTP/2
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
http2TestServer = HttpTestServer.create(HTTP_2);
http2TestServer.addHandler(handler, "/http2/echo/");
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo/x";

https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
https2TestServer = HttpTestServer.create(HTTP_2, sslContext);
https2TestServer.addHandler(handler, "/https2/echo/");
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo/x";

Expand Down
15 changes: 7 additions & 8 deletions test/jdk/java/net/httpclient/AuthFilterCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@

import javax.net.ssl.SSLContext;

import static java.net.http.HttpClient.Version.HTTP_1_1;
import static java.net.http.HttpClient.Version.HTTP_2;

/**
* @test
* @bug 8232853
Expand Down Expand Up @@ -87,7 +90,7 @@ public class AuthFilterCacheTest implements HttpServerAdapters {
ProxySelector proxySelector;
MyAuthenticator auth;
HttpClient client;
Executor executor = Executors.newCachedThreadPool();
ExecutorService executor = Executors.newCachedThreadPool();

@DataProvider(name = "uris")
Object[][] testURIs() {
Expand Down Expand Up @@ -119,9 +122,7 @@ public void setUp() throws Exception {
auth = new MyAuthenticator();

// HTTP/1.1
HttpServer server1 = HttpServer.create(sa, 0);
server1.setExecutor(executor);
http1Server = HttpTestServer.of(server1);
http1Server = HttpTestServer.create(HTTP_1_1, null, executor);
http1Server.addHandler(new TestHandler(), "/AuthFilterCacheTest/http1/");
http1Server.start();
http1URI = new URI("http://" + http1Server.serverAuthority()
Expand All @@ -138,16 +139,14 @@ public void setUp() throws Exception {
+ "/AuthFilterCacheTest/https1/");

// HTTP/2.0
http2Server = HttpTestServer.of(
new Http2TestServer("localhost", false, 0));
http2Server = HttpTestServer.create(HTTP_2);
http2Server.addHandler(new TestHandler(), "/AuthFilterCacheTest/http2/");
http2Server.start();
http2URI = new URI("http://" + http2Server.serverAuthority()
+ "/AuthFilterCacheTest/http2/");

// HTTPS/2.0
https2Server = HttpTestServer.of(
new Http2TestServer("localhost", true, 0));
https2Server = HttpTestServer.create(HTTP_2, SSLContext.getDefault());
https2Server.addHandler(new TestHandler(), "/AuthFilterCacheTest/https2/");
https2Server.start();
https2URI = new URI("https://" + https2Server.serverAuthority()
Expand Down
14 changes: 6 additions & 8 deletions test/jdk/java/net/httpclient/BasicRedirectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static java.lang.System.out;
import static java.net.http.HttpClient.Version.HTTP_1_1;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
Expand Down Expand Up @@ -209,21 +211,17 @@ public void setup() throws Exception {
if (sslContext == null)
throw new AssertionError("Unexpected null sslContext");

InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);

httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
httpTestServer = HttpTestServer.create(HTTP_1_1);
httpTestServer.addHandler(new BasicHttpRedirectHandler(), "/http1/same/");
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/same/redirect";
HttpsServer httpsServer = HttpsServer.create(sa, 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
httpsTestServer = HttpTestServer.of(httpsServer);
httpsTestServer = HttpTestServer.create(HTTP_1_1, sslContext);
httpsTestServer.addHandler(new BasicHttpRedirectHandler(),"/https1/same/");
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/same/redirect";

http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
http2TestServer = HttpTestServer.create(HTTP_2);
http2TestServer.addHandler(new BasicHttpRedirectHandler(), "/http2/same/");
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/same/redirect";
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
https2TestServer = HttpTestServer.create(HTTP_2, sslContext);
https2TestServer.addHandler(new BasicHttpRedirectHandler(), "/https2/same/");
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/same/redirect";

Expand Down
13 changes: 6 additions & 7 deletions test/jdk/java/net/httpclient/CancelRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@

import static java.lang.System.arraycopy;
import static java.lang.System.out;
import static java.net.http.HttpClient.Version.HTTP_1_1;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -547,25 +549,22 @@ public void setup() throws Exception {

// HTTP/1.1
HttpTestHandler h1_chunkHandler = new HTTPSlowHandler();
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
httpTestServer = HttpTestServer.create(HTTP_1_1);
httpTestServer.addHandler(h1_chunkHandler, "/http1/x/");
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/x/";

HttpsServer httpsServer = HttpsServer.create(sa, 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
httpsTestServer = HttpTestServer.of(httpsServer);
httpsTestServer = HttpTestServer.create(HTTP_1_1, sslContext);
httpsTestServer.addHandler(h1_chunkHandler, "/https1/x/");
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/x/";

// HTTP/2
HttpTestHandler h2_chunkedHandler = new HTTPSlowHandler();

http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
http2TestServer = HttpTestServer.create(HTTP_2);
http2TestServer.addHandler(h2_chunkedHandler, "/http2/x/");
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/x/";

https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
https2TestServer = HttpTestServer.create(HTTP_2, sslContext);
https2TestServer.addHandler(h2_chunkedHandler, "/https2/x/");
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/x/";

Expand Down
27 changes: 13 additions & 14 deletions test/jdk/java/net/httpclient/CookieHeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
import jdk.httpclient.test.lib.http2.Http2TestServer;

import static java.lang.System.out;
import static java.net.http.HttpClient.Version.HTTP_1_1;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -113,10 +115,10 @@ public static String now() {
@DataProvider(name = "positive")
public Object[][] positive() {
return new Object[][] {
{ httpURI, HttpClient.Version.HTTP_1_1 },
{ httpsURI, HttpClient.Version.HTTP_1_1 },
{ httpDummy, HttpClient.Version.HTTP_1_1 },
{ httpsDummy, HttpClient.Version.HTTP_1_1 },
{ httpURI, HTTP_1_1 },
{ httpsURI, HTTP_1_1 },
{ httpDummy, HTTP_1_1 },
{ httpsDummy, HTTP_1_1 },
{ httpURI, HttpClient.Version.HTTP_2 },
{ httpsURI, HttpClient.Version.HTTP_2 },
{ httpDummy, HttpClient.Version.HTTP_2 },
Expand Down Expand Up @@ -187,26 +189,23 @@ public void setup() throws Exception {
if (sslContext == null)
throw new AssertionError("Unexpected null sslContext");

InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);

httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
httpTestServer = HttpTestServer.create(HTTP_1_1);
httpTestServer.addHandler(new CookieValidationHandler(), "/http1/cookie/");
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/cookie/retry";
HttpsServer httpsServer = HttpsServer.create(sa, 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
httpsTestServer = HttpTestServer.of(httpsServer);
httpsTestServer = HttpTestServer.create(HTTP_1_1, sslContext);
httpsTestServer.addHandler(new CookieValidationHandler(),"/https1/cookie/");
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/cookie/retry";

http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
http2TestServer = HttpTestServer.create(HTTP_2);
http2TestServer.addHandler(new CookieValidationHandler(), "/http2/cookie/");
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/cookie/retry";
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
https2TestServer = HttpTestServer.create(HTTP_2, sslContext);
https2TestServer.addHandler(new CookieValidationHandler(), "/https2/cookie/");
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/cookie/retry";


// DummyServer
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
httpDummyServer = DummyServer.create(sa);
httpsDummyServer = DummyServer.create(sa, sslContext);
httpDummy = "http://" + httpDummyServer.serverAuthority() + "/http1/dummy/x";
Expand Down Expand Up @@ -274,7 +273,7 @@ public void handle(HttpTestExchange t) throws IOException {
String uuid = uuids.get(0);
// retrying
if (closedRequests.putIfAbsent(uuid, t.getRequestURI().toString()) == null) {
if (t.getExchangeVersion() == HttpClient.Version.HTTP_1_1) {
if (t.getExchangeVersion() == HTTP_1_1) {
// Throwing an exception here only causes a retry
// with HTTP_1_1 - where it forces the server to close
// the connection.
Expand Down Expand Up @@ -302,7 +301,7 @@ public void handle(HttpTestExchange t) throws IOException {
try (OutputStream os = t.getResponseBody()) {
List<String> cookie = t.getRequestHeaders().get("Cookie");
if (cookie != null) {
if (version == HttpClient.Version.HTTP_1_1 || upgraded) {
if (version == HTTP_1_1 || upgraded) {
if (cookie.size() == 1) {
cookie = List.of(cookie.get(0).split("; "));
} else if (cookie.size() > 1) {
Expand Down
Loading

1 comment on commit e913fa7

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.