一、moco框架的介绍
mock用来模拟接口,这里mock用的是moco框架,moco框架是github上的一个开源项目,可模拟http,https,Socket协议。
moco 是一个搭建模拟服务器的工具,其支持 API 和独立运行两种方式,前者通常是在 junit 等测试框架中使用,后者则是通过运行一个 jar 包开启服务。以下实用的是后者
moco框架的简单搭建:https://blog.csdn.net/gx0904/article/details/88220495
二、moco的入门使用
1. moco jar包下载
下载地址: http://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.10.0/
下载的jar包:moco-runner-0.10.0-standalone.jar
2、在需要的进行测试的项目中创建一个project,将下载下来的jar包直接拷进文件夹
3、建立一个json文件(图例中json文件名为startup1),输入以下代码,保存
以下代码可以复制
[
{
"description":"这是第一个moco例子",
"request":{
"uri":"/demo"
},
"response":{
"text":"这是一个moco框架demo"
}
}
]
4、启用命令行(windows键+R键:输入cmd回车),进入json文件夹所在目录
图例中的进入方式
然后输入以下命令,运行
java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8899 -c
startup1.json
-Dfile.encoding=UTF-8:字符集
-p 8888:访问的端口号(8888)
-c startup1.json:访问的字符集
三、moco常用请求
1. get请求(带参和不带参)
不带参请求:
[
{
"description":"不带参数的get请求",
"request":{
"uri":"/withGetDemo",
"method":"get"
},
"response":{
"text":"这是不带参数的get请求"
}
}
]
访问地址:http://localhost:8899/withGetDemo
带参请求:
[
{
"description":"带参数的get请求,p1,p2分别的参数1,参数2,名称可随便起,个数也可随便加",
"request":{
"uri":"/wihtGetDemobyParam",
"method":"get",
"queries":{
"p1":"hh",
"p2":"good"
}
},
"response":{
"text":"这是带参数的get请求"
}
}
]
访问地址::http://localhost:8899/wihtGetDemobyParam?p1=hh&p2=good
2. post请求(带参和不带参)
post请求不可以直接访问,在这里用到了工具:postman
无参请求:
[
{
"description":"post 请求",
"request":{
"uri":"/postDemo",
"method":"post"
},
"response":{
"text":"This is post request"
}
}
]
带参请求(参数格式:json、form表单)
from表单格式:
[
{
"description":"带参数的post请求",
"request":{
"uri":"/postDemoWithParam",
"method":"post",
"forms":{
"param1":"one",
"param2":"two"
}
},
"response":{
"text":"this is post request with param"
}
}
]
访问地址:http://localhost:8899/postDemoWithParam
json格式
[
{
"description":"带json格式参数的post请求",
"request":{
"uri":"/postDemoWithJson",
"method":"post",
"json":{
"param1":"one",
"param2":"two"
}
},
"response":{
"status":"200",
"json":{
"name":"success",
"status":"1"
}
}
}
]
访问地址:http://localhost:8899/postDemoWithJson
3. 带cookies的请求(get & post)
注:因为在访问时,用到了cookies验证,所有需要打开 postman interceptor在这我用的的是谷歌浏览的postman插件,postman interceptor也是谷歌的插件,也需要下载
带有cookies的get(带参)请求
[
{
"description":"这是一个带cookies信息的get请求",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
},
"queries":{
"p1":"hh",
"p2":"good"
}
},
"response":{
"text":"这是一个需要携带cookies信息的get请求"
}
}
]
访问地址:http://localhost:8899/get/with/cookies
带有cookies的post(有参)请求
[
{
"description":"这是一个带cookies信息的post请求",
"request": {
"uri": "/post/with/cookies",
"method": "post",
"cookies": {
"login": "true"
},
"json": {
"name": "huhansan",
"age": "18"
}
},
"response":{
"status":200,
"json":{
"huhansan":"success",
"status":"1"
}
}
}
]
请求地址:http://localhost:8899/post/with/cookies
4. 带请求头(headers)的请求(post)
psot请求(有参,参数格式:json)
[
{
"description":"这是一个带header信息的post请求",
"request":{
"uri":"/post/with/headers",
"method":"post",
"headers":{
"content-type":"application/json"
},
"json":{
"name":"wanglaosi",
"sex":"woman"
}
},
"response":{
"json":{
"wanglaosi":"success",
"status":"1"
}
}
}
]
访问地址:http://localhost:8899/post/with/headers
5. 重定向(页面跳转)
[
{
"description":"重定向到百度",
"request":{
"uri":"/redirect"
},
"redirectTo":"http://www.baidu.com"
},
{
"description":"重定向自己的网页上",
"request":{
"uri":"/redirect/topath"
},
"redirectTo":"/redirect/new"
},
{
"description":"这是被重定向的请求",
"request":{
"uri":"/redirect/new"
},
"response":{
"text":"重定向成功啦。。"
}
}
]
第一个访问 http://localhost:8888/redirect 跳转到百度,
第二个访问 http://localhost:8888/redirect/topath 返回 重定向成功啦。。就代表重定向成功。