Skip to content

Commit

Permalink
Add tests coverage for REST client reactive using HTTP2
Browse files Browse the repository at this point in the history
  • Loading branch information
jedla97 committed Feb 6, 2024
1 parent a672d13 commit 92c2f9d
Show file tree
Hide file tree
Showing 11 changed files with 521 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.ts.http.restclient.reactive;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.smallrye.mutiny.Uni;

@RegisterRestClient
@Path("/httpVersion")
public interface HttpVersionClient {

@GET
@Path("synchronous")
Response getClientHttpVersion(@QueryParam("name") String name);

@GET
@Path("asynchronous")
Uni<Response> getClientHttpVersionAsync(@QueryParam("name") String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.ts.http.restclient.reactive;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient(configKey = "http-client-with-config-key")
public interface HttpVersionClientWithConfigKey extends HttpVersionClient {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.ts.http.restclient.reactive;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient(configKey = "https-client-with-config-key")
public interface HttpsVersionClientWithConfigKey extends HttpVersionClient {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.ts.http.restclient.reactive.resources;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Response;

import io.smallrye.mutiny.Uni;

@ApplicationScoped
@Path("/httpVersion")
public class HttpVersionBaseResource {

@GET
@Path("synchronous")
public Response sayHello(@QueryParam("name") String name) {
return Response.ok("Hello " + name).build();
}

@GET
@Path("asynchronous")
public Uni<Response> sayHelloAsync(@QueryParam("name") String name) {
return Uni.createFrom().item(Response.ok("Hello " + name).build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package io.quarkus.ts.http.restclient.reactive.resources;

import java.net.MalformedURLException;
import java.net.URI;
import java.time.Duration;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.resteasy.reactive.client.impl.ClientResponseImpl;

import io.quarkus.ts.http.restclient.reactive.HttpVersionClient;
import io.quarkus.ts.http.restclient.reactive.HttpVersionClientWithConfigKey;
import io.quarkus.ts.http.restclient.reactive.HttpsVersionClientWithConfigKey;
import io.vertx.core.http.HttpVersion;

@Path("/http2")
public class HttpVersionClientResource {

public static final String WRONG_HTTP_VERSION = "The HTTP version should be HTTP_2 but is ";
public static final String NAME = "Rest Client";
private static final int TIMEOUT_SECONDS = 10;
private final URI baseUri;
private final URI securedBaseUri;

@Inject
@RestClient
HttpVersionClientWithConfigKey httpVersionClientWithConfigKey;

@Inject
@RestClient
HttpsVersionClientWithConfigKey httpsVersionClientWithConfigKey;

public HttpVersionClientResource(@ConfigProperty(name = "quarkus.http.port") int httpPort,
@ConfigProperty(name = "quarkus.http.ssl-port") int sslHttpPort,
@ConfigProperty(name = "quarkus.http.host") String baseHost) {
this.baseUri = URI.create("http://" + baseHost + ":" + httpPort);
this.securedBaseUri = URI.create("https://" + baseHost + ":" + sslHttpPort);
}

@GET
@Path("https-synchronous")
public Response httpsSynchronous() throws MalformedURLException {
return createSynchronousResponseForClient(securedBaseUri);
}

@GET
@Path("http-synchronous")
public Response httpSynchronous() throws MalformedURLException {
return createSynchronousResponseForClient(baseUri);
}

@GET
@Path("https-asynchronous")
public Response httpsAsynchronous() throws MalformedURLException {
return createAsynchronousResponseForClient(securedBaseUri);
}

@GET
@Path("http-asynchronous")
public Response httpAsynchronous() throws MalformedURLException {
return createAsynchronousResponseForClient(baseUri);
}

@GET
@Path("https-synchronous-for-client-with-key")
public Response httpsSynchronousForClientWithKey() {
Response response = httpsVersionClientWithConfigKey.getClientHttpVersion(NAME);
return checkHttpVersion(response);
}

@GET
@Path("http-synchronous-for-client-with-key")
public Response httpSynchronousForClientWithKey() {
Response response = httpVersionClientWithConfigKey.getClientHttpVersion(NAME);
return checkHttpVersion(response);
}

@GET
@Path("https-asynchronous-for-client-with-key")
public Response httpsAsynchronousForClientWithKey() {
Response response = httpsVersionClientWithConfigKey.getClientHttpVersionAsync(NAME)
.await().atMost(Duration.ofSeconds(TIMEOUT_SECONDS));

return checkHttpVersion(response);
}

@GET
@Path("http-asynchronous-for-client-with-key")
public Response httpAsynchronousForClientWithKey() {
Response response = httpVersionClientWithConfigKey.getClientHttpVersionAsync(NAME)
.await().atMost(Duration.ofSeconds(TIMEOUT_SECONDS));

return checkHttpVersion(response);
}

/**
* Create HttpVersionClient and make REST client synchronous request to `httpVersion/synchronous endpoint`
* Response from the client is checked if http 2 was used
*
* @param uri base url for client to use
* @return Response contain original response from endpoint or error message containing actual http version used
*/
private Response createSynchronousResponseForClient(URI uri) throws MalformedURLException {
HttpVersionClient httpVersionClient = RestClientBuilder.newBuilder()
.baseUrl(uri.toURL())
.build(HttpVersionClient.class);

Response response = httpVersionClient.getClientHttpVersion(NAME);
return checkHttpVersion(response);
}

/**
* Create HttpVersionClient and make REST client asynchronous request to `httpVersion/asynchronous endpoint`
* Response from the client is checked if http 2 was used
*
* @param uri base url for client to use
* @return Response contain original response from endpoint or error message containing actual http version used
*/
private Response createAsynchronousResponseForClient(URI uri) throws MalformedURLException {
HttpVersionClient httpVersionClient = RestClientBuilder.newBuilder()
.baseUrl(uri.toURL())
.build(HttpVersionClient.class);

Response response = httpVersionClient.getClientHttpVersionAsync(NAME).await()
.atMost(Duration.ofSeconds(TIMEOUT_SECONDS));
return checkHttpVersion(response);
}

/**
* Check response from the REST client if http 2 is used
*
* @param response response of REST client
* @return Response contain original response from endpoint or error message containing actual http version used
*/
private Response checkHttpVersion(Response response) {
String httpVersion = ((ClientResponseImpl) response).getHttpVersion();
if (httpVersion.equals(HttpVersion.HTTP_2.name())) {
return response;
}

return Response.ok(WRONG_HTTP_VERSION + httpVersion).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quarkus.tls.trust-all=true
quarkus.http.ssl.certificate.key-store-file=META-INF/keystore.jks
quarkus.http.ssl.certificate.key-store-password=password
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.rest-client.http-client-with-config-key.url=http://${quarkus.http.host}:${quarkus.http.port}
quarkus.rest-client.https-client-with-config-key.url=https://${quarkus.http.host}:${quarkus.http.ssl-port}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkus.ts.http.restclient.reactive;

import static io.quarkus.ts.http.restclient.reactive.resources.HttpVersionClientResource.NAME;
import static org.hamcrest.Matchers.containsString;

import org.junit.jupiter.api.Test;

import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.QuarkusApplication;

@QuarkusScenario
public class HttpVersionAlpnIT {

String SUCCESSFUL_RESPONSE_STRING = "Hello " + NAME;

@QuarkusApplication(ssl = true)
static RestService app = new RestService()
.withProperties("httpVersion.properties")
.withProperty("quarkus.rest-client.alpn", "true");

@Test
public void testSyncResponseWithSingleClientOnGlobalClient() {
app.given()
.get("/http2/https-synchronous")
.then()
.statusCode(200)
.body(containsString(SUCCESSFUL_RESPONSE_STRING));
}

@Test
public void testAsyncResponseWithSingleClientOnGlobalClient() {
app.given()
.get("/http2/https-asynchronous")
.then()
.statusCode(200)
.body(containsString(SUCCESSFUL_RESPONSE_STRING));
}

@Test
public void testSyncResponseWithSingleClientOnSingleClient() {
app.given()
.get("/http2/https-synchronous-for-client-with-key")
.then()
.statusCode(200)
.body(containsString(SUCCESSFUL_RESPONSE_STRING));
}

@Test
public void testAsyncResponseWithSingleClientOnSingleClient() {
app.given()
.get("/http2/https-asynchronous-for-client-with-key")
.then()
.statusCode(200)
.body(containsString(SUCCESSFUL_RESPONSE_STRING));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.ts.http.restclient.reactive;

import static io.quarkus.ts.http.restclient.reactive.resources.HttpVersionClientResource.NAME;
import static io.quarkus.ts.http.restclient.reactive.resources.HttpVersionClientResource.WRONG_HTTP_VERSION;
import static org.hamcrest.Matchers.containsString;

import org.junit.jupiter.api.Test;

import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.QuarkusApplication;

@QuarkusScenario
public class HttpVersionAlpnWithConfigKeyIT {

String SUCCESSFUL_RESPONSE_STRING = "Hello " + NAME;

@QuarkusApplication(ssl = true)
static RestService app = new RestService()
.withProperties("httpVersion.properties")
.withProperty("quarkus.rest-client.http-client-with-config-key.alpn", "true")
.withProperty("quarkus.rest-client.https-client-with-config-key.alpn", "true");

@Test
public void testSyncResponseWithSingleClientOnGlobalClient() {
app.given()
.get("/http2/https-synchronous")
.then()
.statusCode(200)
.body(containsString(WRONG_HTTP_VERSION));
}

@Test
public void testAsyncResponseWithSingleClientOnGlobalClient() {
app.given()
.get("/http2/https-asynchronous")
.then()
.statusCode(200)
.body(containsString(WRONG_HTTP_VERSION));
}

@Test
public void testSyncResponseWithSingleClientOnSingleClient() {
app.given()
.get("/http2/https-synchronous-for-client-with-key")
.then()
.statusCode(200)
.body(containsString(SUCCESSFUL_RESPONSE_STRING));
}

@Test
public void testAsyncResponseWithSingleClientOnSingleClient() {
app.given()
.get("/http2/https-asynchronous-for-client-with-key")
.then()
.statusCode(200)
.body(containsString(SUCCESSFUL_RESPONSE_STRING));
}
}
Loading

0 comments on commit 92c2f9d

Please sign in to comment.