Skip to content

Commit

Permalink
Merge pull request #68 from sashirestela/67-multipart-convert-collect…
Browse files Browse the repository at this point in the history
…ions-to-bytearray

Convert collections to bytearray in Multipart
  • Loading branch information
sashirestela authored Apr 7, 2024
2 parents 3a55d1f + 5314ad4 commit 39e2db8
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var newPost = postService.createPost(new Post(
System.out.println(newPost);
```

## 🛠️ Installation
## Installation

You can install CleverClient by adding the following dependency to your Maven project:

Expand Down
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>cleverclient</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
<packaging>jar</packaging>

<name>cleverclient</name>
Expand Down Expand Up @@ -52,19 +52,19 @@
<maven.compiler.release>11</maven.compiler.release>
<!-- Dependencies Versions -->
<slf4j.version>2.0.12</slf4j.version>
<lombok.version>1.18.30</lombok.version>
<jackson.version>2.16.1</jackson.version>
<lombok.version>1.18.32</lombok.version>
<jackson.version>2.17.0</jackson.version>
<junit.version>5.10.2</junit.version>
<mockito.version>5.10.0</mockito.version>
<mockito.version>5.11.0</mockito.version>
<!-- Plugins Versions -->
<compiler.version>3.12.1</compiler.version>
<compiler.version>3.13.0</compiler.version>
<enforcer.version>3.4.1</enforcer.version>
<surefire.version>3.2.5</surefire.version>
<exec.version>3.2.0</exec.version>
<jacoco.version>0.8.11</jacoco.version>
<jacoco.version>0.8.12</jacoco.version>
<helper.version>3.5.0</helper.version>
<versions.version>2.16.2</versions.version>
<source.version>3.3.0</source.version>
<source.version>3.3.1</source.version>
<javadoc.version>3.6.3</javadoc.version>
<gpg.version>3.1.0</gpg.version>
<sonatype.version>1.6.13</sonatype.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -49,15 +50,32 @@ public static List<byte[]> toByteArrays(Map<String, Object> data) {
byteArrays.add(toBytes(NL));
} else {
var fieldValue = entry.getValue();
byteArrays.add(toBytes(FIELD_NAME + DQ + fieldName + DQ + NL));
byteArrays.add(toBytes(NL));
byteArrays.add(toBytes(fieldValue + NL));
var isFirst = true;
if (fieldValue instanceof Collection) {
for (Object item : (Collection<?>) fieldValue) {
addIndividualField(byteArrays, fieldName + "[]", item, isFirst);
isFirst = false;
}
} else {
addIndividualField(byteArrays, fieldName, fieldValue, isFirst);
}
}
}
byteArrays.add(toBytes(DASH + Constant.BOUNDARY_VALUE + DASH + NL));
return byteArrays;
}

private static void addIndividualField(List<byte[]> byteArrays, String fieldName, Object fieldValue,
boolean isFirst) {
if (!isFirst) {
byteArrays.add(toBytes(DASH + Constant.BOUNDARY_VALUE + NL));
byteArrays.add(toBytes(DISPOSITION));
}
byteArrays.add(toBytes(FIELD_NAME + DQ + fieldName + DQ + NL));
byteArrays.add(toBytes(NL));
byteArrays.add(toBytes(fieldValue + NL));
}

private static byte[] toBytes(String text) {
return text.getBytes(StandardCharsets.UTF_8);
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/io/github/sashirestela/cleverclient/http/ITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,20 @@ static class Generic<T> {

}

@Getter
@AllArgsConstructor
static class MultipartClass {

private java.nio.file.Path file;

private int id;

private String text;

private List<String> group;

private Integer[] numbers;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.github.sashirestela.cleverclient.support;

import io.github.sashirestela.cleverclient.http.ITest;
import io.github.sashirestela.cleverclient.util.JsonUtil;
import org.junit.jupiter.api.Test;

import java.nio.file.Paths;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

class HttpMultipartTest {

@Test
void testToByteArrays() {
String[] expectedData = {
"Content-Disposition: form-data",
"Content-Type: text/plain\r\n",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"Content-Disposition: form-data",
"; name=\"id\"\r\n",
"101\r\n",
"Content-Disposition: form-data",
"; name=\"text\"\r\n",
"Testing\r\n",
"Content-Disposition: form-data",
"; name=\"group[]\"\r\n",
"one\r\n",
"Content-Disposition: form-data",
"; name=\"group[]\"\r\n",
"two\r\n",
"Content-Disposition: form-data",
"; name=\"numbers[]\"\r\n",
"13\r\n",
"Content-Disposition: form-data",
"; name=\"numbers[]\"\r\n",
"25\r\n",
"Content-Disposition: form-data",
"; name=\"numbers[]\"\r\n",
"37\r\n"
};
var object = new ITest.MultipartClass(Paths.get("src/test/resources/loremipsum.txt"), 101, "Testing",
List.of("one", "two"), new Integer[] { 13, 25, 37 });
var objectMap = JsonUtil.objectToMap(object);
var bytesList = HttpMultipart.toByteArrays(objectMap);
var i = 0;
for (byte[] bytes : bytesList) {
var currentData = new String(bytes);
if (!currentData.startsWith("--") && !currentData.startsWith("\r\n")
&& !currentData.contains("filename=")) {
assertEquals(expectedData[i], currentData);
i++;
}
}
}

}
1 change: 1 addition & 0 deletions src/test/resources/loremipsum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

0 comments on commit 39e2db8

Please sign in to comment.