-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests coverage for REST client reactive using HTTP2
- Loading branch information
Showing
11 changed files
with
521 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...ient-reactive/src/main/java/io/quarkus/ts/http/restclient/reactive/HttpVersionClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
.../src/main/java/io/quarkus/ts/http/restclient/reactive/HttpVersionClientWithConfigKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
7 changes: 7 additions & 0 deletions
7
...src/main/java/io/quarkus/ts/http/restclient/reactive/HttpsVersionClientWithConfigKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
26 changes: 26 additions & 0 deletions
26
...c/main/java/io/quarkus/ts/http/restclient/reactive/resources/HttpVersionBaseResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
...main/java/io/quarkus/ts/http/restclient/reactive/resources/HttpVersionClientResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
http/rest-client-reactive/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
2 changes: 2 additions & 0 deletions
2
http/rest-client-reactive/src/main/resources/httpVersion.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
57 changes: 57 additions & 0 deletions
57
...ient-reactive/src/test/java/io/quarkus/ts/http/restclient/reactive/HttpVersionAlpnIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
.../src/test/java/io/quarkus/ts/http/restclient/reactive/HttpVersionAlpnWithConfigKeyIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.