用Spring提供的test jar包帮助测试用到HTTP request的类
来源:互联网 发布:联通网络解锁助手 编辑:程序博客网 时间:2024/05/01 19:59
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--
- 用Spring提供的test jar包帮助测试用到HTTP request的类
- 用Spring提供的test jar包帮助测试用到HTTP request的类
- Spring提供的测试帮助类
- spring对Test提供的测试基类
- 用spring +JNLP需要用到的jar
- hibernate要用到的jar包
- Spring 的 jar 包
- Spring的jar包
- Spring 集成测试1(对spring管理的bean进行单元测试,借助于spring提供的test case简化测试)
- spring mvc + mybatis +junit + log4j 用到的jar包整理与讨论。
- Spring中使用aop操作需要用到的aspectjweaver-1.8.7.jar包
- Spring AOP 测试需要POM添加的jar包
- 官方提供jar包的反编译
- spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m
- 如何在Eclipse中引入Spring的jar包并挂载API帮助文档
- mybatis+spring整合需要用到的包
- spring的jar包解析
- spring 的jar包详解
- SLF4J教程
- 好的习惯
- VS找不到MFC90d.dll错误
- log4j详解
- 互动网计算机频道图书7日销售排行(05.27-06.02)
- 用Spring提供的test jar包帮助测试用到HTTP request的类
- oUpFileStream.Write Request.BinaryRead (Request.TotalBytes)上传提示错误 0104
- Linux下c++编程与kdevelop的环境配置
- ext js 中combo对象的使用
- 假象工作目标.PHONY 的三种用法
- thickbox子页面表单提交数据后刷新父页面
- && 和 ||
- Flex 和 AIR 版本之间的不同
- 如何得到pdo设备的设备指针