moco-globalsettings
1.單個json文件
(1)目錄配置
(2)新建foo.json
在project文件夾中新建foo.json
[{"request" : {"uri" : "/foo"},"response" : {"text" : "foo"}} ](3)執行&訪問
windows在命令行cmd中:java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -c project/foo.json
瀏覽器中訪問:
http://localhost:12306/foo
2.Golbal-Settings We could put all configurations in one single configuration files. But if we want stub many services in a single Moco instance, the configurations file would be huge. In this case, we can use settings file to separate our configurations for different into different configugration files.
意思是,當一個moco實例要對外提供非常多的json文件時候,可以用include將所有json文件統一注冊
(1)準備json文件
bar.json[{"request" : {"uri" : "/foo"},"response" : {"text" : "foo"}} ] main.json
[{"include" : "project/foo.json"},{"include" : "project/bar.json"
}
]
注意文件的路徑,否則會拋找不到文件
(2)執行&訪問
windows在命令行cmd中:java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -g project/main.json
瀏覽器中訪問:
http://localhost:12306/foo http://localhost:12306/bar
?
3.Context
We can put all responses for one service in a specified context:
context.json[{"context": "/foo","include": "project/foo.json"},{"context": "/bar","include": "project/bar.json"} ] windows在命令行cmd中:
java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -g project/context.json
瀏覽器中訪問:
http://localhost:12306/foo/foo http://localhost:12306/bar/bar
?4.File Root
我們的目錄結構是json文件都放在project下,每次寫文件都要帶著路徑很麻煩,eg
{"include" : "project/bar.json" }而FileRoot功能可以讓我們不用再加project了,全局享受相對路徑。那么怎么做呢?
root.json[{"file_root": "project", #你的相對路徑文件夾"include": "too.json" #這邊開始就不需要加文件夾了} ] too.json
[{"request" : {"uri" : "/fileroot"},"response" : {"file" : "foo.json"}} ] windows在命令行cmd中:
java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -g project/root.json
瀏覽器中訪問:
http://localhost:12306/fileroot
?5.Environment
For some different cases, you have different configuration. For example, your code may access proxy for remote server and mock server for local testing.
意思大概是,提供給遠程使用的json和提供給本地使用的json可以區別對待
env.json [{"env" : "remote","file_root": "project","include": "foo.json"},{"env" : "local","file_root": "project","include": "bar.json"} ] windows在命令行cmd中:java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -g project/env.json -e remote
瀏覽器中訪問:
http://localhost:12306/foo 能夠返回數據 http://localhost:12306/bar 不能夠返回數據
若換成”-e local“ ,就會相反
6.Request
You may need a global request matcher in some cases, for example, token for some REST APIs, which actually is request parameter. Global request will help you.
大概的意思就是,請求頭中存在某特殊的東東,請求才會被接受。
修改evn.json
[{"request" : {"uri" : "/foo","headers" : {"foo" : "bar"}},"response" : {"text" : "foo", "headers" : {"foo" : "response"}}} ] windows在命令行cmd中:java -jar moco-runner-0.10.2-standalone.jar start -p 12306 -g project/env.json -e remote
瀏覽器中訪問:
http://localhost:12306/foo 不能夠返回數據
? 因為瀏覽器Get請求,header中沒有帶 foo:bar 這個東西,所以請求被丟棄。
? 那么如何構造header中帶”foo:bar“,讓請求順利獲取到response呢?我們用fiddler進行操作。
(1)下載fiddler,安裝
(2)fiddler中,命令行輸入 bpu ?127.0.0.1 進行斷點設置
? ? ? ?
(3)瀏覽器請求?http://127.0.0.1:12306/foo
?再fiddler中點擊抓到的請求,右側如圖
? ? ? ?
(4)右鍵cache - add header ,輸入 name:foo ?value:bar
? ? ? ?點擊break on response,再點擊Run to Completion 獲取結果
? ? ? ?
(5)看瀏覽器獲取結果集
? ? ? ? ps,fiddler中再次輸入bpu 回車,能夠取消斷點
?7.Response
? ? ? ?再6的基礎上,查看response會看到 我們設置的結果集即” ?"foo" : "response"?“
? ? ? ?
?
轉載于:https://www.cnblogs.com/lyt19871224/p/5113916.html
總結
以上是生活随笔為你收集整理的moco-globalsettings的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 页面的宽度
 - 下一篇: macbook excel导入html,