From c9ccfcc25fa620c7277cda6a7d0f0a083b70ed78 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 19 Jul 2021 11:06:58 +0100 Subject: [PATCH] Rework Jetty10Http2OverTlsTests so they compile with Java 8 Closes gh-27382 --- .../build.gradle | 16 +----- .../jetty10/Jetty10Http2OverTlsTests.java | 57 ++++++++++++------- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jetty10/build.gradle b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jetty10/build.gradle index 2591ecd3fc..d0fef13649 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jetty10/build.gradle +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jetty10/build.gradle @@ -19,19 +19,5 @@ dependencies { runtimeOnly("org.eclipse.jetty.http2:http2-server") testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) - testImplementation("org.eclipse.jetty:jetty-client") - testImplementation("org.eclipse.jetty.http2:http2-client") - testImplementation("org.eclipse.jetty.http2:http2-http-client-transport") -} - -def buildingWithJava11OrLater() { - return project.hasProperty("toolchainVersion") || JavaVersion.current().java11Compatible -} - -compileTestJava { - enabled = buildingWithJava11OrLater() -} - -test { - enabled = buildingWithJava11OrLater() + testImplementation("org.apache.httpcomponents.client5:httpclient5") } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jetty10/src/test/java/smoketest/jetty10/Jetty10Http2OverTlsTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jetty10/src/test/java/smoketest/jetty10/Jetty10Http2OverTlsTests.java index 40114a9a5f..91ebdd460c 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jetty10/src/test/java/smoketest/jetty10/Jetty10Http2OverTlsTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jetty10/src/test/java/smoketest/jetty10/Jetty10Http2OverTlsTests.java @@ -16,12 +16,19 @@ package smoketest.jetty10; -import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.client.api.ContentResponse; -import org.eclipse.jetty.http2.client.HTTP2Client; -import org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2; -import org.eclipse.jetty.io.ClientConnector; -import org.eclipse.jetty.util.ssl.SslContextFactory; +import javax.net.ssl.SSLContext; + +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequests; +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; +import org.apache.hc.client5.http.impl.async.HttpAsyncClients; +import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder; +import org.apache.hc.client5.http.ssl.TrustAllStrategy; +import org.apache.hc.core5.concurrent.FutureCallback; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.nio.ssl.TlsStrategy; +import org.apache.hc.core5.ssl.SSLContexts; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.JRE; @@ -29,7 +36,6 @@ import org.junit.jupiter.api.condition.JRE; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.web.server.LocalServerPort; -import org.springframework.http.HttpStatus; import static org.assertj.core.api.Assertions.assertThat; @@ -50,19 +56,30 @@ public class Jetty10Http2OverTlsTests { @Test void httpOverTlsGetWhenHttp2AndSslAreEnabledSucceeds() throws Exception { - SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); - sslContextFactory.setTrustAll(true); - ClientConnector clientConnector = new ClientConnector(); - clientConnector.setSslContextFactory(sslContextFactory); - HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(new HTTP2Client(clientConnector))); - client.start(); - try { - ContentResponse response = client.GET("https://localhost:" + this.port + "/"); - assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); - assertThat(response.getContentAsString()).isEqualTo("Hello World"); - } - finally { - client.stop(); + SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy()).build(); + TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create().setSslContext(sslContext).build(); + try (CloseableHttpAsyncClient http2Client = HttpAsyncClients.customHttp2().setTlsStrategy(tlsStrategy) + .build()) { + http2Client.start(); + SimpleHttpRequest request = SimpleHttpRequests.get("https://localhost:" + this.port); + request.setBody("Hello World", ContentType.TEXT_PLAIN); + SimpleHttpResponse response = http2Client.execute(request, new FutureCallback() { + + @Override + public void failed(Exception ex) { + } + + @Override + public void completed(SimpleHttpResponse result) { + } + + @Override + public void cancelled() { + } + + }).get(); + assertThat(response.getCode()).isEqualTo(200); + assertThat(response.getBodyText()).isEqualTo("Hello World"); } }