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" “