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");