This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch camel-3.20.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.20.x by this push: new 73fedc3c898 CAMEL-18967: Delegate checks whether a request body is allowed to Vert.x 73fedc3c898 is described below commit 73fedc3c89829db1036ef7cdf2c6a12bb88f4430 Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Wed Jan 25 09:46:46 2023 +0000 CAMEL-18967: Delegate checks whether a request body is allowed to Vert.x --- .../http/vertx/VertxPlatformHttpConsumer.java | 13 ++-- .../http/vertx/VertxPlatformHttpEngineTest.java | 83 ++++++++++++++++++++++ .../camel/component/platform/http/spi/Method.java | 31 +++----- 3 files changed, 96 insertions(+), 31 deletions(-) diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java index 01d373d4e8a..387bb93a6aa 100644 --- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java +++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java @@ -276,15 +276,10 @@ public class VertxPlatformHttpConsumer extends DefaultConsumer { populateAttachments(ctx.fileUploads(), result); } } else { - Method m = Method.valueOf(ctx.request().method().name()); - if (m.canHaveBody()) { - final RequestBody requestBody = ctx.body(); - final Buffer body = requestBody.buffer(); - if (body != null) { - result.setBody(body); - } else { - result.setBody(null); - } + final RequestBody requestBody = ctx.body(); + final Buffer body = requestBody.buffer(); + if (body != null) { + result.setBody(body); } else { result.setBody(null); } diff --git a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java index 133e26b855f..9acaecdcf02 100644 --- a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java +++ b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java @@ -21,12 +21,15 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Arrays; import java.util.Iterator; +import java.util.List; import java.util.concurrent.TimeUnit; import javax.activation.DataHandler; import io.restassured.RestAssured; import io.restassured.http.ContentType; +import io.restassured.response.ValidatableResponse; +import io.restassured.specification.RequestSpecification; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import io.vertx.core.json.JsonObject; @@ -40,6 +43,7 @@ import org.apache.camel.attachment.AttachmentMessage; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.platform.http.HttpEndpointModel; import org.apache.camel.component.platform.http.PlatformHttpComponent; +import org.apache.camel.component.platform.http.spi.Method; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.model.rest.RestParamType; import org.apache.camel.spi.RestConfiguration; @@ -49,11 +53,13 @@ import org.apache.camel.support.jsse.SSLContextParameters; import org.apache.camel.support.jsse.SSLContextServerParameters; import org.apache.camel.support.jsse.TrustManagersParameters; import org.apache.camel.test.AvailablePortFinder; +import org.hamcrest.Matcher; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.emptyString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -598,6 +604,83 @@ public class VertxPlatformHttpEngineTest { } } + @Test + public void testRequestBodyAllowed() throws Exception { + final CamelContext context = createCamelContext(); + + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("platform-http:/echo") + .setBody().simple("${body}"); + } + }); + + context.start(); + + for (Method method : Method.values()) { + ValidatableResponse validatableResponse = given() + .contentType(ContentType.JSON) + .when() + .body("{\"method\": \"" + method + "\"}") + .request(method.name(), "/echo") + .then() + .statusCode(200); + + Matcher<String> expectedBody; + if (method.equals(Method.HEAD)) { + // HEAD response body is ignored + validatableResponse.body(emptyString()); + } else { + validatableResponse.body("method", equalTo(method.name())); + } + } + } finally { + context.stop(); + } + } + + @Test + public void testRequestBodyAllowedFormUrlEncoded() throws Exception { + // Methods that are allowed a request body by Vert.x web for application/x-www-form-urlencoded + final List<Method> methodsWithBodyAllowed = List.of(Method.POST, Method.PUT, Method.PATCH, Method.DELETE); + final CamelContext context = createCamelContext(); + + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("platform-http:/test") + .setBody().simple("Hello ${body[method]}"); + } + }); + + context.start(); + + RequestSpecification request = given() + .when() + .contentType(ContentType.URLENC); + + for (Method method : Method.values()) { + if (methodsWithBodyAllowed.contains(method)) { + request.body("method=" + method) + .request(method.name(), "/test") + .then() + .statusCode(200) + .body(equalTo("Hello " + method)); + } else { + request.body(method) + .request(method.name(), "/test") + .then() + .statusCode(500); + } + } + } finally { + context.stop(); + } + } + static CamelContext createCamelContext() throws Exception { return createCamelContext(null); } diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/Method.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/Method.java index dc98b98d7b1..ad5cc17f5c9 100644 --- a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/Method.java +++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/Method.java @@ -26,35 +26,22 @@ import java.util.TreeSet; * An HTTP method. */ public enum Method { - GET(false), - HEAD(false), - POST(true), - PUT(true), - DELETE(false), - TRACE(false), - OPTIONS(false), - CONNECT(false), - PATCH( - true); + GET, + HEAD, + POST, + PUT, + DELETE, + TRACE, + OPTIONS, + CONNECT, + PATCH; private static final Set<Method> ALL = Collections.unmodifiableSet(new TreeSet<>(Arrays.asList(values()))); - private final boolean canHaveBody; - - private Method(boolean canHaveBody) { - this.canHaveBody = canHaveBody; - } public static Set<Method> getAll() { return ALL; } - /** - * @return {@code true} if HTTP requests with this {@link Method} can have a body; {@code false} otherwise - */ - public boolean canHaveBody() { - return canHaveBody; - } - /** * Parse the given comma separated {@code methodList} to a {@link Set} of {@link Method}s. If {@code methodList} is * empty or {@code null} returns {@link #ALL}.