|
| 1 | +package io.quarkus.vertx.http.http2; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | +import java.util.concurrent.ExecutionException; |
| 7 | + |
| 8 | +import jakarta.enterprise.context.ApplicationScoped; |
| 9 | +import jakarta.enterprise.event.Observes; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.Assertions; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 14 | + |
| 15 | +import io.quarkus.test.QuarkusUnitTest; |
| 16 | +import io.quarkus.test.common.http.TestHTTPResource; |
| 17 | +import io.quarkus.vertx.core.runtime.VertxCoreRecorder; |
| 18 | +import io.smallrye.certs.Format; |
| 19 | +import io.smallrye.certs.junit5.Certificate; |
| 20 | +import io.smallrye.certs.junit5.Certificates; |
| 21 | +import io.vertx.core.buffer.Buffer; |
| 22 | +import io.vertx.core.http.HttpVersion; |
| 23 | +import io.vertx.core.net.JksOptions; |
| 24 | +import io.vertx.ext.web.Router; |
| 25 | +import io.vertx.ext.web.client.HttpResponse; |
| 26 | +import io.vertx.ext.web.client.WebClient; |
| 27 | +import io.vertx.ext.web.client.WebClientOptions; |
| 28 | + |
| 29 | +@Certificates(baseDir = "target/certs", certificates = @Certificate(name = "ssl-test", password = "secret", formats = { |
| 30 | + Format.JKS, Format.PKCS12, Format.PEM })) |
| 31 | +class Http2DisabledTest { |
| 32 | + |
| 33 | + protected static final String PING_DATA = "12345678"; |
| 34 | + |
| 35 | + @TestHTTPResource(value = "/ping", tls = true) |
| 36 | + URL sslUrl; |
| 37 | + |
| 38 | + @TestHTTPResource(value = "/ping") |
| 39 | + URL plainUrl; |
| 40 | + |
| 41 | + @RegisterExtension |
| 42 | + static final QuarkusUnitTest config = new QuarkusUnitTest() |
| 43 | + .withApplicationRoot(jar -> jar |
| 44 | + .addClasses(MyBean.class) |
| 45 | + .addAsResource(new File("target/certs/ssl-test-keystore.jks"), "server-keystore.jks")) |
| 46 | + .overrideConfigKey("quarkus.http.ssl.certificate.key-store-file", "server-keystore.jks") |
| 47 | + .overrideConfigKey("quarkus.http.ssl.certificate.key-store-password", "secret") |
| 48 | + .overrideConfigKey("quarkus.http.http2", "false"); |
| 49 | + |
| 50 | + @Test |
| 51 | + void testHttp2EnabledSsl() throws ExecutionException, InterruptedException { |
| 52 | + WebClientOptions options = new WebClientOptions() |
| 53 | + .setUseAlpn(true) |
| 54 | + .setProtocolVersion(HttpVersion.HTTP_2) |
| 55 | + .setSsl(true) |
| 56 | + .setTrustOptions(new JksOptions().setPath("target/certs/ssl-test-truststore.jks").setPassword("secret")); |
| 57 | + WebClient client = WebClient.create(VertxCoreRecorder.getVertx().get(), options); |
| 58 | + int port = sslUrl.getPort(); |
| 59 | + |
| 60 | + runTest(client, port); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testHttp2EnabledPlain() throws ExecutionException, InterruptedException { |
| 65 | + WebClientOptions options = new WebClientOptions() |
| 66 | + .setProtocolVersion(HttpVersion.HTTP_2) |
| 67 | + .setHttp2ClearTextUpgrade(true); |
| 68 | + WebClient client = WebClient.create(VertxCoreRecorder.getVertx().get(), options); |
| 69 | + runTest(client, plainUrl.getPort()); |
| 70 | + } |
| 71 | + |
| 72 | + private void runTest(WebClient client, int port) throws InterruptedException, ExecutionException { |
| 73 | + CompletableFuture<HttpResponse<Buffer>> result = new CompletableFuture<>(); |
| 74 | + |
| 75 | + client |
| 76 | + .get(port, "localhost", "/ping") |
| 77 | + .send(ar -> { |
| 78 | + if (ar.succeeded()) { |
| 79 | + result.complete(ar.result()); |
| 80 | + } else { |
| 81 | + result.completeExceptionally(ar.cause()); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + HttpResponse<Buffer> response = result.get(); |
| 86 | + Assertions.assertNotEquals(HttpVersion.HTTP_2, response.version()); |
| 87 | + Assertions.assertEquals(PING_DATA, response.bodyAsString()); |
| 88 | + } |
| 89 | + |
| 90 | + @ApplicationScoped |
| 91 | + static class MyBean { |
| 92 | + public void register(@Observes Router router) { |
| 93 | + router.get("/ping").handler(rc -> { |
| 94 | + rc.response().end(PING_DATA); |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments