|
| 1 | +package io.quarkus.resteasy.reactive.server.test.resource.basic; |
| 2 | + |
| 3 | +import static io.restassured.RestAssured.given; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
| 5 | + |
| 6 | +import java.util.function.Supplier; |
| 7 | + |
| 8 | +import jakarta.ws.rs.GET; |
| 9 | +import jakarta.ws.rs.Path; |
| 10 | +import jakarta.ws.rs.core.Response; |
| 11 | + |
| 12 | +import org.jboss.resteasy.reactive.RestPath; |
| 13 | +import org.jboss.resteasy.reactive.RestResponse; |
| 14 | +import org.jboss.resteasy.reactive.server.ServerExceptionMapper; |
| 15 | +import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; |
| 16 | +import org.jboss.resteasy.reactive.server.handlers.RestInitialHandler; |
| 17 | +import org.jboss.resteasy.reactive.server.mapping.RequestMapper; |
| 18 | +import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; |
| 19 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 20 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 23 | + |
| 24 | +import io.quarkus.arc.ClientProxy; |
| 25 | +import io.quarkus.resteasy.reactive.server.test.simple.PortProviderUtil; |
| 26 | +import io.quarkus.test.QuarkusUnitTest; |
| 27 | + |
| 28 | +class OverlappingResourceClassPathTest { |
| 29 | + @RegisterExtension |
| 30 | + static QuarkusUnitTest testExtension = new QuarkusUnitTest() |
| 31 | + .setArchiveProducer(new Supplier<>() { |
| 32 | + @Override |
| 33 | + public JavaArchive get() { |
| 34 | + JavaArchive war = ShrinkWrap.create(JavaArchive.class); |
| 35 | + war.addClasses(PortProviderUtil.class); |
| 36 | + war.addClasses(UsersResource.class); |
| 37 | + war.addClasses(UserResource.class); |
| 38 | + war.addClasses(GreetingResource.class); |
| 39 | + return war; |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + @Test |
| 44 | + void basicTest() { |
| 45 | + given() |
| 46 | + .get("/users/userId") |
| 47 | + .then() |
| 48 | + .statusCode(200) |
| 49 | + .body(equalTo("userId")); |
| 50 | + |
| 51 | + given() |
| 52 | + .get("/users/userId/by-id") |
| 53 | + .then() |
| 54 | + .statusCode(200) |
| 55 | + .body(equalTo("getByIdInUserResource-userId")); |
| 56 | + |
| 57 | + // test that only the User, and UsersResource have matched, and that the initial matches are sorted by remaining length |
| 58 | + given() |
| 59 | + .get("/users/userId/resource-does-not-exist") |
| 60 | + .then() |
| 61 | + .statusCode(404) |
| 62 | + .body(equalTo("/resource-does-not-exist|/userId/resource-does-not-exist|")); |
| 63 | + } |
| 64 | + |
| 65 | + @Path("/users") |
| 66 | + public static class UsersResource { |
| 67 | + |
| 68 | + @GET |
| 69 | + @Path("{id}") |
| 70 | + public String getByIdInUsersResource(@RestPath String id) { |
| 71 | + return id; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Path("/users/{id}") |
| 76 | + public static class UserResource { |
| 77 | + |
| 78 | + @GET |
| 79 | + @Path("by-id") |
| 80 | + public String getByIdInUserResource(@RestPath String id) { |
| 81 | + return "getByIdInUserResource-" + id; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Path("/i-do-not-match") |
| 86 | + public static class GreetingResource { |
| 87 | + |
| 88 | + @GET |
| 89 | + @Path("greet") |
| 90 | + public String greet() { |
| 91 | + return "Hello"; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @ServerExceptionMapper |
| 96 | + public RestResponse<String> handle(RuntimeException ignored, ServerRequestContext requestContext) { |
| 97 | + ResteasyReactiveRequestContext ctxt = (ResteasyReactiveRequestContext) ClientProxy.unwrap(requestContext); |
| 98 | + String remainings = ""; |
| 99 | + for (RequestMapper.RequestMatch<RestInitialHandler.InitialMatch> initialMatchRequestMatch : ctxt.getInitialMatches()) { |
| 100 | + remainings += initialMatchRequestMatch.remaining + "|"; |
| 101 | + } |
| 102 | + return RestResponse.status(Response.Status.NOT_FOUND, remainings); |
| 103 | + } |
| 104 | +} |
0 commit comments