Spring提供的test jar(本文用的是org.springframework.test-3.0.1.RELEASE.jar)包里有一个MockMultipartHttpServletRequest类,可以用来帮助测试通过HTTP request上传文件的类,通过MockMultipartHttpServletRequest.addFile(MockMultipartFile)方法将文件'上传'到request里去,不幸的是这个类却不能工作,Spring承诺在3.1中解决.
本文通过使用Spring提供的另一个类MockHttpServletRequest,通过将文件的内容'拼'成符合上传文件时http的请求内容,加的request的contents里去达到上传文件的目的.
测试方法:
@Test
public void testPerform() {
String bondary = "XX12181285033829";//http内容里各个参数间的分割标志,可以是任意字符串
String endline = EnvConstant.NEWLINE_DELIMETER;//"/r/n";//换行符
MockHttpServletRequest request = ContentsUtil.getDefaultMockedRequest(bondary);
String filePath = "testdata/request/CopyBill_1.csv";
//ContentsUtil.getHttpFileContents会返回符合文件上传时候的http内容
String httpFilePart = ContentsUtil.getHttpFileContents(bondary, endline, "file", "CopyBill_1.csv", "text/plain", filePath);
request.setContent(httpFilePart.getBytes());
//加入参数
request.setParameter("bm_cb_dtCategory", "Copy Bill requestuest");
request.setParameter("bm_cb_SRID", "SR0001");
System.out.println(httpFilePart.toString());
ActionCopyBill ActionCopyBill = new ActionCopyBill();
MockHttpServletResponse response = new MockHttpServletResponse();
HttpSession session = request.getSession(true);//如果被测试的方法要用到session值,需要如下填充
session.setAttribute(WebKeys.LOGIN_USERID, "1");
ActionCopyBill.perform(request, response);
String ritContents = ContentsUtil.getContentsFromFile("testdata/rep/CopyBill_1.rsp");
String respContents = ContentsUtil.getContentsFromOutputStream(response);
Assert.assertEquals(respContents, ritContents);
}
用到的帮助类:getHttpFileContents方法将文件'拼'成http请求内容.
public class ContentsUtil {
public static InputStream readFromdefaultClspath(String fileName) {
InputStream stream = ClassLoader.getSystemResourceAsStream(fileName);
return stream;
}
public static String getContentsFromFile(String fileName) {
InputStream stream = readFromdefaultClspath(fileName);
StringBuffer strbf = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str;
while ((str = in.readLine()) != null) {
strbf.append(str);
strbf.append(EnvConstant.NEWLINE_DELIMETER);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return strbf.toString();
}
public static String getContentsFromStream(InputStream stream) {
StringBuffer strbf = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str;
while ((str = in.readLine()) != null) {
strbf.append(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return strbf.toString();
}
public static String getContentsFromOutputStream(MockHttpServletResponse response) {
try {
return response.getContentAsString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static MockHttpServletRequest getDefaultMockedRequest(String bondary) {
MockHttpServletRequest req = new MockHttpServletRequest();
req.setCharacterEncoding("text/plain");
req.setMethod("POST");
req.setContentType("multipart/form-data; boundary=" + bondary);
req.addHeader("Content-type", "multipart/form-data");
return req;
}
public static String getHttpFileContents(String bondary, String newline, String formField,
String filename, String contentType, String filePath) {
String fileContents = ContentsUtil.getContentsFromFile(filePath);
StringBuilder sb = new StringBuilder();
sb.append(newline);
sb.append("--");
sb.append(bondary);
sb.append(newline);
sb.append("Content-Disposition: form-data; name=/"");
sb.append(formField);
sb.append("/"; filename=/"");
sb.append(filename);
sb.append(newline);
sb.append("Content-Type: ");
sb.append(contentType);
sb.append(newline);
sb.append(newline);
sb.append(fileContents);
sb.append("--");
sb.append(bondary);
sb.append("--");
sb.append(newline);
return sb.toString();
}
}
http内容里的文件部分:
--XX12181285033829
Content-Disposition: form-data; name="file"; filename="CopyBill_1.csv
Content-Type: text/plain
Account No,Invoice ID,Invoice Date,Thoroughfare Number,Thoroughfare,SubPremise,Premises Name,Post Town,Post Code
GP00000181,001,22/02/2009,118,COTSWOLD GARDENS,subPremise_004,premisesName_004,LONDON,NW2 1PN
--XX12181285033829--