当前位置: 首页 > news >正文

石家庄市环保局网站建设项目备案系统日本网站源码

石家庄市环保局网站建设项目备案系统,日本网站源码,学校英文版网站建设方案,建站行业是什么意思本文通过实例来讲解如何通过HttpUnit来对web应用进行测试,尤其是当下Ajax越来越流行的情况下,http request和response交互频繁,里面传输的内容也以Json或者XML为主,用HttpUnit结合JUnit来做测试可以带来很多好处,甚至是在web页面还不存在的情况下,通过模拟http请求,包括模拟上传…

本文通过实例来讲解如何通过HttpUnit来对web应用进行测试,尤其是当下Ajax越来越流行的情况下,http request和response交互频繁,里面传输的内容也以Json或者XML为主,用HttpUnit结合JUnit来做测试可以带来很多好处,甚至是在web页面还不存在的情况下,通过模拟http请求,包括模拟上传文件,就可以用来测试服务端的servlet,action(有httprequest参数)等代码.

JAVA实例代码

HTTPStub :包装了HttpUnit提供的一些类,同时在初始化的时候做login验证,WebConversation会维护session的信息.

public class HTTPStub {

private WebConversation httpConversation;

private PostMethodWebRequest httpRequest;

public HTTPStub() {

httpConversation = new WebConversation();

String urlLogin = EnvConstant.SERVER_CTXT + EnvConstant.SERVER_LOGINURL;

GetMethodWebRequest getReq = new GetMethodWebRequest(urlLogin);

try {

httpConversation.getResponse(getReq);

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (SAXException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

public void initHttpRequest(String url) {

httpRequest = new PostMethodWebRequest(EnvConstant.SERVER_CTXT + url, true);

}

public void setParameter(String name, String value) {

httpRequest.setParameter(name, value);

}

public void setFile(String Filename) {

InputStream inputStream = FileUtil.readFromdefaultClspath(Filename);

httpRequest.selectFile("dumyfile", "dumyfile.csv", inputStream, "text/plain");

}

public WebResponse getHttpResponse() {

try {

return httpConversation.getResponse(httpRequest);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SAXException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

public String getHttpResponseContents() {

try {

WebResponse resp = httpConversation.getResponse(httpRequest);

StringBuffer strbf = new StringBuffer();

BufferedReader in = new BufferedReader(new InputStreamReader(resp.getInputStream()));

String str;

while ((str = in.readLine()) != null) {

strbf.append(str);

}

in.close();

return strbf.toString();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SAXException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

}

对inputstream处理的一个util类:

public class FileUtil {

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);

}

in.close();

} catch (IOException e) {

e.printStackTrace();

}

return strbf.toString();

}

}

Junit测试类:

public class ActionCopyBillTest {

private HTTPStub httpStub;

@Before

public void setUp() throws Exception {

httpStub = new HTTPStub();

}

@After

public void tearDown() throws Exception {

}

@Test

public void testPerform() {

httpStub.initHttpRequest("FrontController?command=CopyBill");

httpStub.setParameter("bm_cb_dtCategory", "Copy Bill Request");

httpStub.setParameter("bm_cb_SRID", "SR0001");

httpStub.setParameter("bm_cb_ItemOpt", "2- Custom Itemisation");

httpStub.setParameter("bm_cb_BillLanCode", "ENG");

httpStub.setParameter("bm_cb_LegendPrs", "Copy Legend");

httpStub.setParameter("bm_cb_BillStruct", "Front Page Only");

httpStub.setParameter("bm_cb_ItemThd", "1");

httpStub.setParameter("bm_cb_BillMedia", "Paper Bill");

httpStub.setParameter("bm_cb_BillFormat", "Blue Bill");

httpStub.setFile("testdata/req/CopyBill_1.csv");

String respContents = httpStub.getHttpResponseContents();

String ritContents = FileUtil.getContentsFromFile("testdata/rep/CopyBill_1.rsp");

Assert.assertEquals(respContents, ritContents);

}

}

如果想对response进行验证,可以通过手工从html页面输入数据,提交请求,用工具(如eclipse带有的插件tcp/ip monitor)将response截取下来保存为文件,然后和junit测试的时候的response对比.

另外,返回的response也提供了一系列方法来操作其包含的内容:

1,如返回的是文本,可以通过resp.getText()获取,如果文本是json格式,可以再进一步构造成jsonobject来操作.

String respContents = resp.getText();

JSONObject json = new JSONObject(respContents);

System.out.println(json.getInt("total"));

JSONArray arr = json.getJSONArray("userdata");

System.out.println(arr.get(0));

2,如果返回的是XML(标准结构的html也是合法的XML),可以得到w3c 的document对象,resp.getDOM();

3,如果返回的是html页面,WebResponse提供了一组类似于Javascript操作html dom的方法.

resp.getElementWithID(id)

resp.getTables();

...

http://www.hkea.cn/news/499477/

相关文章:

  • 动态网站代码设计做小程序的公司
  • 网站建设软件开发的新闻北京关键词优化报价
  • 在上海做兼职在哪个网站好百度售后电话人工服务
  • 深圳网站开发招聘谁能给我个网址
  • 长沙做个网站多少钱怎样免费给自己的公司做网站
  • wordpress to微博优化营商环境条例
  • 做外贸通常用哪些网站seo网站监测
  • 电子商务网站建设解决方案必应搜索引擎
  • 企业网页制作与网站设计南京seo优化培训
  • sqlite开发网站想做网络推广的公司
  • 网页设计作业在线网站首页seo教程seo优化
  • 做个网站多钱域名备案查询系统
  • 饰品网站模板官网seo关键词排名系统
  • 文学网站做编辑百度笔记排名优化
  • 公司网站开发语言如何优化百度seo排名
  • 做网站较好的框架惠州百度推广排名
  • 网站建设和运营的课程推广软文发稿
  • 杭州企业网站建设方案ui培训
  • 个人站长做哪些网站好seo优化设计
  • 小白学做搭建网站软文街官方网站
  • 网站模板 可做采集站市场营销咨询
  • 家居网站建设素材天眼查询个人信息
  • 杭州专业网站排名优化交换链接的例子
  • 网站建设和数据容量整合seo的培训课程
  • 深圳 网站制作 哪家百度搜索排名优化哪家好
  • 网站运营者网址发稿平台
  • 内蒙古网站制作公司拼多多网店代运营要多少费用
  • 免费网站建设协议baike seotl
  • 做网站的好处和坏处怎么创建自己的网址
  • 兰州新区城乡建设局网站seo sem是什么职位