如何使用spring的MockMultipartHttpServletRequest?得到“没有发现多部分边界” [英] How to use spring's MockMultipartHttpServletRequest? Getting "no multipart boundary was found"
问题描述
显然我没有正确使用这个测试夹具。我的servlet在tomcat中运行得很好,但是当我尝试使用这个mock时,找不到多部分边界。 请求被拒绝,因为没有找到多部分边界。
Clearly I'm not using this test fixture right. My servlet works just fine in tomcat, but when I try to use this mock, the multi-part boundary is not found. "the request was rejected because no multipart boundary was found".
有答案 here ,显示如何使用文本文件来使用它,但该答案明确设置边界字符串并将文件嵌入为test。我认为我不需要手动处理像 mockrequest.addFile (...)
There is an answer here that shows how to use this using a text file, but that answer sets the boundary string explicitly and embeds the file as test. I would think I would not need to do with by hand with methods like mockrequest.addFile(...)
我不设置什么这里或我怎么做错了?
What am I not setting here or how I am doing this wrong?
@org.testng.annotations.Test
public void testDoPost() throws Exception
{
MockMultipartFile file = new MockMultipartFile("test.zip", "test.zip", "application/zip", MyServletTest.class.getResourceAsStream("/test.zip"));
MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest();
mockRequest.addFile(file);
mockRequest.set
mockRequest.setMethod("POST");
mockRequest.setParameter("variant", "php");
mockRequest.setParameter("os", "mac");
mockRequest.setParameter("version", "3.4");
MockHttpServletResponse response = new MockHttpServletResponse();
new MyServletTest().doPost(mockRequest, response);
// BOOM !
}
这是例外
Caused by: blablah: the request was rejected because no multipart boundary was found
推荐答案
你需要设置边界。
这里有一个很好的解释是什么是边界< a href =https://stackoverflow.com/a/10932533/2762092> https://stackoverflow.com/a/10932533/2762092
Here there is a good explanations about what is the boundary https://stackoverflow.com/a/10932533/2762092
要解决您的问题,请尝试此代码。
To solve your problem try this code.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.mock.web.MockMultipartHttpServletRequest;
public class FileUploadTest {
public void testDoPost() throws IOException {
Path path = Paths.get("c:\\temp\\test.zip");
byte[] data = Files.readAllBytes(path);
MockMultipartFile file = new MockMultipartFile("test.zip", "test.zip",
"application/zip", data);
MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest();
String boundary = "q1w2e3r4t5y6u7i8o9";
mockRequest.setContentType("multipart/form-data; boundary="+boundary);
mockRequest.setContent(createFileContent(data,boundary,"application/zip","test.zip"));
mockRequest.addFile(file);
mockRequest.setMethod("POST");
mockRequest.setParameter("variant", "php");
mockRequest.setParameter("os", "mac");
mockRequest.setParameter("version", "3.4");
MockHttpServletResponse response = new MockHttpServletResponse();
new FileUpload().doPost(mockRequest, response);
}
public byte[] createFileContent(byte[] data, String boundary, String contentType, String fileName){
String start = "--" + boundary + "\r\n Content-Disposition: form-data; name=\"file\"; filename=\""+fileName+"\"\r\n"
+ "Content-type: "+contentType+"\r\n\r\n";;
String end = "\r\n--" + boundary + "--"; // correction suggested @butfly
return ArrayUtils.addAll(start.getBytes(),ArrayUtils.addAll(data,end.getBytes()));
}
}
这篇关于如何使用spring的MockMultipartHttpServletRequest?得到“没有发现多部分边界”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!