Skip to content

Commit

Permalink
Merge pull request #35 from autotraderuk/multipart-text-params
Browse files Browse the repository at this point in the history
Added multipart body to be simple text parameters
  • Loading branch information
shaunstorey authored Feb 23, 2024
2 parents da639d0 + 77bbc84 commit 4a87f93
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public String getContentType() {
public static class BodyPart {
private final String name;
private final byte[] data;
private final String value;
private final InputStream inputStream;
private final String contentType;
private final String filename;
Expand All @@ -59,6 +60,7 @@ public BodyPart(String name, byte[] data, String contentType, String filename) {
this.contentType = contentType;
this.filename = filename;
this.inputStream = null;
this.value = null;
}

/**
Expand All @@ -74,6 +76,21 @@ public BodyPart(String name, InputStream inputStream, String contentType, String
this.contentType = contentType;
this.filename = filename;
this.data = null;
this.value = null;
}

/**
* Constructs a BodyPart
* @param name see {@link BodyPart#getName()}
* @param value see {@link BodyPart#getValue()} ()}
*/
public BodyPart(String name, String value) {
this.name = name;
this.value = value;
this.data = null;
this.inputStream = null;
this.contentType = null;
this.filename = null;
}

/**
Expand Down Expand Up @@ -102,14 +119,20 @@ public InputStream getInputStream() {
return inputStream;
}

/**
* @return the value for this body part
*/
public String getValue() {
return value;
}

/**
* @return the content type for this given body part
*/
public String getContentType() {
return contentType;
}


/**
* This can be useful to describe the original filename this body part was generated from
* @return an optional file name for this body type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,30 @@ void init_GivenValues_SetsProperties() {
assertThat(multipart.getContentType()).isEqualTo("contentType");
assertThat(multipart.getFilename()).isEqualTo("filename");
assertThat(multipart.getInputStream()).isNull();
assertThat(multipart.getValue()).isNull();
}

@Test
void init_GivenValuesWithInputStream_SetsProperties() throws IOException {
SimpleMultipartBody.BodyPart multipart = new SimpleMultipartBody.BodyPart("name", new ByteArrayInputStream("data".getBytes(StandardCharsets.UTF_8)), "contentType", "filename");

assertThat(multipart.getName()).isEqualTo("name");
assertThat(multipart.getData()).isNull();
assertThat(multipart.getInputStream().readAllBytes()).isEqualTo("data".getBytes(StandardCharsets.UTF_8));
assertThat(multipart.getContentType()).isEqualTo("contentType");
assertThat(multipart.getFilename()).isEqualTo("filename");
assertThat(multipart.getInputStream().readAllBytes()).isEqualTo("data".getBytes(StandardCharsets.UTF_8));
assertThat(multipart.getData()).isNull();
assertThat(multipart.getValue()).isNull();
}

@Test
void init_GivenValuesWithValue_SetsProperties() {
SimpleMultipartBody.BodyPart multipart = new SimpleMultipartBody.BodyPart("name", "value");

assertThat(multipart.getName()).isEqualTo("name");
assertThat(multipart.getValue()).isEqualTo("value");
assertThat(multipart.getContentType()).isNull();
assertThat(multipart.getFilename()).isNull();
assertThat(multipart.getData()).isNull();
assertThat(multipart.getInputStream()).isNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public HttpEntity toEntity(Body body) {

if (bodyPart.getData() != null) {
multipartEntityBuilder.addBinaryBody(bodyPart.getName(), bodyPart.getData(), ContentType.create(bodyPart.getContentType()), bodyPart.getFilename());
} else if (bodyPart.getValue() != null) {
multipartEntityBuilder.addTextBody(bodyPart.getName(), bodyPart.getValue());
} else {
multipartEntityBuilder.addBinaryBody(bodyPart.getName(), bodyPart.getInputStream(), ContentType.create(bodyPart.getContentType()), bodyPart.getFilename());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ void requestBody_MultipartBodyIsSerializedAndPostedCorrectly() {
assertThat(response.getStatusCode()).isEqualTo(202);
}

@Test
void requestBody_MultipartBodyWithStringKeyValueIsSerializedAndPostedCorrectly() {

wireMockServer.stubFor(post("/records")
.withMultipartRequestBody(aMultipart()
.withName("key")
.withBody(equalTo("value"))
)
.willReturn(WireMock.status(202)));
SimpleMultipartBody.BodyPart bodyPart = new SimpleMultipartBody.BodyPart("key", "value");
SimpleMultipartBody multipartBody = new SimpleMultipartBody(bodyPart);
Response<JSONObject> response = traverson.from("http://localhost:8089/records")
.post(multipartBody);

wireMockServer.verify(1, postRequestedFor(urlEqualTo("/records")));
assertThat(response.getStatusCode()).isEqualTo(202);
}

@Test
void requestBody_MultipartBodyFromInputStreamIsSerializedAndPostedCorrectly() {
byte[] data = new byte[]{0x00, 0x01, 0x02};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ void toEntity_GivenTextBodyFromInputStream_ReturnsStringEntity() throws Exceptio
.matches(Pattern.compile(".*Content-Type: contenttype.*", Pattern.DOTALL | Pattern.MULTILINE))
.matches(Pattern.compile(".*data.*", Pattern.DOTALL | Pattern.MULTILINE));
}

@Test
void toEntity_GivenKeyValueBody_ReturnsStringEntity() throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

HttpEntity entity = converter.toEntity(new SimpleMultipartBody(new SimpleMultipartBody.BodyPart("key", "value")));

assertThat(entity.getContentType()).matches(Pattern.compile("multipart/form-data; charset=ISO-8859-1; boundary=.*"));
entity.writeTo(outputStream);
String content = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
assertThat(content)
.matches(Pattern.compile(".*name=\"key\".*", Pattern.DOTALL | Pattern.MULTILINE))
.matches(Pattern.compile(".*value.*", Pattern.DOTALL | Pattern.MULTILINE));
}
}

0 comments on commit 4a87f93

Please sign in to comment.