Rework Jetty10Http2OverTlsTests so they compile with Java 8

Closes gh-27382
This commit is contained in:
Andy Wilkinson 2021-07-19 11:06:58 +01:00
parent 46b3d1e47b
commit c9ccfcc25f
2 changed files with 38 additions and 35 deletions

View File

@ -19,19 +19,5 @@ dependencies {
runtimeOnly("org.eclipse.jetty.http2:http2-server") runtimeOnly("org.eclipse.jetty.http2:http2-server")
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
testImplementation("org.eclipse.jetty:jetty-client") testImplementation("org.apache.httpcomponents.client5:httpclient5")
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()
} }

View File

@ -16,12 +16,19 @@
package smoketest.jetty10; package smoketest.jetty10;
import org.eclipse.jetty.client.HttpClient; import javax.net.ssl.SSLContext;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http2.client.HTTP2Client; import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2; import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
import org.eclipse.jetty.io.ClientConnector; import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.eclipse.jetty.util.ssl.SslContextFactory; 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.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE; 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;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -50,19 +56,30 @@ public class Jetty10Http2OverTlsTests {
@Test @Test
void httpOverTlsGetWhenHttp2AndSslAreEnabledSucceeds() throws Exception { void httpOverTlsGetWhenHttp2AndSslAreEnabledSucceeds() throws Exception {
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy()).build();
sslContextFactory.setTrustAll(true); TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create().setSslContext(sslContext).build();
ClientConnector clientConnector = new ClientConnector(); try (CloseableHttpAsyncClient http2Client = HttpAsyncClients.customHttp2().setTlsStrategy(tlsStrategy)
clientConnector.setSslContextFactory(sslContextFactory); .build()) {
HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(new HTTP2Client(clientConnector))); http2Client.start();
client.start(); SimpleHttpRequest request = SimpleHttpRequests.get("https://localhost:" + this.port);
try { request.setBody("Hello World", ContentType.TEXT_PLAIN);
ContentResponse response = client.GET("https://localhost:" + this.port + "/"); SimpleHttpResponse response = http2Client.execute(request, new FutureCallback<SimpleHttpResponse>() {
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(response.getContentAsString()).isEqualTo("Hello World"); @Override
} public void failed(Exception ex) {
finally { }
client.stop();
@Override
public void completed(SimpleHttpResponse result) {
}
@Override
public void cancelled() {
}
}).get();
assertThat(response.getCode()).isEqualTo(200);
assertThat(response.getBodyText()).isEqualTo("Hello World");
} }
} }