Skip to content

Commit

Permalink
Ensure a custom factory can be used with HttpClient#sendForm (#2265)
Browse files Browse the repository at this point in the history
HttpClient#sendForm API uses BiConsumer and not BiFunction.
The user cannot change the encoder in the callback.
When a custom factory is provided, apply the changes when the control is again in Reactor Netty.

Fixes #2259
  • Loading branch information
violetagg committed Jun 2, 2022
1 parent 1c4be66 commit e28067f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2021 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2011-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -131,7 +131,7 @@ public HttpClientForm factory(HttpDataFactory factory) {
}
this.newFactory = Objects.requireNonNull(factory, "factory");
this.needNewEncoder = true;
return applyChanges(request);
return this;
}

@Override
Expand Down Expand Up @@ -317,6 +317,8 @@ final HttpClientFormEncoder applyChanges(HttpRequest request) {

encoder.setBodyHttpDatas(getBodyListAttributes());

needNewEncoder = false;

return encoder;
}
catch (ErrorDataEncoderException ee) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory;
import io.netty.handler.codec.http.multipart.HttpData;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -109,6 +110,16 @@ void postUploadNoMultipart() throws Exception {
doTestPostUpload((req, form) -> form.multipart(false).attr("attr1", "attr2"), "attr1=attr2");
}

@Test
void postUploadNoMultipartWithCustomFactory() throws Exception {
doTestPostUpload((req, form) -> {
DefaultHttpDataFactory customFactory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
form.factory(customFactory)
.multipart(false)
.attr("attr1", "attr2");
}, "attr1=attr2");
}

@SuppressWarnings("unchecked")
private void doTestPostUpload(BiConsumer<? super HttpClientRequest, HttpClientForm> formCallback,
String expectedResponse) throws Exception {
Expand Down

0 comments on commit e28067f

Please sign in to comment.