8307626: java/net/httpclient/FlowAdapter* tests should close the HttpClient instances

Reviewed-by: aefimov, jpai
This commit is contained in:
Daniel Fuchs 2023-05-11 16:31:04 +00:00
parent 2bf7ac58b7
commit 9ad38cbeaf
2 changed files with 398 additions and 369 deletions

View File

@ -24,9 +24,9 @@
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient.Builder;
import java.net.http.HttpClient.Version;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer; import java.nio.MappedByteBuffer;
import java.util.Arrays; import java.util.Arrays;
@ -35,18 +35,11 @@ import java.util.concurrent.Flow.Publisher;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import jdk.httpclient.test.lib.common.HttpServerAdapters; 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.Http2Handler;
import jdk.test.lib.net.SimpleSSLContext; import jdk.test.lib.net.SimpleSSLContext;
import org.testng.annotations.AfterTest; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
@ -66,18 +59,18 @@ import static org.testng.Assert.fail;
* @test * @test
* @summary Basic tests for Flow adapter Publishers * @summary Basic tests for Flow adapter Publishers
* @library /test/lib /test/jdk/java/net/httpclient/lib * @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.httpclient.test.lib.http2.Http2TestServer * @build jdk.httpclient.test.lib.common.HttpServerAdapters
* jdk.test.lib.net.SimpleSSLContext * jdk.test.lib.net.SimpleSSLContext
* @run testng/othervm FlowAdapterPublisherTest * @run testng/othervm FlowAdapterPublisherTest
*/ */
public class FlowAdapterPublisherTest { public class FlowAdapterPublisherTest implements HttpServerAdapters {
SSLContext sslContext; SSLContext sslContext;
HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ] HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ]
HttpsServer httpsTestServer; // HTTPS/1.1 HttpTestServer httpsTestServer; // HTTPS/1.1
Http2TestServer http2TestServer; // HTTP/2 ( h2c ) HttpTestServer http2TestServer; // HTTP/2 ( h2c )
Http2TestServer https2TestServer; // HTTP/2 ( h2 ) HttpTestServer https2TestServer; // HTTP/2 ( h2 )
String httpURI; String httpURI;
String httpsURI; String httpsURI;
String http2URI; String http2URI;
@ -96,6 +89,23 @@ public class FlowAdapterPublisherTest {
static final Class<NullPointerException> NPE = NullPointerException.class; static final Class<NullPointerException> NPE = NullPointerException.class;
static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
private static Version version(String uri) {
if (uri.contains("/http1/")) return Version.HTTP_1_1;
if (uri.contains("/https1/")) return Version.HTTP_1_1;
if (uri.contains("/http2/")) return Version.HTTP_2;
if (uri.contains("/https2/")) return Version.HTTP_2;
return null;
}
private HttpClient newHttpClient(String uri) {
var builder = HttpClient.newBuilder();
return builder.sslContext(sslContext).proxy(Builder.NO_PROXY).build();
}
private HttpRequest.Builder newRequestBuilder(String uri) {
return HttpRequest.newBuilder(URI.create(uri));
}
@Test @Test
public void testAPIExceptions() { public void testAPIExceptions() {
assertThrows(NPE, () -> fromPublisher(null)); assertThrows(NPE, () -> fromPublisher(null));
@ -111,67 +121,75 @@ public class FlowAdapterPublisherTest {
// Flow.Publisher<ByteBuffer> // Flow.Publisher<ByteBuffer>
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testByteBufferPublisherUnknownLength(String url) { void testByteBufferPublisherUnknownLength(String uri) {
String[] body = new String[] { "You know ", "it's summer ", "in Ireland ", String[] body = new String[] { "You know ", "it's summer ", "in Ireland ",
"when the ", "rain gets ", "warmer." }; "when the ", "rain gets ", "warmer." };
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(fromPublisher(new BBPublisher(body))).build(); .POST(fromPublisher(new BBPublisher(body))).build();
HttpResponse<String> response = client.sendAsync(request, ofString(UTF_8)).join(); HttpResponse<String> response = client.sendAsync(request, ofString(UTF_8)).join();
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, Arrays.stream(body).collect(joining())); assertEquals(response.version(), version(uri));
assertEquals(text, Arrays.stream(body).collect(joining()));
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testByteBufferPublisherFixedLength(String url) { void testByteBufferPublisherFixedLength(String uri) {
String[] body = new String[] { "You know ", "it's summer ", "in Ireland ", String[] body = new String[] { "You know ", "it's summer ", "in Ireland ",
"when the ", "rain gets ", "warmer." }; "when the ", "rain gets ", "warmer." };
int cl = Arrays.stream(body).mapToInt(String::length).sum(); int cl = Arrays.stream(body).mapToInt(String::length).sum();
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(fromPublisher(new BBPublisher(body), cl)).build(); .POST(fromPublisher(new BBPublisher(body), cl)).build();
HttpResponse<String> response = client.sendAsync(request, ofString(UTF_8)).join(); HttpResponse<String> response = client.sendAsync(request, ofString(UTF_8)).join();
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, Arrays.stream(body).collect(joining())); assertEquals(response.version(), version(uri));
assertEquals(text, Arrays.stream(body).collect(joining()));
}
} }
// Flow.Publisher<MappedByteBuffer> // Flow.Publisher<MappedByteBuffer>
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testMappedByteBufferPublisherUnknownLength(String url) { void testMappedByteBufferPublisherUnknownLength(String uri) {
String[] body = new String[] { "God invented ", "whiskey to ", "keep the ", String[] body = new String[] { "God invented ", "whiskey to ", "keep the ",
"Irish from ", "ruling the ", "world." }; "Irish from ", "ruling the ", "world." };
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(fromPublisher(new MBBPublisher(body))).build(); .POST(fromPublisher(new MBBPublisher(body))).build();
HttpResponse<String> response = client.sendAsync(request, ofString(UTF_8)).join(); HttpResponse<String> response = client.sendAsync(request, ofString(UTF_8)).join();
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, Arrays.stream(body).collect(joining())); assertEquals(response.version(), version(uri));
assertEquals(text, Arrays.stream(body).collect(joining()));
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testMappedByteBufferPublisherFixedLength(String url) { void testMappedByteBufferPublisherFixedLength(String uri) {
String[] body = new String[] { "God invented ", "whiskey to ", "keep the ", String[] body = new String[] { "God invented ", "whiskey to ", "keep the ",
"Irish from ", "ruling the ", "world." }; "Irish from ", "ruling the ", "world." };
int cl = Arrays.stream(body).mapToInt(String::length).sum(); int cl = Arrays.stream(body).mapToInt(String::length).sum();
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(fromPublisher(new MBBPublisher(body), cl)).build(); .POST(fromPublisher(new MBBPublisher(body), cl)).build();
HttpResponse<String> response = client.sendAsync(request, ofString(UTF_8)).join(); HttpResponse<String> response = client.sendAsync(request, ofString(UTF_8)).join();
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, Arrays.stream(body).collect(joining())); assertEquals(response.version(), version(uri));
assertEquals(text, Arrays.stream(body).collect(joining()));
}
} }
// The following two tests depend on Exception detail messages, which is // The following two tests depend on Exception detail messages, which is
@ -179,36 +197,38 @@ public class FlowAdapterPublisherTest {
// updated if the exception message is updated. // updated if the exception message is updated.
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testPublishTooFew(String url) throws InterruptedException { void testPublishTooFew(String uri) throws InterruptedException {
String[] body = new String[] { "You know ", "it's summer ", "in Ireland ", String[] body = new String[] { "You know ", "it's summer ", "in Ireland ",
"when the ", "rain gets ", "warmer." }; "when the ", "rain gets ", "warmer." };
int cl = Arrays.stream(body).mapToInt(String::length).sum() + 1; // length + 1 int cl = Arrays.stream(body).mapToInt(String::length).sum() + 1; // length + 1
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(fromPublisher(new BBPublisher(body), cl)).build(); .POST(fromPublisher(new BBPublisher(body), cl)).build();
try { try {
HttpResponse<String> response = client.send(request, ofString(UTF_8)); HttpResponse<String> response = client.send(request, ofString(UTF_8));
fail("Unexpected response: " + response); fail("Unexpected response: " + response);
} catch (IOException expected) { } catch (IOException expected) {
assertMessage(expected, "Too few bytes returned"); assertMessage(expected, "Too few bytes returned");
}
} }
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testPublishTooMany(String url) throws InterruptedException { void testPublishTooMany(String uri) throws InterruptedException {
String[] body = new String[] { "You know ", "it's summer ", "in Ireland ", String[] body = new String[] { "You know ", "it's summer ", "in Ireland ",
"when the ", "rain gets ", "warmer." }; "when the ", "rain gets ", "warmer." };
int cl = Arrays.stream(body).mapToInt(String::length).sum() - 1; // length - 1 int cl = Arrays.stream(body).mapToInt(String::length).sum() - 1; // length - 1
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(fromPublisher(new BBPublisher(body), cl)).build(); .POST(fromPublisher(new BBPublisher(body), cl)).build();
try { try {
HttpResponse<String> response = client.send(request, ofString(UTF_8)); HttpResponse<String> response = client.send(request, ofString(UTF_8));
fail("Unexpected response: " + response); fail("Unexpected response: " + response);
} catch (IOException expected) { } catch (IOException expected) {
assertMessage(expected, "Too many bytes in request body"); assertMessage(expected, "Too many bytes in request body");
}
} }
} }
@ -332,33 +352,26 @@ public class FlowAdapterPublisherTest {
} }
} }
static String serverAuthority(HttpServer server) {
return InetAddress.getLoopbackAddress().getHostName() + ":"
+ server.getAddress().getPort();
}
@BeforeTest @BeforeTest
public void setup() throws Exception { public void setup() throws Exception {
sslContext = new SimpleSSLContext().get(); sslContext = new SimpleSSLContext().get();
if (sslContext == null) if (sslContext == null)
throw new AssertionError("Unexpected null sslContext"); throw new AssertionError("Unexpected null sslContext");
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(),0); httpTestServer = HttpTestServer.create(Version.HTTP_1_1);
httpTestServer = HttpServer.create(sa, 0); httpTestServer.addHandler(new HttpEchoHandler(), "/http1/echo");
httpTestServer.createContext("/http1/echo", new Http1EchoHandler()); httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/echo";
httpURI = "http://" + serverAuthority(httpTestServer) + "/http1/echo";
httpsTestServer = HttpsServer.create(sa, 0); httpsTestServer = HttpTestServer.create(Version.HTTP_1_1, sslContext);
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); httpsTestServer.addHandler(new HttpEchoHandler(), "/https1/echo");
httpsTestServer.createContext("/https1/echo", new Http1EchoHandler()); httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/echo";
httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1/echo";
http2TestServer = new Http2TestServer("localhost", false, 0); http2TestServer = HttpTestServer.create(Version.HTTP_2);
http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo"); http2TestServer.addHandler(new HttpEchoHandler(), "/http2/echo");
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo"; http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";
https2TestServer = new Http2TestServer("localhost", true, sslContext); https2TestServer = HttpTestServer.create(Version.HTTP_2, sslContext);
https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo"); https2TestServer.addHandler(new HttpEchoHandler(), "/https2/echo");
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo"; https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";
httpTestServer.start(); httpTestServer.start();
@ -369,27 +382,15 @@ public class FlowAdapterPublisherTest {
@AfterTest @AfterTest
public void teardown() throws Exception { public void teardown() throws Exception {
httpTestServer.stop(0); httpTestServer.stop();
httpsTestServer.stop(0); httpsTestServer.stop();
http2TestServer.stop(); http2TestServer.stop();
https2TestServer.stop(); https2TestServer.stop();
} }
static class Http1EchoHandler implements HttpHandler { static class HttpEchoHandler implements HttpTestHandler {
@Override @Override
public void handle(HttpExchange t) throws IOException { public void handle(HttpTestExchange t) throws IOException {
try (InputStream is = t.getRequestBody();
OutputStream os = t.getResponseBody()) {
byte[] bytes = is.readAllBytes();
t.sendResponseHeaders(200, bytes.length);
os.write(bytes);
}
}
}
static class Http2EchoHandler implements Http2Handler {
@Override
public void handle(Http2TestExchange t) throws IOException {
try (InputStream is = t.getRequestBody(); try (InputStream is = t.getRequestBody();
OutputStream os = t.getResponseBody()) { OutputStream os = t.getResponseBody()) {
byte[] bytes = is.readAllBytes(); byte[] bytes = is.readAllBytes();

View File

@ -26,32 +26,24 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient.Builder;
import java.net.http.HttpClient.Version;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Flow; import java.util.concurrent.Flow;
import java.util.concurrent.Flow.Subscriber; import java.util.concurrent.Flow.Subscriber;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers; import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers; import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.HttpResponse.BodySubscribers; import java.net.http.HttpResponse.BodySubscribers;
import jdk.httpclient.test.lib.common.HttpServerAdapters; 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.Http2Handler;
import jdk.test.lib.net.SimpleSSLContext; import jdk.test.lib.net.SimpleSSLContext;
import org.testng.annotations.AfterTest; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
@ -67,21 +59,23 @@ import static org.testng.Assert.assertTrue;
* @test * @test
* @summary Basic tests for Flow adapter Subscribers * @summary Basic tests for Flow adapter Subscribers
* @library /test/lib /test/jdk/java/net/httpclient/lib * @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext * @build jdk.httpclient.test.lib.common.HttpServerAdapters
* jdk.test.lib.net.SimpleSSLContext
* @run testng/othervm -Djdk.internal.httpclient.debug=true FlowAdapterSubscriberTest * @run testng/othervm -Djdk.internal.httpclient.debug=true FlowAdapterSubscriberTest
*/ */
public class FlowAdapterSubscriberTest { public class FlowAdapterSubscriberTest implements HttpServerAdapters {
SSLContext sslContext; SSLContext sslContext;
HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ] HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ]
HttpsServer httpsTestServer; // HTTPS/1.1 HttpTestServer httpsTestServer; // HTTPS/1.1
Http2TestServer http2TestServer; // HTTP/2 ( h2c ) HttpTestServer http2TestServer; // HTTP/2 ( h2c )
Http2TestServer https2TestServer; // HTTP/2 ( h2 ) HttpTestServer https2TestServer; // HTTP/2 ( h2 )
String httpURI; String httpURI;
String httpsURI; String httpsURI;
String http2URI; String http2URI;
String https2URI; String https2URI;
static final long start = System.nanoTime(); static final long start = System.nanoTime();
public static String now() { public static String now() {
long now = System.nanoTime() - start; long now = System.nanoTime() - start;
@ -103,6 +97,23 @@ public class FlowAdapterSubscriberTest {
static final Class<NullPointerException> NPE = NullPointerException.class; static final Class<NullPointerException> NPE = NullPointerException.class;
private static Version version(String uri) {
if (uri.contains("/http1/")) return Version.HTTP_1_1;
if (uri.contains("/https1/")) return Version.HTTP_1_1;
if (uri.contains("/http2/")) return Version.HTTP_2;
if (uri.contains("/https2/")) return Version.HTTP_2;
return null;
}
private HttpClient newHttpClient(String uri) {
var builder = HttpClient.newBuilder();
return builder.sslContext(sslContext).proxy(Builder.NO_PROXY).build();
}
private HttpRequest.Builder newRequestBuilder(String uri) {
return HttpRequest.newBuilder(URI.create(uri));
}
@Test @Test
public void testNull() { public void testNull() {
System.out.printf(now() + "testNull() starting%n"); System.out.printf(now() + "testNull() starting%n");
@ -125,300 +136,335 @@ public class FlowAdapterSubscriberTest {
// List<ByteBuffer> // List<ByteBuffer>
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testListWithFinisher(String url) { void testListWithFinisher(String uri) {
System.out.printf(now() + "testListWithFinisher(%s) starting%n", url); System.out.printf(now() + "testListWithFinisher(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build(); .POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build();
ListSubscriber subscriber = new ListSubscriber(); ListSubscriber subscriber = new ListSubscriber();
HttpResponse<String> response = client.sendAsync(request, HttpResponse<String> response = client.sendAsync(request,
BodyHandlers.fromSubscriber(subscriber, Supplier::get)).join(); BodyHandlers.fromSubscriber(subscriber, Supplier::get)).join();
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "May the luck of the Irish be with you!"); assertEquals(response.version(), version(uri));
assertEquals(text, "May the luck of the Irish be with you!");
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testListWithoutFinisher(String url) { void testListWithoutFinisher(String uri) {
System.out.printf(now() + "testListWithoutFinisher(%s) starting%n", url); System.out.printf(now() + "testListWithoutFinisher(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build(); .POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build();
ListSubscriber subscriber = new ListSubscriber(); ListSubscriber subscriber = new ListSubscriber();
HttpResponse<Void> response = client.sendAsync(request, HttpResponse<Void> response = client.sendAsync(request,
BodyHandlers.fromSubscriber(subscriber)).join(); BodyHandlers.fromSubscriber(subscriber)).join();
String text = subscriber.get(); String text = subscriber.get();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "May the luck of the Irish be with you!"); assertEquals(response.version(), version(uri));
assertEquals(text, "May the luck of the Irish be with you!");
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testListWithFinisherBlocking(String url) throws Exception { void testListWithFinisherBlocking(String uri) throws Exception {
System.out.printf(now() + "testListWithFinisherBlocking(%s) starting%n", url); System.out.printf(now() + "testListWithFinisherBlocking(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build(); .POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build();
ListSubscriber subscriber = new ListSubscriber(); ListSubscriber subscriber = new ListSubscriber();
HttpResponse<String> response = client.send(request, HttpResponse<String> response = client.send(request,
BodyHandlers.fromSubscriber(subscriber, Supplier::get)); BodyHandlers.fromSubscriber(subscriber, Supplier::get));
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "May the luck of the Irish be with you!"); assertEquals(response.version(), version(uri));
assertEquals(text, "May the luck of the Irish be with you!");
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testListWithoutFinisherBlocking(String url) throws Exception { void testListWithoutFinisherBlocking(String uri) throws Exception {
System.out.printf(now() + "testListWithoutFinisherBlocking(%s) starting%n", url); System.out.printf(now() + "testListWithoutFinisherBlocking(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build(); .POST(BodyPublishers.ofString("May the luck of the Irish be with you!")).build();
ListSubscriber subscriber = new ListSubscriber(); ListSubscriber subscriber = new ListSubscriber();
HttpResponse<Void> response = client.send(request, HttpResponse<Void> response = client.send(request,
BodyHandlers.fromSubscriber(subscriber)); BodyHandlers.fromSubscriber(subscriber));
String text = subscriber.get(); String text = subscriber.get();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "May the luck of the Irish be with you!"); assertEquals(response.version(), version(uri));
assertEquals(text, "May the luck of the Irish be with you!");
}
} }
// Collection<ByteBuffer> // Collection<ByteBuffer>
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testCollectionWithFinisher(String url) { void testCollectionWithFinisher(String uri) {
System.out.printf(now() + "testCollectionWithFinisher(%s) starting%n", url); System.out.printf(now() + "testCollectionWithFinisher(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("What's the craic?")).build(); .POST(BodyPublishers.ofString("What's the craic?")).build();
CollectionSubscriber subscriber = new CollectionSubscriber(); CollectionSubscriber subscriber = new CollectionSubscriber();
HttpResponse<String> response = client.sendAsync(request, HttpResponse<String> response = client.sendAsync(request,
BodyHandlers.fromSubscriber(subscriber, CollectionSubscriber::get)).join(); BodyHandlers.fromSubscriber(subscriber, CollectionSubscriber::get)).join();
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "What's the craic?"); assertEquals(response.version(), version(uri));
assertEquals(text, "What's the craic?");
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testCollectionWithoutFinisher(String url) { void testCollectionWithoutFinisher(String uri) {
System.out.printf(now() + "testCollectionWithoutFinisher(%s) starting%n", url); System.out.printf(now() + "testCollectionWithoutFinisher(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("What's the craic?")).build(); .POST(BodyPublishers.ofString("What's the craic?")).build();
CollectionSubscriber subscriber = new CollectionSubscriber(); CollectionSubscriber subscriber = new CollectionSubscriber();
HttpResponse<Void> response = client.sendAsync(request, HttpResponse<Void> response = client.sendAsync(request,
BodyHandlers.fromSubscriber(subscriber)).join(); BodyHandlers.fromSubscriber(subscriber)).join();
String text = subscriber.get(); String text = subscriber.get();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "What's the craic?"); assertEquals(response.version(), version(uri));
assertEquals(text, "What's the craic?");
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testCollectionWithFinisherBlocking(String url) throws Exception { void testCollectionWithFinisherBlocking(String uri) throws Exception {
System.out.printf(now() + "testCollectionWithFinisherBlocking(%s) starting%n", url); System.out.printf(now() + "testCollectionWithFinisherBlocking(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("What's the craic?")).build(); .POST(BodyPublishers.ofString("What's the craic?")).build();
CollectionSubscriber subscriber = new CollectionSubscriber(); CollectionSubscriber subscriber = new CollectionSubscriber();
HttpResponse<String> response = client.send(request, HttpResponse<String> response = client.send(request,
BodyHandlers.fromSubscriber(subscriber, CollectionSubscriber::get)); BodyHandlers.fromSubscriber(subscriber, CollectionSubscriber::get));
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "What's the craic?"); assertEquals(response.version(), version(uri));
assertEquals(text, "What's the craic?");
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testCollectionWithoutFinisheBlocking(String url) throws Exception { void testCollectionWithoutFinisheBlocking(String uri) throws Exception {
System.out.printf(now() + "testCollectionWithoutFinisheBlocking(%s) starting%n", url); System.out.printf(now() + "testCollectionWithoutFinisheBlocking(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("What's the craic?")).build(); .POST(BodyPublishers.ofString("What's the craic?")).build();
CollectionSubscriber subscriber = new CollectionSubscriber(); CollectionSubscriber subscriber = new CollectionSubscriber();
HttpResponse<Void> response = client.send(request, HttpResponse<Void> response = client.send(request,
BodyHandlers.fromSubscriber(subscriber)); BodyHandlers.fromSubscriber(subscriber));
String text = subscriber.get(); String text = subscriber.get();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "What's the craic?"); assertEquals(response.version(), version(uri));
assertEquals(text, "What's the craic?");
}
} }
// Iterable<ByteBuffer> // Iterable<ByteBuffer>
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testIterableWithFinisher(String url) { void testIterableWithFinisher(String uri) {
System.out.printf(now() + "testIterableWithFinisher(%s) starting%n", url); System.out.printf(now() + "testIterableWithFinisher(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build(); .POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
IterableSubscriber subscriber = new IterableSubscriber(); IterableSubscriber subscriber = new IterableSubscriber();
HttpResponse<String> response = client.sendAsync(request, HttpResponse<String> response = client.sendAsync(request,
BodyHandlers.fromSubscriber(subscriber, Supplier::get)).join(); BodyHandlers.fromSubscriber(subscriber, Supplier::get)).join();
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "We're sucking diesel now!"); assertEquals(response.version(), version(uri));
assertEquals(text, "We're sucking diesel now!");
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testIterableWithoutFinisher(String url) { void testIterableWithoutFinisher(String uri) {
System.out.printf(now() + "testIterableWithoutFinisher(%s) starting%n", url); System.out.printf(now() + "testIterableWithoutFinisher(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build(); .POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
IterableSubscriber subscriber = new IterableSubscriber(); IterableSubscriber subscriber = new IterableSubscriber();
HttpResponse<Void> response = client.sendAsync(request, HttpResponse<Void> response = client.sendAsync(request,
BodyHandlers.fromSubscriber(subscriber)).join(); BodyHandlers.fromSubscriber(subscriber)).join();
String text = subscriber.get(); String text = subscriber.get();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "We're sucking diesel now!"); assertEquals(response.version(), version(uri));
assertEquals(text, "We're sucking diesel now!");
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testIterableWithFinisherBlocking(String url) throws Exception { void testIterableWithFinisherBlocking(String uri) throws Exception {
System.out.printf(now() + "testIterableWithFinisherBlocking(%s) starting%n", url); System.out.printf(now() + "testIterableWithFinisherBlocking(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build(); .POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
IterableSubscriber subscriber = new IterableSubscriber(); IterableSubscriber subscriber = new IterableSubscriber();
HttpResponse<String> response = client.send(request, HttpResponse<String> response = client.send(request,
BodyHandlers.fromSubscriber(subscriber, Supplier::get)); BodyHandlers.fromSubscriber(subscriber, Supplier::get));
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "We're sucking diesel now!"); assertEquals(response.version(), version(uri));
assertEquals(text, "We're sucking diesel now!");
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testIterableWithoutFinisherBlocking(String url) throws Exception { void testIterableWithoutFinisherBlocking(String uri) throws Exception {
System.out.printf(now() + "testIterableWithoutFinisherBlocking(%s) starting%n", url); System.out.printf(now() + "testIterableWithoutFinisherBlocking(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build(); .POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
IterableSubscriber subscriber = new IterableSubscriber(); IterableSubscriber subscriber = new IterableSubscriber();
HttpResponse<Void> response = client.send(request, HttpResponse<Void> response = client.send(request,
BodyHandlers.fromSubscriber(subscriber)); BodyHandlers.fromSubscriber(subscriber));
String text = subscriber.get(); String text = subscriber.get();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(text, "We're sucking diesel now!"); assertEquals(response.version(), version(uri));
assertEquals(text, "We're sucking diesel now!");
}
} }
// Subscriber<Object> // Subscriber<Object>
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testObjectWithFinisher(String url) { void testObjectWithFinisher(String uri) {
System.out.printf(now() + "testObjectWithFinisher(%s) starting%n", url); System.out.printf(now() + "testObjectWithFinisher(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build(); .POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
ObjectSubscriber subscriber = new ObjectSubscriber(); ObjectSubscriber subscriber = new ObjectSubscriber();
HttpResponse<String> response = client.sendAsync(request, HttpResponse<String> response = client.sendAsync(request,
BodyHandlers.fromSubscriber(subscriber, ObjectSubscriber::get)).join(); BodyHandlers.fromSubscriber(subscriber, ObjectSubscriber::get)).join();
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted! assertEquals(response.version(), version(uri));
assertTrue(text.length() != 0); // what else can be asserted!
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testObjectWithoutFinisher(String url) { void testObjectWithoutFinisher(String uri) {
System.out.printf(now() + "testObjectWithoutFinisher(%s) starting%n", url); System.out.printf(now() + "testObjectWithoutFinisher(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build(); .POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
ObjectSubscriber subscriber = new ObjectSubscriber(); ObjectSubscriber subscriber = new ObjectSubscriber();
HttpResponse<Void> response = client.sendAsync(request, HttpResponse<Void> response = client.sendAsync(request,
BodyHandlers.fromSubscriber(subscriber)).join(); BodyHandlers.fromSubscriber(subscriber)).join();
String text = subscriber.get(); String text = subscriber.get();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted! assertEquals(response.version(), version(uri));
assertTrue(text.length() != 0); // what else can be asserted!
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testObjectWithFinisherBlocking(String url) throws Exception { void testObjectWithFinisherBlocking(String uri) throws Exception {
System.out.printf(now() + "testObjectWithFinisherBlocking(%s) starting%n", url); System.out.printf(now() + "testObjectWithFinisherBlocking(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build(); .POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
ObjectSubscriber subscriber = new ObjectSubscriber(); ObjectSubscriber subscriber = new ObjectSubscriber();
HttpResponse<String> response = client.send(request, HttpResponse<String> response = client.send(request,
BodyHandlers.fromSubscriber(subscriber, ObjectSubscriber::get)); BodyHandlers.fromSubscriber(subscriber, ObjectSubscriber::get));
String text = response.body(); String text = response.body();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted! assertEquals(response.version(), version(uri));
assertTrue(text.length() != 0); // what else can be asserted!
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void testObjectWithoutFinisherBlocking(String url) throws Exception { void testObjectWithoutFinisherBlocking(String uri) throws Exception {
System.out.printf(now() + "testObjectWithoutFinisherBlocking(%s) starting%n", url); System.out.printf(now() + "testObjectWithoutFinisherBlocking(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build(); .POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
ObjectSubscriber subscriber = new ObjectSubscriber(); ObjectSubscriber subscriber = new ObjectSubscriber();
HttpResponse<Void> response = client.send(request, HttpResponse<Void> response = client.send(request,
BodyHandlers.fromSubscriber(subscriber)); BodyHandlers.fromSubscriber(subscriber));
String text = subscriber.get(); String text = subscriber.get();
System.out.println(text); System.out.println(text);
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertTrue(text.length() != 0); // what else can be asserted! assertEquals(response.version(), version(uri));
assertTrue(text.length() != 0); // what else can be asserted!
}
} }
// -- mapping using convenience handlers // -- mapping using convenience handlers
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void mappingFromByteArray(String url) throws Exception { void mappingFromByteArray(String uri) throws Exception {
System.out.printf(now() + "mappingFromByteArray(%s) starting%n", url); System.out.printf(now() + "mappingFromByteArray(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("We're sucking diesel now!")).build(); .POST(BodyPublishers.ofString("We're sucking diesel now!")).build();
client.sendAsync(request, BodyHandlers.fromSubscriber(BodySubscribers.ofByteArray(), client.sendAsync(request, BodyHandlers.fromSubscriber(BodySubscribers.ofByteArray(),
bas -> new String(bas.getBody().toCompletableFuture().join(), UTF_8))) bas -> new String(bas.getBody().toCompletableFuture().join(), UTF_8)))
.thenApply(FlowAdapterSubscriberTest::assert200ResponseCode) .thenApply(FlowAdapterSubscriberTest::assert200ResponseCode)
.thenApply(HttpResponse::body) .thenApply(HttpResponse::body)
.thenAccept(body -> assertEquals(body, "We're sucking diesel now!")) .thenAccept(body -> assertEquals(body, "We're sucking diesel now!"))
.join(); .join();
}
} }
@Test(dataProvider = "uris") @Test(dataProvider = "uris")
void mappingFromInputStream(String url) throws Exception { void mappingFromInputStream(String uri) throws Exception {
System.out.printf(now() + "mappingFromInputStream(%s) starting%n", url); System.out.printf(now() + "mappingFromInputStream(%s) starting%n", uri);
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build(); try (HttpClient client = newHttpClient(uri)) {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = newRequestBuilder(uri)
.POST(BodyPublishers.ofString("May the wind always be at your back.")).build(); .POST(BodyPublishers.ofString("May the wind always be at your back.")).build();
client.sendAsync(request, BodyHandlers.fromSubscriber(BodySubscribers.ofInputStream(), client.sendAsync(request, BodyHandlers.fromSubscriber(BodySubscribers.ofInputStream(),
ins -> { ins -> {
InputStream is = ins.getBody().toCompletableFuture().join(); InputStream is = ins.getBody().toCompletableFuture().join();
return new String(uncheckedReadAllBytes(is), UTF_8); } )) return new String(uncheckedReadAllBytes(is), UTF_8);
.thenApply(FlowAdapterSubscriberTest::assert200ResponseCode) }))
.thenApply(HttpResponse::body) .thenApply(FlowAdapterSubscriberTest::assert200ResponseCode)
.thenAccept(body -> assertEquals(body, "May the wind always be at your back.")) .thenApply(HttpResponse::body)
.join(); .thenAccept(body -> assertEquals(body, "May the wind always be at your back."))
.join();
}
} }
/** An abstract Subscriber that converts all received data into a String. */ /** An abstract Subscriber that converts all received data into a String. */
@ -503,36 +549,30 @@ public class FlowAdapterSubscriberTest {
static final <T> HttpResponse<T> assert200ResponseCode(HttpResponse<T> response) { static final <T> HttpResponse<T> assert200ResponseCode(HttpResponse<T> response) {
assertEquals(response.statusCode(), 200); assertEquals(response.statusCode(), 200);
assertEquals(response.version(), version(response.request().uri().toString()));
return response; return response;
} }
static String serverAuthority(HttpServer server) {
return InetAddress.getLoopbackAddress().getHostName() + ":"
+ server.getAddress().getPort();
}
@BeforeTest @BeforeTest
public void setup() throws Exception { public void setup() throws Exception {
sslContext = new SimpleSSLContext().get(); sslContext = new SimpleSSLContext().get();
if (sslContext == null) if (sslContext == null)
throw new AssertionError("Unexpected null sslContext"); throw new AssertionError("Unexpected null sslContext");
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); httpTestServer = HttpTestServer.create(Version.HTTP_1_1);
httpTestServer = HttpServer.create(sa, 0); httpTestServer.addHandler(new HttpEchoHandler(), "/http1/echo");
httpTestServer.createContext("/http1/echo", new Http1EchoHandler()); httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/echo";
httpURI = "http://" + serverAuthority(httpTestServer) + "/http1/echo";
httpsTestServer = HttpsServer.create(sa, 0); httpsTestServer = HttpTestServer.create(Version.HTTP_1_1, sslContext);
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); httpsTestServer.addHandler(new HttpEchoHandler(), "/https1/echo");
httpsTestServer.createContext("/https1/echo", new Http1EchoHandler()); httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/echo";
httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1/echo";
http2TestServer = new Http2TestServer("localhost", false, 0); http2TestServer = HttpTestServer.create(Version.HTTP_2);
http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo"); http2TestServer.addHandler(new HttpEchoHandler(), "/http2/echo");
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo"; http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";
https2TestServer = new Http2TestServer("localhost", true, sslContext); https2TestServer = HttpTestServer.create(Version.HTTP_2, sslContext);
https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo"); https2TestServer.addHandler(new HttpEchoHandler(), "/https2/echo");
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo"; https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";
httpTestServer.start(); httpTestServer.start();
@ -543,27 +583,15 @@ public class FlowAdapterSubscriberTest {
@AfterTest @AfterTest
public void teardown() throws Exception { public void teardown() throws Exception {
httpTestServer.stop(0); httpTestServer.stop();
httpsTestServer.stop(0); httpsTestServer.stop();
http2TestServer.stop(); http2TestServer.stop();
https2TestServer.stop(); https2TestServer.stop();
} }
static class Http1EchoHandler implements HttpHandler { static class HttpEchoHandler implements HttpTestHandler {
@Override @Override
public void handle(HttpExchange t) throws IOException { public void handle(HttpTestExchange t) throws IOException {
try (InputStream is = t.getRequestBody();
OutputStream os = t.getResponseBody()) {
byte[] bytes = is.readAllBytes();
t.sendResponseHeaders(200, bytes.length);
os.write(bytes);
}
}
}
static class Http2EchoHandler implements Http2Handler {
@Override
public void handle(Http2TestExchange t) throws IOException {
try (InputStream is = t.getRequestBody(); try (InputStream is = t.getRequestBody();
OutputStream os = t.getResponseBody()) { OutputStream os = t.getResponseBody()) {
byte[] bytes = is.readAllBytes(); byte[] bytes = is.readAllBytes();