moco框架的使用

匿名 (未验证) 提交于 2019-12-03 00:22:01

一、moco框架基本介绍

mock用来模拟接口,这里mock用的是moco框架,moco框架是github上的一个开源项目,可模拟http,https,Socket协议。moco有几种使用方法,这里介绍的是standolone用法,更多用法可参考https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md

Usage  You have several ways to use Moco. One is API, which you can use in your unit test. The other is that run Moco as standalone. Currently, you put all your configuration in JSON file.  On the other hand, Moco has several different ways to integrate with some tools: Maven plugin, Gradle plugin and shell support

二、moco的启动及demo

1、moco的启动

1)、到github上下载moco-runner-0.12.0-standalone.jar 包【这里下载了当前最新版本】

下载地址https://github.com/dreamhead/moco

2)、moco的启动

在命令行中执行如下命令:

2、demo

1)将jar包放在某个目录下。

2)json配置文件:demo.json

[   {     "description":"这是我们的第一个mock例子",     "request":{       "uri":"/demo"     },     "response":     {       "text":"Hello,Moco"     }   } ]

3)在命令行中执行


执行结果如下:说明启动成功了

2

4)在浏览器中输入http://127.0.0.1:8888/demo 进行访问,得到响应结果


三、常用请求

以下是json配置文件内容

1、get请求

[   {     "description":"不带参数的get请求",     "request":{       "uri":"/withGetDemo",       "method":"get"     },     "response":{       "text":"这是不带参数的get请求"     }   },   {     "description":"带参数的get请求,p1,p2分别的参数1,参数2,名称可随便起,个数也可随便加",     "request":{       "uri":"/wihtGetDemobyParam",       "method":"get",       "queries":{         "p1":"hh",         "p2":"good"       }     },     "response":{       "text":"this is a get method with paramter"     }   } ]

2、post请求

[   {     "description":"post 请求",     "request":{       "uri":"/postDemo",       "method":"post"     },     "response":{       "text":"This is post request"     }   },   {     "description":"带参数的post请求",     "request":{       "uri":"/postDemoWithParam",       "method":"post",       "forms":{         "param1":"one",         "param2":"two"       }     },     "response":{       "text":"this is post request with param"     }   } ]

3、post请求,(请求参数为json格式、请求带cookies)

[   {     "description":"这是一个带cookies的Post请求",     "request":{       "uri":"/postDemoWithCookies",       "cookies":{         "login":"true"       },       "json":{         "name":"hi",         "age":"3"       }     },     "response":{       "status":"200",       "json":{         "name":"success",         "status":"1"       }     }   } ]

4、请求带header

[   {     "description":"带header请求",     "request": {       "uri": "/withHeader",       "method": "post",       "headers": {         "content-type": "application/json"       },       "json": {         "name": "xiaoming",         "age": "18"       }     },       "response":{         "json":{           "message":"success",           "status":"1"         }       }     } ]

5、请求重定向

[   {     "description":"重定向到百度",     "request":{       "uri":"/redirect",       "method":"get"     },     "redirectTo":"http://www.baidu.com"   },   {     "description":"这是被重定向的请求",     "request":{       "uri":"/toRedirect"     },     "response":{       "text":"this is the redirect page"     }   },   {     "description":"重定向到自己的网页上",     "request":{       "uri":"/myStation"     },     "redirectTo":"/toRedirect"   } ]

文章来源: moco框架的使用
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!