Moco框架基本介紹
下載地址:
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/
可以下載jar包

Moco啟動(dòng)以及第一個(gè)demo
啟動(dòng)命令
java -jar ./moco-runner-0.11.0-standalone.jar 服務(wù)類型 -p 端口號 -c json配置文件
例如
java -jar ./moco-runner-0.11.0-standalone.jar http -p 8809 -c startup.json
第一個(gè)demo
1、創(chuàng)建startup.json文件
2、填入如下代碼塊,并啟動(dòng)moco
3、瀏覽器輸入本地網(wǎng)址127.0.0.1:8009/demo
4、這個(gè)是一個(gè)不帶參數(shù)的get方法
[
{
"description": "這是我們的第一個(gè)mock例子",
"request": {
"uri": "/demo"
},
"response": {
"text": "第一個(gè)基本框架說明"
}
}
]
注意:修改json文件不用重新部署,服務(wù)熱部署,會(huì)自動(dòng)重啟
不帶參數(shù)的get方法
[
{
"description": "模擬一個(gè)沒有參數(shù)的get請求",
"request": {
"uri": ",/getdemo",
"method": "get"
},
"response": {
"text": "這是一個(gè)沒有參數(shù)的GET請求"
}
}
]
帶參數(shù)的get方法
[
{
"description": "模擬一個(gè)沒有參數(shù)的get請求",
"request": {
"uri": "/getdemo",
"method": "get",
"queries": {
"name": "1",
"age": "2"
}
},
"response": {
"text": "這是一個(gè)有參數(shù)的GET請求"
}
}
]
不帶參數(shù)的post方法
[
{
"description": "模擬一個(gè)沒有參數(shù)的post請求",
"request": {
"uri": "/postdemo",
"method": "post"
},
"response": {
"text": "這是一個(gè)沒參數(shù)的post請求"
}
}
]
帶參數(shù)的post方法
[
{
"description": "模擬一個(gè)有參數(shù)的post請求",
"request": {
"uri": "/postdemo",
"method": "post",
"forms": {
"name": "1",
"age": "2"
}
},
"response": {
"text": "這是一個(gè)有參數(shù)的post請求"
}
}
]
請求中帶cookies信息的get請求
[
{
"description":"這是一個(gè)request請求中帶cookies信息的get請求",
"request":{
"uri":"/getwithcookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"返回這是一個(gè)request請求中帶cookies信息的get請求"
}
}
]
請求中帶cookies的post請求
[
{
"description":"這是一個(gè)request請求中帶cookies、使用json格式傳參、返回結(jié)果為json格式的post請求",
"request":{
"uri":"/postwithcookiesandjson",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"name":"qinzhenxia",
"age":"28"
}
},
"response":{
"status":200,
"json":{
"code":"0",
"msg":"success",
"p2pdata":{
"name":"moguzhixing",
"address":"beisanhuan"
}
}
}
}
]
response返回中帶有cookies信息的get請求
[
{
"description":"模擬response返回中帶有cookies信息的get請求",
"request":{
"uri":"/getcookies"
},
"response":{
"cookies":{
"login":"true",
"token":"1234567890"
},
"json":{
"name":"zhoujielun",
"age":"38"
}
}
}
]
有重定向的接口(redirectTo)
[
{
"description":"模擬重定向接口",
"request":{
"uri":"/redirectto"
},
"redirectTo":"/redirectedpath"
},
{
"description":"模擬被重定向的接口",
"request":{
"uri":"/redirectedpath"
},
"response":{
"text":"返回重定向結(jié)果",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]