You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/10/30 10:47:57 UTC
[camel-quarkus] 01/02: Fix #220 platform-http component should
return 204 for success and no body
This is an automated email from the ASF dual-hosted git repository.
lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit b20849240e73559f761cadc0f64f098f4283590f
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Tue Oct 29 12:31:15 2019 +0100
Fix #220 platform-http component should return 204 for success and no body
---
.../http/runtime/QuarkusPlatformHttpConsumer.java | 36 ++++++++++++++++------
.../platform/http/it/PlatformHttpRouteBuilder.java | 11 +++++++
.../component/http/server/it/PlatformHttpTest.java | 36 ++++++++++++++++++++++
3 files changed, 73 insertions(+), 10 deletions(-)
diff --git a/extensions/platform-http/runtime/src/main/java/org/apache/camel/quarkus/component/platform/http/runtime/QuarkusPlatformHttpConsumer.java b/extensions/platform-http/runtime/src/main/java/org/apache/camel/quarkus/component/platform/http/runtime/QuarkusPlatformHttpConsumer.java
index 46886d1..051805c 100644
--- a/extensions/platform-http/runtime/src/main/java/org/apache/camel/quarkus/component/platform/http/runtime/QuarkusPlatformHttpConsumer.java
+++ b/extensions/platform-http/runtime/src/main/java/org/apache/camel/quarkus/component/platform/http/runtime/QuarkusPlatformHttpConsumer.java
@@ -152,11 +152,8 @@ public class QuarkusPlatformHttpConsumer extends DefaultConsumer {
static Object toHttpResponse(HttpServerResponse response, Message message, HeaderFilterStrategy headerFilterStrategy) {
final Exchange exchange = message.getExchange();
- final boolean failed = exchange.isFailed();
- final int defaultCode = failed ? 500 : 200;
-
- final int code = message.getHeader(Exchange.HTTP_RESPONSE_CODE, defaultCode, int.class);
+ final int code = determineResponseCode(exchange, message.getBody());
response.setStatusCode(code);
final TypeConverter tc = exchange.getContext().getTypeConverter();
@@ -220,18 +217,37 @@ public class QuarkusPlatformHttpConsumer extends DefaultConsumer {
return body;
}
+ /*
+ * Copied from org.apache.camel.http.common.DefaultHttpBinding.determineResponseCode(Exchange, Object)
+ * If DefaultHttpBinding.determineResponseCode(Exchange, Object) is moved to a module without the servlet-api
+ * dependency we could eventually consume it from there.
+ */
+ static int determineResponseCode(Exchange camelExchange, Object body) {
+ boolean failed = camelExchange.isFailed();
+ int defaultCode = failed ? 500 : 200;
+
+ Message message = camelExchange.getMessage();
+ Integer currentCode = message.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
+ int codeToUse = currentCode == null ? defaultCode : currentCode;
+
+ if (codeToUse != 500) {
+ if ((body == null) || (body instanceof String && ((String) body).trim().isEmpty())) {
+ // no content
+ codeToUse = currentCode == null ? 204 : currentCode;
+ }
+ }
+
+ return codeToUse;
+ }
+
static void writeResponse(RoutingContext ctx, Exchange camelExchange, HeaderFilterStrategy headerFilterStrategy) {
final Object body = toHttpResponse(ctx.response(), camelExchange.getMessage(), headerFilterStrategy);
final HttpServerResponse response = ctx.response();
if (body == null) {
LOG.tracef("No payload to send as reply for exchange: %s", camelExchange);
- response.putHeader("Content-Type", "text/plain; charset=utf-8");
- response.end("No response available");
- return;
- }
-
- if (body instanceof String) {
+ response.end();
+ } else if (body instanceof String) {
response.end((String) body);
} else if (body instanceof InputStream) {
final byte[] bytes = new byte[4096];
diff --git a/integration-tests/platform-http/src/main/java/org/apache/camel/quarkus/component/platform/http/it/PlatformHttpRouteBuilder.java b/integration-tests/platform-http/src/main/java/org/apache/camel/quarkus/component/platform/http/it/PlatformHttpRouteBuilder.java
index e023020..82cdef9 100644
--- a/integration-tests/platform-http/src/main/java/org/apache/camel/quarkus/component/platform/http/it/PlatformHttpRouteBuilder.java
+++ b/integration-tests/platform-http/src/main/java/org/apache/camel/quarkus/component/platform/http/it/PlatformHttpRouteBuilder.java
@@ -87,5 +87,16 @@ public class PlatformHttpRouteBuilder extends RouteBuilder {
from("platform-http:/platform-http/produces?httpMethodRestrict=POST&produces=text/plain")
.setBody(simple("Hello ${body}"));
+ /* 204 tests */
+ from("platform-http:/platform-http/null-body")
+ .setBody(constant(null));
+ from("platform-http:/platform-http/empty-string-body")
+ .setBody().constant("");
+ from("platform-http:/platform-http/some-string")
+ .setBody().constant("No Content");
+ from("platform-http:/platform-http/empty-string-200")
+ .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
+ .setBody().constant("");
+
}
}
diff --git a/integration-tests/platform-http/src/test/java/org/apache/camel/quarkus/component/http/server/it/PlatformHttpTest.java b/integration-tests/platform-http/src/test/java/org/apache/camel/quarkus/component/http/server/it/PlatformHttpTest.java
index e95747c..fade646 100644
--- a/integration-tests/platform-http/src/test/java/org/apache/camel/quarkus/component/http/server/it/PlatformHttpTest.java
+++ b/integration-tests/platform-http/src/test/java/org/apache/camel/quarkus/component/http/server/it/PlatformHttpTest.java
@@ -198,4 +198,40 @@ class PlatformHttpTest {
.then()
.statusCode(299);
}
+
+
+ @Test
+ public void code204Null() throws Exception {
+ RestAssured.given()
+ .get("/platform-http/null-body")
+ .then()
+ .statusCode(204);
+ }
+
+ @Test
+ public void code204EmptyString() throws Exception {
+ RestAssured.given()
+ .get("/platform-http/empty-string-body")
+ .then()
+ .statusCode(204);
+ }
+
+ @Test
+ public void code204SomeString() throws Exception {
+ RestAssured.given()
+ .get("/platform-http/some-string")
+ .then()
+ .statusCode(200)
+ .body(equalTo("No Content"));
+ }
+
+ @Test
+ public void code200EmptyString() throws Exception {
+ RestAssured.given()
+ .get("/platform-http/empty-string-200")
+ .then()
+ .statusCode(200)
+ .body(equalTo(""));
+ }
+
}