关闭

测试工具Wiremock介绍

发表于:2015-11-02 13:24

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Tang.Lang    来源:51Testing软件测试网采编

  Query参数匹配:http://localhost:9999/api/products?search=china
{
"request": {
"method": "GET",
"urlPath": "/api/products",
"queryParameters": {
"search": {
"contains": "chin"
}
}
},
"response": {
"status": 200,
"headers":{ "Content-Type": "application/json"},
"body": "{ \"id\": 7, \"name\": \"shan zai\", \"from\":\"China\" },{ \"id\": 7, \"name\": \"shan zai\", \"from\":\"China(RPC)\" }"
}
}
  返回文件内容:http://localhost:9999/file/1
  {
  "request": {
  "method": "GET",
  "url": "/file/1"
  },
  "response": {
  "status": 200,
  "bodyFileName": "test.xml"(或:”xmlfiles/test.xml”)
  }
  }
  (在__files文件夹下建好所调文件,路径为相对__files文件夹。)
  模拟错误
{
"request" : {
"url" : "/unknown.html",
"method" : "GET"
},
"response" : {
"status" : 404,
"headers" : {
"Content-Type" : "text/html; charset=utf-8"
}
}
}
{
"request": {
"method": "GET",
"url": "/fault"
},
"response": {
"fault": "MALFORMED_RESPONSE_CHUNK"
}
}
  录制HTTP请求及回放
  Wiremock的录制过程是启动一个代理服务,截取HTTP请求和响应,在mappings文件夹中创建一json文件记录下请求地址和响应概要,在__files文件夹下创建一文件包含响应内容;当重启Standalone进程时,那些记录下的请求响应就会作为Mock Service生成。
  启动录制服务:
  java -jar wiremock-1.57-standalone.jar --proxy-all="http://localhost:7777" --record-mappings –verbose
  默认的代理服务端口是8080,即之后发向http://localhost:7777的请求,可以用http://localhst:8080/来代理。
  生成mapping文件和响应内容文件:
{
"request" : {
"url" : "/test.aspx",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "body-test.aspx-WK0fD.json",
"headers" : {
"Cache-Control" : "private",
"Content-Type" : "text/html; charset=utf-8",
"Server" : "Microsoft-IIS/7.5",
"X-AspNet-Version" : "2.0.50727",
"X-Powered-By" : "ASP.NET",
"Date" : "Tue, 08 Sep 2015 03:14:36 GMT",
"Content-Length" : "61"
}
}
}
  录制完重启服务,验证刚才录制的请求是否生效:
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号