OPEN API平台测试之Restful服务接入
上一篇 / 下一篇 2008-12-19 20:46:41 / 置顶(1)
原始需求:平台在新的版本要支持对标准Rest风格的接口的接入,对用户提交的delete,post,put,update请求要正确的转发。而且请求的url格式要转换成isp能接收的格式,即资源占位符格式。
需求理解:在本次的测试中隐含了两个大的需求点。如下:
A.在新的需求中需要测试占位符是否能正确的转换,即客户发起http://sipserver?param1=AAA¶m2=BBB¶m3=CCC,能否路由到
http://ispserver/AAA/BBB?param3=CCC
或者http://ispserver/AAA?param2=BBB¶m3=CCC的请求
B.需要测试delete,post,put,update,get的操作符能否正确的被传递。即用户通过sip向isp发起一个delete请求,那么是否能得到正确的isp响应。
测试模型如下图
图一
由上图可见如果要验证A的需求,首先需要有Rest风格的服务。由于目前网上已经有很丰富的OPEN API如YAHOO的API和google的API都是标准的Rest风格的接口。所以在实战中选择了Yahoo的接口来测试SIP平台的路由功能。
如果要验证B的需求,那么想在网上找合适的接口来测试就比较麻烦,所以我们决定自己来搭建Rest风格的服务来进行测试。
本文将对上面两种方法逐一进行介绍。
方法一直接调用Yahoo接口(测试需求点A)
前置条件:注册yahoo接口到SIP平台上,暴露为alitest.ali-002-restyahoo
测试步骤:一、直接访问yahoo的接口,得到返回值A
二、通过sip访问服务alitest.ali-002-restyahoo,得到返回值B
三、验证A是否等于B
测试代码如下(代码中使用了httpunit,至于如何使用这个工具的工作原理,本文不做详细介绍):
publicclassTestRestful {
privateStringapi_server="http://10.2.226.19:8180/sip/rest";
staticStringmysql_url="jdbc:mysql://10.2.226.19:3306/sip";
@Test
publicvoidTestAccessYahooFromSip()throwsException
{
String url ="http://boss.yahooapis.com/ysearch/web/v1/car?appid=xq88cS3V34GQE7xg
WebConversation conversation =newWebConversation();
GetMethodWebRequest request =newGetMethodWebRequest(url);
WebResponse response = conversation.getResponse(request);
String ExpXmlText = response.getText();
System.out.println(ExpXmlText);
String sip_appkey ="test_app003";// app_id
String sip_apiname ="alitest.ali-002-restyahoo";
String sip_appsecret ="secret_app003";
String sessionid ="sddddeee2";
String side ="ysearch";
String type ="web";
String version ="v1";
String content ="car";
String appid ="xq88cS3V34GQE7xg
String format ="xml";
String sip_thirdpart ="true";
DeleteHisRec(sip_apiname);
util.SIPHelp.Crush_Sip();
Map<String, String> map =newHashMap<String, String>();
map.put("side", side);
map.put("type", type);
map.put("version", version);
map.put("content", content);
map.put("appid", appid);
map.put("format", format);
map.put("sip_thirdpart",sip_thirdpart);
map.put("sip_httpmethod","get");
url = util.SIPHelp.prepareUrl(sip_appkey, sip_apiname, sip_appsecret,api_server, sessionid, map);
//发起请求
//System.out.println(url);
request =newGetMethodWebRequest(url);
response = conversation.getResponse(request);
String ActXmlText = response.getText();
//XMLAssert.assertXMLEqual("对比XML结果", ExpXmlText, ActXmlText);
String path1 ="/ysearchresponse/resultset_web/result/title";
XMLAssert.assertXpathValuesEqual(path1,ExpXmlText,path1,ActXmlText);
System.out.println(ActXmlText);
}
}
方法二通过自己构建的Restful服务来进行平台透传行为(get,delete,put,post){需求点B测试}
前置条件:搭建Restful服务。 本文只是简单介绍一下搭建过程。
1. 安装ruby、rails、mysql。安装mysql是rails的特性,它要依照数据库来创建资源。
2. 创建空的rails web应用骨架
rails mybook
3. 创建用于访问的资源,rails将自动完成各种操作的map(路由或映射)
ruby scrīpt/generate scaffold book name:string isbn:string
这个命令的意思是创建一个资源(可以认为是一个数据结构)
在这里是书,它包含书名和刊物号两个字段。
在做完1,2,3的步骤之后我们会发现在mybook\config\routes.rb的代码中多了map.resources :books
这说明在该服务中books已经是标明是一种资源,而且系统自动为他创建了new,delete,edit等服务
TAG: