Skip to content

Commit

Permalink
Merge pull request #6 from piotrkot/320-multipart-filename
Browse files Browse the repository at this point in the history
jcabi#320 - Adding optional arguments for formParam
  • Loading branch information
piotrkot authored Jun 25, 2022
2 parents 73067cc + 349ed8f commit 1944dc4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
6 changes: 2 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ install:
- cmd: SET JAVA_OPTS=-XX:MaxPermSize=2g -Xmx4g
- cmd: mvn --version
- cmd: java -version
- cmd: ruby --version
- cmd: gem install xcop
build_script:
- mvn --batch-mode -Dmaven.test.skip=true clean package
- mvn --batch-mode -Dmaven.test.skip=true clean package
test_script:
- mvn --batch-mode -Pqulice clean install
- mvn --batch-mode -Pqulice clean install
cache:
- C:\maven\
- C:\Users\appveyor\.m2
3 changes: 2 additions & 1 deletion src/main/java/com/jcabi/http/RequestBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ public interface RequestBody {
* Add form param.
* @param name Query param name
* @param value Value of the query param to set
* @param opt Optional values of the query param to set
* @return New alternated body
*/
RequestBody formParam(String name, Object value);
RequestBody formParam(String name, Object value, Object... opt);

/**
* Add form params.
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/jcabi/http/request/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,8 @@ public RequestBody set(final byte[] txt) {
}

@Override
public RequestBody formParam(final String name, final Object value) {
public RequestBody formParam(final String name, final Object value,
final Object... opt) {
final String boundary = this.boundary();
final String dashes = "--";
final byte[] last = Arrays.copyOfRange(
Expand All @@ -647,11 +648,17 @@ public RequestBody formParam(final String name, final Object value) {
} else {
bytes = value.toString().getBytes(BaseRequest.CHARSET);
}
final String filename;
if (opt.length == 0) {
filename = "binary";
} else {
filename = (String) opt[0];
}
final byte[] disposition = Joiner.on("; ")
.join(
"Content-Disposition: form-data",
String.format("name=\"%s\"", name),
"filename=\"binary\""
String.format("filename=\"%s\"", filename)
).getBytes(BaseRequest.CHARSET);
final byte[] type = "Content-Type: application/octet-stream"
.getBytes(BaseRequest.CHARSET);
Expand Down Expand Up @@ -773,7 +780,8 @@ public RequestBody set(final byte[] txt) {
}

@Override
public RequestBody formParam(final String name, final Object value) {
public RequestBody formParam(final String name, final Object value,
final Object... opt) {
try {
final StringBuilder builder = new StringBuilder(this.get());
if (!builder.toString().isEmpty()) {
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/jcabi/http/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ void sendsMultipartPostRequestMatchTwoParams(
)
)
.multipartBody()
.formParam("d", value)
.formParam("e", other)
.formParam("d", value, "d.txt")
.formParam("e", other, "e.txt")
.back()
.fetch().as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_OK);
Expand All @@ -365,12 +365,12 @@ void sendsMultipartPostRequestMatchTwoParams(
Matchers.is(
Joiner.on(Constants.CRLF).join(
separator,
"Content-Disposition: form-data; name=\"d\"; filename=\"binary\"",
"Content-Disposition: form-data; name=\"d\"; filename=\"d.txt\"",
RequestTest.steamContentType(),
"",
"value of € one param \"&^%*;'\"",
separator,
"Content-Disposition: form-data; name=\"e\"; filename=\"binary\"",
"Content-Disposition: form-data; name=\"e\"; filename=\"e.txt\"",
RequestTest.steamContentType(),
"",
"value of € two param \"&^%*;'\"",
Expand Down

0 comments on commit 1944dc4

Please sign in to comment.