Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message headers that weren't set in the http response headers. #2564

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public interface TestService {
@Deprecated
Future<ServiceResponse> testAuthorization(ServiceRequest context);

@Deprecated
Future<ServiceResponse> testHeaders(ServiceRequest context);

static TestService create(Vertx vertx) {
return new TestServiceImpl(vertx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -53,4 +54,10 @@ public Future<ServiceResponse> testAuthorization(ServiceRequest context) {
ServiceResponse.completedWithJson(new JsonObject().put("result", context.getHeaders().get("Authorization"))))
;
}

public Future<ServiceResponse> testHeaders(ServiceRequest context) {
return Future.succeededFuture(
new ServiceResponse(200, "OK", null, context.getHeaders())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ public static Consumer<HttpResponse<Buffer>> responseHeader(String headerName, S
};
}

public static Consumer<HttpResponse<Buffer>> responseHeaders(String headerName, List<String> headerValues) {
return res -> {
assertEquals(headerValues.toString(), res.headers().getAll(headerName).toString());
};
}

public static Consumer<HttpResponse<Buffer>> stringBody(Consumer<String> assertBody) {
return res -> {
assertBody.accept(res.bodyAsString());
Expand Down