2014-03-25 46 views
5

顯然我沒有使用這個測試夾具的權利。我的servlet在tomcat中工作得很好,但是當我嘗試使用這個模擬時,沒有找到多部分邊界。 「該請求被拒絕,因爲沒有找到多部分邊界」。如何使用spring的MockMultipartHttpServletRequest?獲得「沒有多邊界被發現」

有一個回答here顯示如何使用這個使用文本文件,但該答案明確設置邊界字符串,並嵌入文件作爲測試。我想我不需要用手工做像mockrequest.addFile(...)

什麼我不是在這裏設置或如何,我這樣做不對的方法呢?

@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 
+1

請張貼完整堆棧跟蹤。 –

+1

另外,閱讀'setParameter'的javadoc。 –

+1

另外,我們需要知道'MyServletTest#doPost()'實際上對請求做了什麼。 –

回答

10

您需要設置的邊界。

在這裏有什麼是邊界https://stackoverflow.com/a/10932533/2762092

解決你的問題試試這個代碼的好解釋。

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())); 
     } 
} 
+0

包含@butfly –

2

大答案薩穆埃爾,但一個錯誤:

 String end = "\r\n"+ boundary + "--"; 

應該是:

 String end = "--"+ boundary + "--"; 

感謝他的工作非常多。

+2

建議的修正其實應該是: String end =「 - 」+ boundary +「 - 」; – butfly

+0

感謝您的提示。 –

+0

@Samuel很高興爲您回覆,但請再次更新您的代碼以刪除尾部後綴字符串中的前導「\ r \ n」,否則它將被追加到零件的原始內容中,以便像整數形式的零件解析會導致異常。 – butfly

2

投票給塞繆爾。儘管花了一天的時間試圖讓它工作。問題是:

String end = "--" + boundary + "--"; 

應該是:

String end = "\r\n--" + boundary + "--"; 
相關問題