Moco是一个搭建模拟服务器的工具
1、下载地址:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/
例如:我下载的是moco-runner-0.12.0-standalone.jar
2、新建一个文件夹moco,将上述jar包放入文件夹中
3、在moco中新建一个json文件,例如:config.json
4、在文件中写入以下内容(这是一个不带参数的get请求):
[
{
"request":{
"uri":"/getMethod"
},
"response":{
"text":"Hello World!"
}
}
]
5、cd命令进入moco文件,输入命令:java -jar moco-runner-0.12.0-standalone.jar start -p 8888 -c config.json 运行
6、访问http://localhost:8888/getMethod,查看结果
注意:moco是热部署的,即:修改配置文件后,服务器自动重启
带参数的get请求:
{
"description":"这是一个带参数的get请求",
"request":{
"uri":"/getwithparam",
"method":"get",
"queries":{
"name":"ztt",
"age":"18"
}
},
"response":{
"text":"ztt come back"
}
}
以下post请求不能直接在浏览器中访问,只能通过类似postman或者Jmeter等工具来模拟发送请求,或者使用httpClient编写代码
不带参数的post请求:
{
"description":"这是不带参数的post请求",
"request":{
"uri":"/post",
"method":"post"
},
"response":{
"text":"这是一个不带参数的post请求"
}
}
带参数的post请求:
{
"description":"这是一个带参数的post请求",
"request":{
"uri":"/postwithparam",
"forms":{
"name":"ztt",
"age":"18"
}
},
"response":{
"text":"ztt come back with param"
}
}
带cookie的get请求:
{
"description":"这是一个带cookies的get请求",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"这是一个带cookies的get请求"
}
}
带cookie的post请求:
{
"description":"这是一个带cookie的post请求",
"request":{
"uri":"/post/with/cookies",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"name":"ztt",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"ztt":"success",
"status":"1"
}
}
}
带headers的post请求:
{
"description":"这是一个带headers的post请求",
"request":{
"uri":"/post/with/headers",
"method":"post",
"headers":{
"content-type":"application/json"
},
"json":{
"name":"ztt",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"ztt":"success",
"status":"11111"
}
}
}
重定向:
{
"description":"重定向到百度",
"request":{
"uri":"/redirect"
},
"redirectTo":"http://www.baidu.com"
}
重定向到自己的页面:
{
"description":"重定向到自己的页面",
"request":{
"uri":"/redirectTo"
},
"redirectTo":"/redirectTo/new"
},
{
"description":"这是被重定向到的自己的页面",
"request":{
"uri":"/redirectTo/new"
},
"response":{
"text":"redirect success"
}
}
moco一个返回cookie的请求:
{
"description":"这是一个会返回cookies信息的get请求",
"request":{
"uri":"/getcookies",
"method":"get"
},
"response":{
"cookies":{
"login":"true",
"status":"10000"
},
"text":"恭喜你获得cookies成功"
}
}