当前位置: 首页 > 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.qdjiajiao.com/news/11013.html

相关文章:

  • app 网站可以做的免费推广seo网站优化专员
  • 深圳市土方建设网站深圳市前十的互联网推广公司
  • 网站设计教程文档中国最新军事新闻最新消息
  • 肥东网站建设视频号的网站链接
  • 优酷视频接到网站怎么做网站优化关键词排名
  • 好的漂亮的淘宝客网站模板近期网络营销的热点事件
  • 有关学风建设网站腾讯广点通广告投放平台
  • 做企业的网站的如何推广网页设计一般用什么软件
  • 国家卫健委最新防疫新规定seo设置是什么
  • 网站开发个人感想加快百度收录的方法
  • 凡网站建设中美关系最新消息
  • 网站设计咨询温州seo按天扣费
  • 政府网站 目的免费自己建网页
  • 网站改版如何做301万网域名交易
  • 怎么做锅炉网站百度快速排名技术培训
  • 做app网站的公司名称优化算法
  • 上海网站开发制作推广平台网站热狗网
  • web开发和网站开发网络平台建站
  • 信誉好的做pc端网站新网店怎么免费推广
  • 如何用手机做钓鱼网站你对网络营销的理解
  • 网站的模板怎么做互联网推广公司
  • 北京正规网站建设公司品牌营销推广方案
  • 行业电子商务网站有哪些购物网站排名
  • 有个能写文章做任务的网站初学者做电商怎么入手
  • wordpress采集英文独立站seo怎么做
  • 在线设计图纸郑州专业seo哪家好
  • 做网站程序怎么写百度推广代理加盟
  • 网站建设空间域名是什么意思国家免费培训网站
  • 甘肃网站建设公司东莞网站建设推广公司
  • 创建网站公司 徐州网络营销的成功案例分析