接口测试mock server 工具moco

it2022-05-05  128

    看过乙醇分享的接口测试,自己练习了moco,这里呢,吧一些练习的笔记坐下记录,方便自己查阅。

  开源地址https://github.com/dreamhead/moco  ,  到QuickStart 进行下载。

下载就是一个jar包,

新建文件夹,然后看着官网的例子来做第一个接口

创建config.json,

[ { "request": { "method":"get", "uri":"/" }, "response": { "json":"huanyingni moco" } } ]

  然后cmd输入 

java -jar moco-runner-0.11.1-standalone.jar   http -p 12306 -c config.json

代表已经启动,那么我们在url访问

可以看到我们的server已经启动,我们看下命令行给我们的记录信息。

 

 其实我们还可以吧这个config文件的数据进行数据驱动

[ { "request": { "method":"get", "uri":"/get" }, "response": { "file":"allpost.json" } } ]

数据文件如下

[ { "title":'first1', 'url':'/post/1' }, { "title":'first2', 'url':'/post/2' }, { "title":'first3', 'url':'/post/3' } ]

  这样我们保存后,后台检查到变动,就会自动重新加载

访问网址后得到我们在数据驱动文件写入的数据。

其实我们还可以增加请求的参数如:

[ { "request": { "method":"get", "uri":"/get", "queries": { "id":"12306", "name":"moco" } }, "response": { "text":"moco get" } } ]

这样我们的访问必须增加参数才可以访问

还可以约定请求头必须加json格式

[ { "request": { "method":"post", "uri":"/post", "text": { "json":"{\"beijing\":\"2018\",\"shanghai\":\"mocoshanghai\"}" } }, "response": { "status":"200" } } ]

增加headers

[ { "request": { "method":"post", "uri":"/post", "text": { "json":"{\"beijing\":\"2018\",\"shanghai\":\"mocoshanghai\"}" }, "headers": { "content-type":"application/json", "Connection":"keep-alive", "Content-Encoding":"gzip" } }, "response": { "status":"300" } } ]

  返回文件,

[ { "request": { "method":"post", "uri":"/post" }, "response": { "file":"allpost.json", "version":"HTTP/1.0" } } ]

加入请求cookie: 

{ "request" : { "uri" : "/cookie", "cookies" : { "login" : "true" } }, "response" : { "text" : "success" } }

返回cookie

{ "request" : { "uri" : "/cookie" }, "response" : { "cookies" : { "login" : "true" } } }

  

重定向,

{ "request" : { "uri" : "/redirect" }, "redirectTo" : "http://www.github.com" }

  

https协议的 需要有https证书,其实已经给了说明,

启动命令

java -jar moco-runner-<version>-standalone.jar https -p 12306 -c foo.json --https /path/to/cert.jks --cert mocohttps --keystore mocohttps

需要增加你的证书

很多在github上都有了详细的讲解。看不懂英文吧例子摘抄下来,本地试试,你就知道这是怎么回事了。

 

转载于:https://www.cnblogs.com/leiziv5/p/7469492.html

相关资源:moco-runner-0.12.0-sources.jar

最新回复(0)