Skip to content

Commit

Permalink
[2678] Send multiple headers for url-encoded and multipart-form bodies
Browse files Browse the repository at this point in the history
Bug: #2678
  • Loading branch information
ilia1243 authored and ilia1243 committed Nov 11, 2024
1 parent 2a661ad commit f4dd014
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<?>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,18 @@ Future<HttpResponse<T>> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Buffer> 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 -> {
Expand Down Expand Up @@ -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<Buffer> 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<Buffer> builder = webClient.post("somepath");
Expand Down

0 comments on commit f4dd014

Please sign in to comment.