Mock Server 之 moco-runner 使用指南二
文章出处http://blog.csdn.net/crisschan/article/details/53335234
moco-runner 安装配置
1、 下载jar
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/moco-runner-0.11.0-standalone.jar
2、 编译运行
- 配置java环境变量
- 安装并配置Gradle(ref:http://www.gradle.org)
- 然后获取源代码:https://github.com/dreamhead/moco
- 进入代码目录,
./gradle build 撰写json
[ { "response" : { "text" : "Hello, Moco" } } ]
然后写好json后就可以启动了
java -jar moco-runner-<version>-standalone.jar start -p 12306 -c foo.json
然后就可以通过http://localhost:12306访问了
3、进阶
3.1 Content
根据request内容返回response
[{
"request" :
{
"text" : "foo"
},
"response" :
{
"text" : "bar"
}
}]
如果request内容太多,可以放到一个文件里面
{
"request" :
{
"file" : "foo.request"
},
"response" :
{
"text" : "bar"
}
}
3.2 配置文件
PS:Moco支持动态加载配置文件,所以无论你是修改还是添加配置文件都是不需要重启服务的
Moco支持在全局的配置文件中引入其他配置文件,这样就可以分服务定义配置文件
例如你有两个项目Boy和Girl项目需要使用同一个Mock Server,那么可以分别定义boy.json和girl.json配置文件,然后在全局文件中引入即可: 全局配置如下:
[ { "context": "/boy", "include": "boy.json" }, { "context": "/girl", "include": "girl.json" } ]
在boy.json和girl.json中分别定义:
//boy
[ { "request" : { "uri" : "/hello" }, "response" : { "text" : "I am a boy." } } ]
//girl
[ { "request" : { "uri" : "/hello" }, "response" : { "text" : "I am a girl." } } ]
此时需要通过参数-g在加载全局配置文件(-g仅仅在3.1context章节使用)
java -jar moco-runner-<version>-standalone.jar start -p 12306 -g onecoder.json
启动成功后,我们分别通过http://localhost:12306/girl/hello 和 http://localhost:12306/boy/hello 访问服务,便可得到对应的reponse结果。 其实全局文件的引入方式还有直接include等,不过OneCoder觉得context这种方式应该比较常用,
Request参数
request里自然有很多带参数的,配置如下:
[{ "request" : { "uri" : "/getBoy", "queries": { "name":"onecoder" } }, "response" : { "text" : "Hey." } }]
上述配置匹配的url即为:http://localhost:12306/getBoy?name=onecoder,返回值即为: Hey. 也就是说,使用这种方式你需要在开发期有固定的测试参数和参数值
对于rest风格的url,Moco支持正则匹配。
[{ "request": { "uri": { "match": "/searchboy/\\w+" } }, "response": { "text": "Find a boy." } }]
此时,访问http://localhost:12306/searchboy/* 结尾加任何参数均可匹配到。
除了Get外,Post,Put,Delete等请求模式自然是支持的:
[{
"request" :
{
"method" : "post",
"forms" :
{
"name" : "onecoder"
}
},
"response" :
{
"text" : "Hi."
}}]
对于Header、Cookies等请求信息的配置也是支持的。
3.3 template
从0.8版本开始,Moco提供了template功能,可以动态的返回一些参数值。例如:
[
{
"request": {
"uri": "/template"
},
"response": {
"text": {
"template": "${req.queries['name']}"
}
}
}
]
此时通过url:http://localhost:12306/template?name=onecoder 访问,则会返回onecoder。 这样就可以通过template这种方式灵活的返回一些值。
3.4 redirect
[{ "request" : { "uri" : "/redirect" }, "redirectTo" : "http://www.coderli.com" }]
3.5 Asynchronous
[
"request":{
"uri":"/event"
},
"response":{
"text":"event"
},
"on":{
"complete":{
"async":"true",
"post":{
"url":"http://another_siter",
"content":"cintent"
}
}
}
]
这样,对于/event的访问将会是异步的。要等到对http://another_siter访问结束后,才会将结果放到response里
Mock Server 之 moco-runner 使用指南二的更多相关文章
- 接口测试 mock server 工具moco学习笔记
看过乙醇分享的接口测试,自己练习了moco,这里呢,吧一些练习的笔记坐下记录,方便自己查阅. 开源地址https://github.com/dreamhead/moco , 到QuickStart ...
- 接口测试 mock server 工具moco
看过乙醇分享的接口测试,自己练习了moco,这里呢,吧一些练习的笔记坐下记录,方便自己查阅. 开源地址https://github.com/dreamhead/moco , 到QuickStart ...
- Mock Server利器 - Moco
Moco介绍Moco独立运行所需环境如何运行Moco启动http服务启动https服务Moco HTTPs API配置如何在配置文件添加注释约定请求Body约定接口的uri约定请求参数约定请求方法约定 ...
- 怎样使用Mock Server
转载自:http://www.cnblogs.com/111testing/p/6091460.html 怎样使用Mock Server 一,去这里https://github.com/dream ...
- Mock Server文章链接
Mock Server文章链接 2017-06-14 1 Dreamhead (Zheng Ye) Moco可以提供以下服务: HTTP APIs Socket APIs REST API GitHu ...
- 基于moco的mock server 简单应用 来玩玩吧
提起mock大家应该就知道是干嘛用的了,再次再介绍一种简单的方式,基于moco的mock server.步骤很简单: 1. 首先,要下载个moco的jar0_1482402640757_moco-ru ...
- mock server 实现get方法的接口(二)
mock server 实现get方法的接口(二) 下面是实现查询品牌的接口demo: 1.当response数据量小的时候,可以直接使用json, mock会自动设置headers为applicat ...
- Mock Server 之 moco-runner 使用指南一
文章出处http://ju.outofmemory.cn/entry/96866 用以下命令可以启动moco-runner 服务 java -jar moco-runner-<version&g ...
- mock server搭建及接口测试简单实例
一.搭建mock server mock工具很多,这里我们选择用简单易操作的moco服务器 使用前必须先安装java,去相关网站下载Standalone Moco Runner 二.使用mock se ...
随机推荐
- Unity5.X 新版AssetBundle打包控制
一.什么是AssetBundle 估计很多人只知道Unity的模型之类的东西可以导出成一种叫做AssetBundle的文件,然后打包后可以在Unity程序运行的时候再加载出来用.那么AssetBund ...
- 【BZOJ】1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛(lis)
http://www.lydsy.com/JudgeOnline/problem.php?id=1669 水题太严重 #include <cstdio> #include <cstr ...
- Objective-C实用类和协议
Objective-C实用类和协议 目录 概述 NSObject 概述 NSObject 协议<NSObject> 类NSObject 详细方法参考文档 实用操作 是否为某个类或其子类 是 ...
- Android Fragment Base
public class FragmentTabsActivity extends FragmentActivity implements OnClickListener { //定义Fragment ...
- layerui如何设置显示的位置?
转自:http://www.layui.com/doc/modules/layer.html#offset layer.open({ type: 1,//默认:0 (0表示信息框,1表示页面层,2表示 ...
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- Spring MVC静态资源访问
最近在学习servlet的时候发现自己不能访问到css和js, 于是google一番学到不少方法加载,总结如下: 1.对于Spring MVC, 由于我们截获了所有请求<url-pattern& ...
- java发展历程、常用dos命令与jDK工具使用
Java菜鸟学习之旅 1.勤敲代码 2.必须将课堂上讲的内容学到位 2.1 学到会用 2.2 在学会之前不要自作主张 2.3 当天课程当天完成 3.能说会道 3.1 善于表达 3.2 学会的都能讲 4 ...
- sersync+rsync原理及部署
标签:sersync+rsync部署文档 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liubao0312.blog.51ct ...
- linux使用http代理连接服务器设置方法
连接腾讯的额cvm服务器官方给出的也有个方法,详细可以看这里:http://wiki.open.qq.com/wiki/%E4%BB%8E%E6%9C%AC%E5%9C%B0linux%E6%9C%B ...