Skip to content

Commit

Permalink
(#243) Fix bug with form parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoss committed Apr 26, 2021
1 parent 0f8f7df commit f831a44
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/main/java/com/jcabi/http/request/BaseRequest.java
Expand Up @@ -720,32 +720,16 @@ private static final class FormEncodedBody implements RequestBody {
*/
private final transient BaseRequest owner;

/**
* URL form character to prepend.
*/
private final transient String prepend;

/**
* Public ctor.
* @param req Request
* @param body Text to encapsulate
*/
FormEncodedBody(final BaseRequest req, final byte[] body) {
this(req, body, "");
}

/**
* Public ctor.
* @param req Request
* @param body Text to encapsulate
* @param pre Character to prepend
*/
FormEncodedBody(
final BaseRequest req, final byte[] body, final String pre
final BaseRequest req, final byte[] body
) {
this.owner = req;
this.text = body.clone();
this.prepend = pre;
}

@Override
Expand Down Expand Up @@ -791,10 +775,13 @@ public RequestBody set(final byte[] txt) {
@Override
public RequestBody formParam(final String name, final Object value) {
try {
final StringBuilder builder = new StringBuilder(this.get());
if (!builder.toString().isEmpty()) {
builder.append('&');
}
return new BaseRequest.FormEncodedBody(
this.owner,
new StringBuilder(this.get())
.append(this.prepend)
builder
.append(name)
.append('=')
.append(
Expand All @@ -804,8 +791,7 @@ public RequestBody formParam(final String name, final Object value) {
)
)
.toString()
.getBytes(BaseRequest.CHARSET),
"&"
.getBytes(BaseRequest.CHARSET)
);
} catch (final UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/jcabi/http/request/BaseRequestTest.java
Expand Up @@ -29,8 +29,13 @@
*/
package com.jcabi.http.request;

import com.jcabi.http.Request;
import com.jcabi.http.Wire;
import com.jcabi.http.mock.MkAnswer;
import com.jcabi.http.mock.MkContainer;
import com.jcabi.http.mock.MkGrizzlyContainer;
import com.jcabi.immutable.ArrayMap;
import java.io.IOException;
import javax.json.Json;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -174,6 +179,29 @@ public void execute() throws Throwable {
);
}

@Test
void shouldHaveCorrectFormParameters() throws IOException {
final MkContainer srv = new MkGrizzlyContainer()
.next(new MkAnswer.Simple("OK")).start();
new JdkRequest(srv.home())
.body()
.formParam("foo1", "bar1")
.back()
.body()
.formParam("foo2", "bar2")
.back()
.body()
.formParam("foo3", "bar3")
.formParam("foo4", "bar4")
.back()
.method(Request.POST)
.fetch();
MatcherAssert.assertThat(
srv.take().body(),
Matchers.is("foo1=bar1&foo2=bar2&foo3=bar3&foo4=bar4")
);
}

/**
* Boundary error message.
*
Expand Down

0 comments on commit f831a44

Please sign in to comment.