Skip to content

Commit

Permalink
fix: Fix order of file upload multipart request (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsocha2 authored Jul 16, 2024
1 parent 8f7bdc0 commit 7200ac7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/box/sdk/AbstractBoxMultipartRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,14 @@ protected String bodyToString() {
protected void writeMethodWithBody(Request.Builder requestBuilder, ProgressListener progressListener) {
MultipartBody.Builder bodyBuilder =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
getPartName(),
filename,
getBody(progressListener)
);
.setType(MultipartBody.FORM);
// Attributes should go first
this.fields.forEach(bodyBuilder::addFormDataPart);
// File should go second
bodyBuilder.addFormDataPart(
getPartName(),
filename,
getBody(progressListener));
requestBuilder.post(bodyBuilder.build());
}

Expand Down
39 changes: 39 additions & 0 deletions src/test/java/com/box/sdk/BoxFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.EqualToJsonPattern;
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
import com.github.tomakehurst.wiremock.matching.MultipartValuePatternBuilder;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -1235,6 +1237,43 @@ public void testDeleteClassification() {
WireMock.verify(1, WireMock.deleteRequestedFor(WireMock.urlPathEqualTo(metadataURL)));
}

@Test
public void testUploadFileWithCorrectMultipartOrder() {
final String folderID = "0";
final String fileURL = "/2.0/files/content";
final String fileContent = "Test file";
final String fileName = "Test File.txt";
final String fileDescription = "Test Description";
InputStream stream = new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8));
final String expectedAttributesJson =
"{\"name\":\"Test File.txt\",\"parent\":{\"id\": \"0\"},\"description\":\"Test Description\"}";

String result = TestUtils.getFixture("BoxFile/CreateFileWithDescription201");

wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(fileURL))
.withRequestBody(WireMock.matching(".*attributes.*file.*")) // Check that attributes comes before file
.withMultipartRequestBody(
new MultipartValuePatternBuilder()
.withName("attributes")
.withBody(new EqualToJsonPattern(expectedAttributesJson, false, false))
).withMultipartRequestBody(
new MultipartValuePatternBuilder()
.withName("file")
.withBody(new EqualToPattern(fileContent))
)
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", APPLICATION_JSON_PATCH)
.withBody(result)
.withStatus(201)));

BoxFolder folder = new BoxFolder(this.api, folderID);
BoxFile.Info file = folder.uploadFile(stream, fileName, fileDescription);

assertEquals(fileDescription, file.getDescription());
assertEquals(folderID, file.getParent().getID());
assertEquals(fileName, file.getName());
}

@Test
public void testUploadFileWithDescriptionSucceeds() {
final String folderID = "12345";
Expand Down

0 comments on commit 7200ac7

Please sign in to comment.