From f4dd014ae4be5e4a4d9f1198797fbfcfe98636ec Mon Sep 17 00:00:00 2001 From: ilia1243 Date: Mon, 11 Nov 2024 21:53:46 +0300 Subject: [PATCH] [2678] Send multiple headers for url-encoded and multipart-form bodies Bug: https://github.com/vert-x3/vertx-web/issues/2678 --- .../ext/web/client/impl/HttpContext.java | 9 +++-- .../ext/web/client/impl/HttpRequestImpl.java | 15 +++++--- .../ext/web/client/tests/WebClientTest.java | 34 +++++++++++++++++++ 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/HttpContext.java b/vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/HttpContext.java index 16f1a3371a..becfd3a3f5 100644 --- a/vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/HttpContext.java +++ b/vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/HttpContext.java @@ -443,11 +443,14 @@ private void handleCreateRequest() { fail(e); return; } - for (String headerName : this.request.headers().names()) { - requestOptions.putHeader(headerName, this.request.headers().get(headerName)); + MultiMap headers = this.request.checkHeaders(requestOptions); + MultiMap requestHeaders = this.request.headers(); + for (String headerName : requestHeaders.names()) { + headers.remove(headerName); } + headers.addAll(requestHeaders); multipartForm.headers().forEach(header -> { - requestOptions.putHeader(header.getKey(), header.getValue()); + headers.set(header.getKey(), header.getValue()); }); } if (body instanceof ReadStream) { diff --git a/vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/HttpRequestImpl.java b/vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/HttpRequestImpl.java index c32863b678..cf955e3e4e 100644 --- a/vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/HttpRequestImpl.java +++ b/vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/HttpRequestImpl.java @@ -535,13 +535,18 @@ Future> send(String contentType, Object body) { return ctx.future(); } + MultiMap checkHeaders(RequestOptions options) { + MultiMap tmp = options.getHeaders(); + if (tmp == null) { + tmp = MultiMap.caseInsensitiveMultiMap(); + options.setHeaders(tmp); + } + return tmp; + } + void mergeHeaders(RequestOptions options) { if (headers != null) { - MultiMap tmp = options.getHeaders(); - if (tmp == null) { - tmp = MultiMap.caseInsensitiveMultiMap(); - options.setHeaders(tmp); - } + MultiMap tmp = checkHeaders(options); tmp.addAll(headers); } } diff --git a/vertx-web-client/src/test/java/io/vertx/ext/web/client/tests/WebClientTest.java b/vertx-web-client/src/test/java/io/vertx/ext/web/client/tests/WebClientTest.java index 57ecc559ae..5dd983f915 100644 --- a/vertx-web-client/src/test/java/io/vertx/ext/web/client/tests/WebClientTest.java +++ b/vertx-web-client/src/test/java/io/vertx/ext/web/client/tests/WebClientTest.java @@ -1183,6 +1183,23 @@ public void testFormUrlEncodedUnescaped() throws Exception { await(); } + @Test + public void testFormUrlEncodedMultipleHeaders() throws Exception { + server.requestHandler(req -> { + req.setExpectMultipart(true); + req.endHandler(v -> { + assertEquals(Arrays.asList("1", "2"), req.headers().getAll("bla")); + req.response().end(); + }); + }); + startServer(); + MultiMap form = MultiMap.caseInsensitiveMultiMap(); + HttpRequest builder = webClient.post("/somepath"); + builder.putHeader("bla", Arrays.asList("1", "2")); + builder.sendForm(form).onComplete(onSuccess(resp -> complete())); + await(); + } + @Test public void testFormMultipart() throws Exception { server.requestHandler(req -> { @@ -1395,6 +1412,23 @@ static Upload memoryUpload(String name, String filename, Buffer data) { } } + @Test + public void testMultipartFormMultipleHeaders() throws Exception { + server.requestHandler(req -> { + req.setExpectMultipart(true); + req.endHandler(v -> { + assertEquals(Arrays.asList("1", "2"), req.headers().getAll("bla")); + req.response().end(); + }); + }); + startServer(); + HttpRequest builder = webClient.post("somepath"); + MultipartForm form = MultipartForm.create(); + builder.putHeader("bla", Arrays.asList("1", "2")); + builder.sendMultipartForm(form).onComplete(onSuccess(resp -> complete())); + await(); + } + @Test public void testFileUploadWhenFileDoesNotExist() { HttpRequest builder = webClient.post("somepath");