Skip to content

Commit

Permalink
Add Reactive Routes support to @TestHTTPEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand authored and stuartwdouglas committed Jul 31, 2020
1 parent bc68117 commit fddd2e1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/getting-started-testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ For now `@TestHTTPResource` allows you to inject `URI`, `URL` and `String` repre
== Testing a specific endpoint

Both RESTassured and `@TestHTTPResource` allow you to specify the endpoint class you are testing rather than hard coding
a path. This currently supports both JAX-RS endpoints and Servlets. This makes it a lot easier to see exactly which endpoints
a path. This currently supports both JAX-RS endpoints, Servlets and Reactive Routes. This makes it a lot easier to see exactly which endpoints
a given test is testing.

For the purposes of these examples I am going to assume we have an endpoint that looks like the following:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.quarkus.vertx.web.runtime;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Function;

import io.quarkus.runtime.test.TestHttpEndpointProvider;
import io.quarkus.vertx.web.RouteBase;

public class ReactiveRoutesTestHttpProvider implements TestHttpEndpointProvider {
@Override
public Function<Class<?>, String> endpointProvider() {
return new Function<Class<?>, String>() {
@Override
public String apply(Class<?> aClass) {
String value = null;
for (Annotation annotation : aClass.getAnnotations()) {
if (annotation.annotationType().getName().equals(RouteBase.class.getName())) {
try {
value = (String) annotation.annotationType().getMethod("path").invoke(annotation);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
if ((value == null) || value.isEmpty()) {
return null;
}
if (!value.startsWith("/")) {
value = "/" + value;
}
return value;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.vertx.web.runtime.ReactiveRoutesTestHttpProvider
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@

import org.junit.jupiter.api.Test;

import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@TestHTTPEndpoint(SimpleEndpoint.class)
public class SimpleEndpointTestCase {

@Test
public void testEndpoint() throws Exception {
when().get("/simple/person").then().statusCode(200)
when().get("/person").then().statusCode(200)
.body("name", is("Jan"))
.header("content-type", "application/json");
when().get("/simple/pet").then().statusCode(200)
when().get("/pet").then().statusCode(200)
.body("name", is("Jack"))
.header("content-type", "application/json");
when().get("/simple/pong").then().statusCode(200)
when().get("/pong").then().statusCode(200)
.body("name", is("ping"))
.header("content-type", "application/json");
given().body("{\"name\":\"pi\"}").post("/simple/data").then().statusCode(200)
given().body("{\"name\":\"pi\"}").post("/data").then().statusCode(200)
.body("name", is("pipi"))
.header("content-type", "application/json");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* can also be applied to {@link TestHTTPResource} fields to set the base path.
*
*
* This mechanism is pluggable, and currently supports JAX-RS endpoints.
* This mechanism is pluggable, and currently supports JAX-RS endpoints, Servlets and Reactive Routes.
*
*/
@Retention(RetentionPolicy.RUNTIME)
Expand Down

0 comments on commit fddd2e1

Please sign in to comment.