Skip to content

Commit

Permalink
allow consumers to set the value of the boundary param
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Feb 18, 2024
1 parent 48beed0 commit 1ffe66e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,20 @@ void defaultMediaTypes() {
});

}

@Test
void settingTheBoundary() {
String boundary = "ABC-123-BOUNDARY";

Unirest.post(MockServer.POST)
.field("spidey", rezFile("/spidey.pdf"))
.field("something", "else")
.boundary(boundary)
.asObject(RequestCapture.class)
.getBody()
.assertHeader("Content-Type", h -> {
h.assertParam("boundary", boundary);
});

}
}
4 changes: 4 additions & 0 deletions unirest/src/main/java/kong/unirest/core/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ default ProgressMonitor getMonitor(){
return null;
}

default String getBoundary() {
return null;
}

default BodyPart getField(String name){
return multiParts()
.stream()
Expand Down
15 changes: 15 additions & 0 deletions unirest/src/main/java/kong/unirest/core/HttpRequestMultiPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class HttpRequestMultiPart extends BaseRequest<MultipartBody> implements Multipa
private Charset charSet;
private boolean forceMulti = false;
private ProgressMonitor monitor;
private String boundary;

HttpRequestMultiPart(HttpRequestBody httpRequest) {
super(httpRequest);
Expand Down Expand Up @@ -136,6 +137,20 @@ public MultipartBody uploadMonitor(ProgressMonitor uploadMonitor) {
return this;
}

@Override
public MultipartBody boundary(String boundaryIdentifier) {
this.boundary = boundaryIdentifier;
return this;
}

@Override
public String getBoundary() {
if(boundary == null){
boundary = UUID.randomUUID().toString();
}
return boundary;
}

@Override
public Charset getCharset() {
return this.charSet;
Expand Down
8 changes: 8 additions & 0 deletions unirest/src/main/java/kong/unirest/core/MultipartBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,12 @@ public interface MultipartBody extends HttpRequest<MultipartBody>, Body {
* @return The same MultipartBody
* */
MultipartBody uploadMonitor(ProgressMonitor monitor);

/**
* Sets the value to use as the boundary identifier.
* see https://datatracker.ietf.org/doc/html/rfc2046
* @param boundaryIdentifier the value
* @return The same MultipartBody
*/
MultipartBody boundary(String boundaryIdentifier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private java.net.http.HttpRequest.BodyPublisher mapToMultipart(Body o) {
);
}

MultipartBodyPublisher.Builder builder = MultipartBodyPublisher.newBuilder();
var builder = MultipartBodyPublisher.newBuilder(o.getBoundary());
o.multiParts().forEach(part -> {
setMultiPart(o, builder, part);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Flow.Subscriber;

import static java.nio.charset.StandardCharsets.UTF_8;
Expand All @@ -56,12 +55,13 @@ final class MultipartBodyPublisher implements BodyPublisher {

private final List<Part> parts;
private final ProgressMonitor monitor;
private final String boundary = UUID.randomUUID().toString();
private final String boundary;
private long contentLength;

private MultipartBodyPublisher(List<Part> parts, ProgressMonitor monitor) {
private MultipartBodyPublisher(List<Part> parts, ProgressMonitor monitor, String boundary) {
this.parts = parts;
this.monitor = monitor;
this.boundary = boundary;
contentLength = UNINITIALIZED_LENGTH;
}

Expand Down Expand Up @@ -119,15 +119,17 @@ private static void appendHeader(StringBuilder target, String name, String value
}

/** Returns a new {@code MultipartBodyPublisher.Builder}. */
static MultipartBodyPublisher.Builder newBuilder() {
return new MultipartBodyPublisher.Builder();
static MultipartBodyPublisher.Builder newBuilder(String boundary) {
return new MultipartBodyPublisher.Builder(boundary);
}

static final class Builder {

private final List<Part> parts;
private final String boundary;

Builder() {
Builder(String boundary) {
this.boundary = boundary;
parts = new ArrayList<>();
}

Expand Down Expand Up @@ -220,7 +222,7 @@ MultipartBodyPublisher build(ProgressMonitor monitor) {
if (!!addedParts.isEmpty()) {
throw new UnirestException("at least one part should be added");
}
return new MultipartBodyPublisher(addedParts, monitor);
return new MultipartBodyPublisher(addedParts, monitor, boundary);
}
}
}

0 comments on commit 1ffe66e

Please sign in to comment.