Skip to content

Commit

Permalink
Merge pull request #23095 from geoand/#23084
Browse files Browse the repository at this point in the history
Make @TestHTTPEndpoint work with path variables
  • Loading branch information
geoand authored Jan 21, 2022
2 parents c458f08 + c57f3a0 commit 38111b8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.it.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/otherTest/{name:[a-z]{1,10}}")
public class TestResourceWithVariable {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello(@PathParam("name") String name) {
return "hello " + name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.jupiter.api.Test;

import io.quarkus.it.rest.TestResource;
import io.quarkus.it.rest.TestResourceWithVariable;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.DisabledOnNativeImage;
import io.quarkus.test.junit.QuarkusTest;
Expand Down Expand Up @@ -278,4 +279,15 @@ public void testSSE() throws Exception {
containsString("<settings><foo bar=\"1\"/></settings>"),
containsString("<settings><foo bar=\"2\"/></settings>"));
}

@Test
@TestHTTPEndpoint(TestResourceWithVariable.class)
public void testPathVariable() {
RestAssured.given()
.pathParam("name", "yolo")
.when().get()
.then()
.statusCode(200)
.body(is("hello yolo"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,42 @@ public static String getEndpointPath(ExtensionContext context, List<Function<Cla
+ " for test method " + context.getRequiredTestMethod());
}
}
if (endpointPath != null) {
if (endpointPath.indexOf(':') != -1) {
return sanitizeEndpointPath(endpointPath);
}
}

return endpointPath;
}

/**
* Remove any sort of regex restrictions from "variables in the path"
*/
private static String sanitizeEndpointPath(String path) {
int openBrackets = 0;
boolean inRegex = false;
StringBuilder replaced = new StringBuilder(path.length() - 1);
for (int i = 0; i < path.length(); i++) {
char c = path.charAt(i);
if (c == '{') {
openBrackets++;
} else if (c == '}') {
openBrackets--;
if (openBrackets == 0) {
inRegex = false;
}
} else if ((c == ':') && (openBrackets > 0)) {
inRegex = true;
}
if (!inRegex) {
replaced.append(c);
}

}
return replaced.toString();
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
if (isNativeOrIntegrationTest(context.getRequiredTestClass())) {
Expand Down

0 comments on commit 38111b8

Please sign in to comment.