Merge pull request #8315 from albanoj2/BAEL-3559

BAEL-3559: Added purpose-built endpoint for testing multipart uploads with HttpClient
This commit is contained in:
Eric Martin 2019-12-11 20:03:27 -06:00 committed by GitHub
commit 8d1acf29d5
2 changed files with 63 additions and 5 deletions

View File

@ -34,7 +34,7 @@ public class HttpClientMultipartLiveTest {
// No longer available
// private static final String SERVER = "http://echo.200please.com";
private static final String SERVER = "http://posttestserver.com/post.php";
private static final String SERVER = "http://localhost:8080/spring-mvc-java/stub/multipart";
private static final String TEXTFILENAME = "temp.txt";
private static final String IMAGEFILENAME = "image.jpg";
private static final String ZIPFILENAME = "zipFile.zip";
@ -84,7 +84,7 @@ public class HttpClientMultipartLiveTest {
//
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
builder.addPart("file", fileBody);
builder.addPart("text1", stringBody1);
builder.addPart("text2", stringBody2);
final HttpEntity entity = builder.build();
@ -112,7 +112,7 @@ public class HttpClientMultipartLiveTest {
final String message = "This is a multipart post";
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, TEXTFILENAME);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, TEXTFILENAME);
builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);
final HttpEntity entity = builder.build();
post.setEntity(entity);
@ -141,7 +141,7 @@ public class HttpClientMultipartLiveTest {
final String message = "This is a multipart post";
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, IMAGEFILENAME);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, IMAGEFILENAME);
builder.addBinaryBody("upstream", inputStream, ContentType.create("application/zip"), ZIPFILENAME);
builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build();
@ -165,7 +165,7 @@ public class HttpClientMultipartLiveTest {
final byte[] bytes = "binary code".getBytes();
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", bytes, ContentType.DEFAULT_BINARY, TEXTFILENAME);
builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, TEXTFILENAME);
builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build();
post.setEntity(entity);

View File

@ -0,0 +1,58 @@
package com.baeldung.web.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class MultipartFileUploadStubController {
@PostMapping("/stub/multipart")
public ResponseEntity<UploadResultResource> uploadFile(MultipartFile file, String text, String text1, String text2, MultipartFile upstream) {
UploadResultResource result = new UploadResultResource(file, text, text1, text2, upstream);
return new ResponseEntity<>(result, HttpStatus.OK);
}
public static class UploadResultResource {
private final String file;
private final String text;
private final String text1;
private final String text2;
private final String upstream;
public UploadResultResource(MultipartFile file, String text, String text1, String text2, MultipartFile upstream) {
this.file = format(file);
this.text = text;
this.text1 = text1;
this.text2 = text2;
this.upstream = format(upstream);
}
private static String format(MultipartFile file) {
return file == null ? null : file.getOriginalFilename() + " (size: " + file.getSize() + " bytes)";
}
public String getFile() {
return file;
}
public String getText() {
return text;
}
public String getText1() {
return text1;
}
public String getText2() {
return text2;
}
public String getUpstream() {
return upstream;
}
}
}