diff --git a/vertx-web-api-service/src/main/java/io/vertx/ext/web/api/service/impl/RouteToEBServiceHandlerImpl.java b/vertx-web-api-service/src/main/java/io/vertx/ext/web/api/service/impl/RouteToEBServiceHandlerImpl.java index 0643deed29..50e286d211 100644 --- a/vertx-web-api-service/src/main/java/io/vertx/ext/web/api/service/impl/RouteToEBServiceHandlerImpl.java +++ b/vertx-web-api-service/src/main/java/io/vertx/ext/web/api/service/impl/RouteToEBServiceHandlerImpl.java @@ -53,7 +53,9 @@ public void handle(RoutingContext routingContext) { if (op.getStatusMessage() != null) response.setStatusMessage(op.getStatusMessage()); if (op.getHeaders() != null) - op.getHeaders().forEach(h -> response.putHeader(h.getKey(), h.getValue())); + response.headers().addAll(op.getHeaders()); + if (deliveryOptions.getHeaders() != null) + response.headers().addAll(deliveryOptions.getHeaders()); if (op.getPayload() != null) response.end(op.getPayload()); else diff --git a/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/RouteToEBServiceHandlerTest.java b/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/RouteToEBServiceHandlerTest.java index ec7b4e0dfc..d7cec1ce4d 100644 --- a/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/RouteToEBServiceHandlerTest.java +++ b/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/RouteToEBServiceHandlerTest.java @@ -2,9 +2,11 @@ import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; +import io.vertx.core.eventbus.DeliveryOptions; import io.vertx.core.eventbus.MessageConsumer; import io.vertx.core.http.HttpHeaders; import io.vertx.core.http.HttpMethod; +import io.vertx.core.http.impl.headers.HeadersMultiMap; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import io.vertx.core.json.pointer.JsonPointer; @@ -26,15 +28,11 @@ import java.net.URI; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.Arrays; import static io.vertx.ext.web.validation.builder.Bodies.json; import static io.vertx.ext.web.validation.builder.Parameters.param; -import static io.vertx.ext.web.validation.testutils.TestRequest.bodyResponse; -import static io.vertx.ext.web.validation.testutils.TestRequest.emptyResponse; -import static io.vertx.ext.web.validation.testutils.TestRequest.jsonBodyResponse; -import static io.vertx.ext.web.validation.testutils.TestRequest.statusCode; -import static io.vertx.ext.web.validation.testutils.TestRequest.statusMessage; -import static io.vertx.ext.web.validation.testutils.TestRequest.testRequest; +import static io.vertx.ext.web.validation.testutils.TestRequest.*; import static io.vertx.json.schema.common.dsl.Schemas.anyOf; import static io.vertx.json.schema.common.dsl.Schemas.arraySchema; import static io.vertx.json.schema.common.dsl.Schemas.intSchema; @@ -122,7 +120,7 @@ public void serviceProxyDataObjectTest(Vertx vertx, VertxTestContext testContext "src", "test", "resources", "filter.json"))))); schemaRepo.dereference("app://filter.json", filterSchema); - + router .post("/test") .handler(BodyHandler.create()) @@ -217,6 +215,35 @@ public void extraPayloadTest(Vertx vertx, VertxTestContext testContext) { .send(testContext, checkpoint); } + @Test + void headersTest(Vertx vertx, VertxTestContext testContext) { + Checkpoint checkpoint = testContext.checkpoint(); + + TestService service = new TestServiceImpl(vertx); + final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress"); + consumer = serviceBinder.register(TestService.class, service); + + HeadersMultiMap headers = HeadersMultiMap.headers(); + headers.add("Set-Cookie", "cookie1=cookie1"); + headers.add("Set-Cookie", "cookie2=cookie2"); + + DeliveryOptions deliveryOptions = new DeliveryOptions(); + deliveryOptions.setHeaders(headers); + + router + .get("/test") + .handler( + ValidationHandlerBuilder.create(schemaRepo).build() + ).handler( + RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testHeaders", deliveryOptions) + ); + + testRequest(client, HttpMethod.GET, "/test") + .expect(statusCode(200), statusMessage("OK"), responseHeaders("Set-Cookie", Arrays.asList("cookie1=cookie1", "cookie2=cookie2"))) + .expect(emptyResponse()) + .send(testContext, checkpoint); + } + @Test public void serviceProxyManualFailureTest(Vertx vertx, VertxTestContext testContext) { Checkpoint checkpoint = testContext.checkpoint(2); diff --git a/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestService.java b/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestService.java index ecbfa834f0..bdca48ecc4 100644 --- a/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestService.java +++ b/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestService.java @@ -21,6 +21,9 @@ public interface TestService { @Deprecated Future testAuthorization(ServiceRequest context); + @Deprecated + Future testHeaders(ServiceRequest context); + static TestService create(Vertx vertx) { return new TestServiceImpl(vertx); } diff --git a/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestServiceImpl.java b/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestServiceImpl.java index b5fc729133..12501b0c98 100644 --- a/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestServiceImpl.java +++ b/vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestServiceImpl.java @@ -2,6 +2,7 @@ import io.vertx.core.Future; import io.vertx.core.Vertx; +import io.vertx.core.http.impl.headers.HeadersMultiMap; import io.vertx.core.json.JsonObject; public class TestServiceImpl implements TestService { @@ -53,4 +54,10 @@ public Future testAuthorization(ServiceRequest context) { ServiceResponse.completedWithJson(new JsonObject().put("result", context.getHeaders().get("Authorization")))) ; } + + public Future testHeaders(ServiceRequest context) { + return Future.succeededFuture( + new ServiceResponse(200, "OK", null, context.getHeaders()) + ); + } } diff --git a/vertx-web-validation/src/test/java/io/vertx/ext/web/validation/testutils/TestRequest.java b/vertx-web-validation/src/test/java/io/vertx/ext/web/validation/testutils/TestRequest.java index 912be17a5d..9cab06f793 100644 --- a/vertx-web-validation/src/test/java/io/vertx/ext/web/validation/testutils/TestRequest.java +++ b/vertx-web-validation/src/test/java/io/vertx/ext/web/validation/testutils/TestRequest.java @@ -375,6 +375,12 @@ public static Consumer> responseHeader(String headerName, S }; } + public static Consumer> responseHeaders(String headerName, List headerValues) { + return res -> { + assertEquals(headerValues.toString(), res.headers().getAll(headerName).toString()); + }; + } + public static Consumer> stringBody(Consumer assertBody) { return res -> { assertBody.accept(res.bodyAsString());