Skip to content

Commit abc7721

Browse files
Fix message headers that weren't set in the http response headers.
1 parent f0037f8 commit abc7721

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

vertx-web-api-service/src/main/java/io/vertx/ext/web/api/service/impl/RouteToEBServiceHandlerImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public void handle(RoutingContext routingContext) {
5353
if (op.getStatusMessage() != null)
5454
response.setStatusMessage(op.getStatusMessage());
5555
if (op.getHeaders() != null)
56-
op.getHeaders().forEach(h -> response.putHeader(h.getKey(), h.getValue()));
56+
response.headers().addAll(op.getHeaders());
57+
if (deliveryOptions.getHeaders() != null)
58+
response.headers().addAll(deliveryOptions.getHeaders());
5759
if (op.getPayload() != null)
5860
response.end(op.getPayload());
5961
else

vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/RouteToEBServiceHandlerTest.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import io.vertx.core.Vertx;
44
import io.vertx.core.buffer.Buffer;
5+
import io.vertx.core.eventbus.DeliveryOptions;
56
import io.vertx.core.eventbus.MessageConsumer;
67
import io.vertx.core.http.HttpHeaders;
78
import io.vertx.core.http.HttpMethod;
9+
import io.vertx.core.http.impl.headers.HeadersMultiMap;
810
import io.vertx.core.json.JsonArray;
911
import io.vertx.core.json.JsonObject;
1012
import io.vertx.core.json.pointer.JsonPointer;
@@ -26,15 +28,11 @@
2628
import java.net.URI;
2729
import java.nio.file.Files;
2830
import java.nio.file.Paths;
31+
import java.util.Arrays;
2932

3033
import static io.vertx.ext.web.validation.builder.Bodies.json;
3134
import static io.vertx.ext.web.validation.builder.Parameters.param;
32-
import static io.vertx.ext.web.validation.testutils.TestRequest.bodyResponse;
33-
import static io.vertx.ext.web.validation.testutils.TestRequest.emptyResponse;
34-
import static io.vertx.ext.web.validation.testutils.TestRequest.jsonBodyResponse;
35-
import static io.vertx.ext.web.validation.testutils.TestRequest.statusCode;
36-
import static io.vertx.ext.web.validation.testutils.TestRequest.statusMessage;
37-
import static io.vertx.ext.web.validation.testutils.TestRequest.testRequest;
35+
import static io.vertx.ext.web.validation.testutils.TestRequest.*;
3836
import static io.vertx.json.schema.common.dsl.Schemas.anyOf;
3937
import static io.vertx.json.schema.common.dsl.Schemas.arraySchema;
4038
import static io.vertx.json.schema.common.dsl.Schemas.intSchema;
@@ -122,7 +120,7 @@ public void serviceProxyDataObjectTest(Vertx vertx, VertxTestContext testContext
122120
"src", "test",
123121
"resources", "filter.json")))));
124122
schemaRepo.dereference("app://filter.json", filterSchema);
125-
123+
126124
router
127125
.post("/test")
128126
.handler(BodyHandler.create())
@@ -217,6 +215,35 @@ public void extraPayloadTest(Vertx vertx, VertxTestContext testContext) {
217215
.send(testContext, checkpoint);
218216
}
219217

218+
@Test
219+
void headersTest(Vertx vertx, VertxTestContext testContext) {
220+
Checkpoint checkpoint = testContext.checkpoint();
221+
222+
TestService service = new TestServiceImpl(vertx);
223+
final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
224+
consumer = serviceBinder.register(TestService.class, service);
225+
226+
HeadersMultiMap headers = HeadersMultiMap.headers();
227+
headers.add("Set-Cookie", "cookie1=cookie1");
228+
headers.add("Set-Cookie", "cookie2=cookie2");
229+
230+
DeliveryOptions deliveryOptions = new DeliveryOptions();
231+
deliveryOptions.setHeaders(headers);
232+
233+
router
234+
.get("/test")
235+
.handler(
236+
ValidationHandlerBuilder.create(schemaRepo).build()
237+
).handler(
238+
RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testHeaders", deliveryOptions)
239+
);
240+
241+
testRequest(client, HttpMethod.GET, "/test")
242+
.expect(statusCode(200), statusMessage("OK"), responseHeaders("Set-Cookie", Arrays.asList("cookie1=cookie1", "cookie2=cookie2")))
243+
.expect(emptyResponse())
244+
.send(testContext, checkpoint);
245+
}
246+
220247
@Test
221248
public void serviceProxyManualFailureTest(Vertx vertx, VertxTestContext testContext) {
222249
Checkpoint checkpoint = testContext.checkpoint(2);

vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public interface TestService {
2121
@Deprecated
2222
Future<ServiceResponse> testAuthorization(ServiceRequest context);
2323

24+
@Deprecated
25+
Future<ServiceResponse> testHeaders(ServiceRequest context);
26+
2427
static TestService create(Vertx vertx) {
2528
return new TestServiceImpl(vertx);
2629
}

vertx-web-api-service/src/test/java/io/vertx/ext/web/api/service/TestServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.vertx.core.Future;
44
import io.vertx.core.Vertx;
5+
import io.vertx.core.http.impl.headers.HeadersMultiMap;
56
import io.vertx.core.json.JsonObject;
67

78
public class TestServiceImpl implements TestService {
@@ -53,4 +54,10 @@ public Future<ServiceResponse> testAuthorization(ServiceRequest context) {
5354
ServiceResponse.completedWithJson(new JsonObject().put("result", context.getHeaders().get("Authorization"))))
5455
;
5556
}
57+
58+
public Future<ServiceResponse> testHeaders(ServiceRequest context) {
59+
return Future.succeededFuture(
60+
new ServiceResponse(200, "OK", null, context.getHeaders())
61+
);
62+
}
5663
}

vertx-web-validation/src/test/java/io/vertx/ext/web/validation/testutils/TestRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ public static Consumer<HttpResponse<Buffer>> responseHeader(String headerName, S
375375
};
376376
}
377377

378+
public static Consumer<HttpResponse<Buffer>> responseHeaders(String headerName, List<String> headerValues) {
379+
return res -> {
380+
assertEquals(headerValues.toString(), res.headers().getAll(headerName).toString());
381+
};
382+
}
383+
378384
public static Consumer<HttpResponse<Buffer>> stringBody(Consumer<String> assertBody) {
379385
return res -> {
380386
assertBody.accept(res.bodyAsString());

0 commit comments

Comments
 (0)