About
mock除了用在單元測試過程中,還有一個用途,當前端開發在開發頁面的時候,需要服務端提供API接口。
此時服務端沒開發完成,或者說沒搭建測試環境,這個時候前端開發會自己mock一個api服務端,自己給自己提供調用接口的返回數據。
moco框架用途就是開發在開發的過程中,需要依賴一部分的接口,但是對方沒有提供或者環境等等情況。
moco框架支持API和獨立運行兩種方式,API的方式通常運行在Junit等java的測試框架中,而獨立的運行方式是通過jar包開啟服務,可以模擬HTTP,HTTPS,socket協議。這里介紹標准用法。
更多使用方法參考:https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md
另外,jar包的運行依賴於java虛擬機,這里我們還需要安裝java的JDK:
- java for windows:https://www.cnblogs.com/sundawei7/p/11956103.html
- java for Mac OS:https://www.cnblogs.com/sundawei7/p/11956203.html
- java for centos:https://www.cnblogs.com/sundawei7/p/11956295.html
jar包下載
- github官網:https://github.com/dreamhead/moco
- moco-runner:http://central.maven.org/maven2/com/github/dreamhead/moco-runner/1.0.0/moco-runner-1.0.0-standalone.jar
這個jar包只提供服務,我們還需要配置文件,配置我們自己定義的規則信息。
將配置文件和下載的jar放在同一目錄下。配置規則的文件,是json類型的文件,比如叫做configure.json
文件。記得,每次修改了配置文件,就要重新運行jar包。
M:\test\moco-runner\ ├─moco-runner-1.0.0-standalone.jar └─configure.json
如何運行:
M:\tests\moco-runner>java -jar moco-runner-0.12.0-standalone.jar start -p 8888 -c config.json
訪問:
http://127.0.0.1:8888/login
快速上手
再次強調:
- 配置文件修改后,就要重新運行jar包。
- 配置文件為json類型,對格式的要求比較高,要注意點!
開搞!
get請求
configure.json
:

[ { "description": "一個簡單的get請求", "request": { "method": "get", "uri": "/login" }, "response": { "text": "我是login get method", "headers":{ "Content-Type":"text/html;charset=gbk" } } }, { "description": "帶參數的get請求,p1和p2是兩個參數", "request": { "method": "get", "uri": "/reg", "queries": { "p1": "v1", "p2": "v2" } }, "response": { "text": "帶參數的get請求", "headers":{ "Content-Type":"text/html;charset=gbk" } } }, { "description": "get請求返回json類型數據", "request": { "method": "get", "uri": "/login_json" }, "response": { "json": { "key":"value", "請求方式是get":"相應結果為json類型" }, "headers": { "Content-Type": "application/json;charset=gbk" } } } ]
各個參數無需多言吧,簡單明了。在響應體加入headers
指定Content-Type
編碼格式為gbk是為了防止中文亂碼問題。
另外可以根據響應文本內容指定application/json
或者text/html
。
瀏覽器地址欄輸入:

http://127.0.0.1:8888/login http://127.0.0.1:8888/reg?p1=v1&p2=v2 http://127.0.0.1:8888/login_json
post請求configure.json
:

[ { "description": "post請求,請求參數為json格式,響應格式為json", "request": { "method": "post", "uri": "/post_json", "json": { "login_status": "successful" } }, "response": { "json": { "login": "ok" }, "headers": { "Content-Type": "application/json;charset=gbk" } } }, { "description": "post請求,請求及響應都為json,並且請求帶cookies", "request": { "method": "post", "uri": "/post_cookie", "json": { "login_status": "successful" }, "cookies":{ "user_id":"xsdaqawea" } }, "response": { "json": { "login": "ok" }, "headers": { "Content-Type": "application/json;charset=gbk" } } }, { "description": "post請求,請求及響應都為json,並且請求帶cookies和headers", "request": { "method": "post", "uri": "/post_cookie", "json": { "login_status": "successful" }, "cookies": { "user_id": "xsdaqawea" }, "headers":{ "Content-Type":"application/json" } }, "response": { "json": { "login": "ok" }, "headers": { "Content-Type": "application/json;charset=gbk" } } } ]
發送請求及結果:

import requests res1 = requests.post(url='http://127.0.0.1:8888/post_json', json={"login_status": "successful"}) print(res1.json()) # {'login': 'ok'} res2 = requests.post(url='http://127.0.0.1:8888/post_cookie', cookies={"user_id": "xsdaqawea"}, json={"login_status": "successful"}) print(res2.json()) # {'login': 'ok'}
請求重定向confiure.json
:

[ { "description":"重定向到指定網站", "request":{ "method":"get", "uri":"/login_redirect" }, "redirectTo":"https://www.baidu.com" } ]
發送請求及結果:
http://127.0.0.1:8888/login_json
see also:解決moco模擬請求返回中文亂碼問題 | moco框架的使用