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")
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")
}

View File

@ -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<SimpleHttpResponse>() {
@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");
}
}