Skip to content

Commit

Permalink
feat: support passing filename to multipart form data output
Browse files Browse the repository at this point in the history
Signed-off-by: Ramon Boss <[email protected]>
  • Loading branch information
piu130 committed Feb 8, 2023
1 parent ded5c04 commit 65886aa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class MultipartOutputResource {

public static final String RESPONSE_NAME = "a name";
public static final String RESPONSE_FILENAME = "a filename";
public static final String RESPONSE_SURNAME = "a surname";
public static final Status RESPONSE_STATUS = Status.WORKING;
public static final List<String> RESPONSE_VALUES = List.of("one", "two");
Expand Down Expand Up @@ -58,6 +59,7 @@ public RestResponse<MultipartOutputResponse> restResponse() {
public RestResponse<MultipartFormDataOutput> withFormDataOutput() {
MultipartFormDataOutput form = new MultipartFormDataOutput();
form.addFormData("name", RESPONSE_NAME, MediaType.TEXT_PLAIN_TYPE);
form.addFormData("part-with-filename", RESPONSE_FILENAME, MediaType.TEXT_PLAIN_TYPE, "file.txt");
form.addFormData("custom-surname", RESPONSE_SURNAME, MediaType.TEXT_PLAIN_TYPE);
form.addFormData("custom-status", RESPONSE_STATUS, MediaType.TEXT_PLAIN_TYPE)
.getHeaders().putSingle("extra-header", "extra-value");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void testWithFormData() {

String body = extractable.asString();
assertContainsValue(body, "name", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_NAME);
assertContainsValue(body, "part-with-filename", MediaType.TEXT_PLAIN, "filename=\"file.txt\"");
assertContainsValue(body, "custom-surname", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_SURNAME);
assertContainsValue(body, "custom-status", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_STATUS);
assertContainsValue(body, "active", MediaType.TEXT_PLAIN, MultipartOutputResource.RESPONSE_ACTIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private String getFileNameIfFile(Object value, String partFileName) {
} else if (value instanceof FileDownload) {
return "; filename=\"" + ((FileDownload) value).fileName() + "\"";
} else if (partFileName != null) {
return partFileName;
return "; filename=\"" + partFileName + "\"";
}

return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public PartItem addFormData(String key, Object entity, MediaType mediaType) {

public PartItem addFormData(String key, Object entity, String genericType, MediaType mediaType) {
PartItem part = new PartItem(entity, genericType, mediaType);
return addFormData(key, part);
}

public PartItem addFormData(String key, Object entity, MediaType mediaType, String filename) {
PartItem part = new PartItem(entity, null, mediaType, filename);
return addFormData(key, part);
}

private PartItem addFormData(String key, PartItem part) {
parts.put(key, part);
return part;
}
Expand Down

0 comments on commit 65886aa

Please sign in to comment.