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

[2678] Send multiple headers for url-encoded and multipart-form bodies #2679

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 @@ -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);
Comment on lines +448 to +451
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I do not quite understand why headers are copied twice. See

)

Without the second copying all tests are also passed. Check test commit ilia1243@f470308

Though I see some activity with this code

  1. 8c482962
  2. 4ea2fb35

I did not remove this part, because do not see the whole picture. Please comment if this can be removed.

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