1.概述
Moco可以通過簡單的配置request和response 對象,達到模擬后端返回的效果;
- 支持HTTP、HTTPS、socket協議;
- 支持在request中設置Headers、cookies、statusCode;
- 支持get、post、put、delete請求;
- 無需環境配置,只需要java運行環境即可;
2.下載安裝
moco安裝使用只需要下載moco jar包,即可使用;
moco jar包下載地址:
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/1.1.0/
下載moco-runner-1.1.0-standalone.jar文件即可;
3.moco使用
1)新建json配置文件(test.json),用於配置請求和響應內容,可根據需求自定義配置,內容如下:
[ { "request":{ "uri":"/demo" }, "response":{ "text":"The first moco script!" } } ]
2)啟動moco服務
在moco服務器cmd窗口,進入moco jar包所在目錄,執行命令:
java –jar moco-runner-1.1.0-standalone.jar http –p 3001 –c test.json
http:指定http協議
-p:指定啟動服務的端口號
-c:指定啟動服務使用的json文件
成功啟動界面如下:
3)接口請求調用
通過瀏覽器或者通過接口調用工具(postman、jmeter等)輸入接口請求地址,http://172.16.85.61:3001/demo
調用結果如下:
4.Json配置文件
1)不帶參數請求
[ { "description":"moco test01", "request":{ "uri":"/moco/test01" }, "response":{ "text":"The first moco script!" } } ]
- description:腳本備注,不執行;
- uri:腳本請求路徑
- response:響應內容
執行請求:
http://172.16.85.61:3001/moco/test01
執行結果:
注:請求路徑或響應內容修改,不需要重新啟動moco,重新訪問請求地址即可生效;
2)響應報文添加頭文件headers
[ { "description":"moco test01", "request":{ "uri":"/moco/test01" }, "response":{ "headers":{ "Content-Type":"text/html;charset=utf-8" }, "text":"The first moco script!moco第一個腳本。" } } ]
執行請求:
http://172.16.85.61:3001/moco/test01
執行結果:
3)多個請求多個響應
[ { "description":"moco test03", "request":{ "uri":"/moco/test01" }, "response":{ "headers":{ "Content-Type":"text/html;charset=utf8" }, "text":"The first moco script!moco 第一個腳本。" } }, { "description":"moco test03", "request":{ "uri":"/moco/test02" }, "response":{ "headers":{ "Content-Type":"text/html;charset=utf8" }, "text":"The first moco script!moco第2個腳本。" } } ]
注:多個請求和響應時間通過逗號隔開;
執行請求:
http://172.16.85.61:3001/moco/test02
執行結果:
4)帶參數請求
[ { "description":"moco test03", "request":{ "method":"get", "uri":"/moco/test01", "queries":{ "UserName":"wzl", "Password":"123456" } }, "response":{ "headers":{ "Content-Type":"text/html;charset=utf8" }, "text":"Login success" } } ]
- method:請求方法;
- queries:請求參數;
執行請求:
http://172.16.85.61:3001/moco/test01?UserName=wzl&Password=123456
執行結果:
5)響應結果讀取外部文件
[ { "description":"moco test03", "request":{ "method":"get", "uri":"/moco/test01", "queries":{ "UserName":"wzl", "Password":"123456" } }, "response":{ "headers":{ "Content-Type":"text/html;charset=utf8" }, "file":"success_result.json" } } ]
file:響應結果讀取外部文件success_result.json,文件內容如下:
{ "status":"200", "msg":"Login Success", "data":{ "UserName":"wzl", "Password":"123456" } }
執行請求:
http://172.16.85.61:3001/moco/test01?UserName=wzl&Password=123456
執行結果: