Mock接口平台Moco学习
Mock就是模拟接口的。本文学习Mock的 Moco开源框架。
Moco源码和jar下载地址: git jar 下载moco-runner-xxxx-standalone.jar
- moco的启动及第一个demo
Step1: 在项目中创建一个package:moco,并将下载的jar包放在该package下。
Step2:创建一个json文件,格式如下:
[
{
"description":"This is my first mock demo",
"request":{
"uri":"/demo"
},
"response":{
"text":"This is response"
}
}
]
Step3:cmd进入到该package下,运行命令:java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c startup1.json
在命令行中出现,则命令运行成功
29 Apr 2019 14:31:54 [main] INFO Server is started at 8888
29 Apr 2019 14:31:55 [main] INFO Shutdown port is 52901
Step4:打开浏览器,输入 localhost:8888.在浏览器上就可以看到我们在json文件中定义的数据。
- Moco框架的http协议get方法的Mock实现
1. 不带参数的get实现:
[
{
"description":"This is Get request without paramter",
"request":{
"uri":"/getdemo",
"method":"get"
},
"response":{
"text":"This is response for Get request without paramter "
}
}
]
2.带参数的get实现:
{
"description":"This is Get request with paramter",
"request":{
"uri":"/getwithparam",
"method":"get",
"queries":{
"name":"zhangsan",
"age":"18"
}
},
"response":{
"text":"This is response for Get request with paramter "
}
}
在浏览器中访问:http://localhost:8888/getwithparam?name=zhangsan&age=18 就可以返回定义的json数据。
- Moco框架的http协议Post方法的Mock实现
1.不带参数的post请求
[
{
"description":"This is Post request",
"request":{
"uri":"/postdemo",
"method":"post"
},
"response":{
"text":"This is Post response"
}
}
]
注意的是post请求不能直接在浏览器中访问,这个时候我们就需要借助jmeter postman等测试工具进行post请求的测试了。具体方法这里不演示。
2.带参数的post请求实现
{
"description":"This is Post request with paramter",
"request":{
"uri":"/postwithparam",
"method":"post",
"forms":{
"name":"zhangsan",
"age":"18"
}
},
"response":{
"text":"This is Post response with paramter"
}
}
- Moco框架如何加入Cookies
1.带cookies信息的get请求
{
"description":"This is Get request with cookies",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"This is get response with cookies"
}
}
2.带cookies信息的post请求
{
"description":"This is Post request with cookies",
"request":{
"uri":"/post/with/cookies",
"method":"post",
"cookies":{
"login":"true"
}
"json":{
"name":"zhangsan",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"zhangsan":"success",
"status":"1"
}
}
}
- Moco框架如何加入Header
Header请求头信息的格式在get和post请求中是一致的。
{
"description":"This is Post request with header",
"request":{
"uri":"/postwithheader",
"method":"post",
"headers":{
"content-type":"application/json"
},
"json":{
"name":"zhangsan",
"age":"18"
}
},
"response":{
"text":"This is Post response with paramter"
}
}
- Moco框架如何进行重定向
1.重定向到baidu
{
"description":"redirect to www.baidu.com",
"request":{
"uri":"/redirect"
},
"redirectTo":"http://www.baidu.com"
}
2.重定向到自己网站的某个地址
{
"description":"redirect to my path",
"request":{
"uri":"/redirect2"
},
"redirectTo":"redirected"
},
{
"description":"This is my path",
"request":{
"uri":"/redirected"
},
"response":{
"text":"redirect success!"
}
}
如果喜欢作者的文章,请关注"写代码的猿"订阅号以便第一时间获得最新内容。本文版权归作者所有,欢迎转载.

Mock接口平台Moco学习的更多相关文章
- 接口测试 mock server 工具moco学习笔记
看过乙醇分享的接口测试,自己练习了moco,这里呢,吧一些练习的笔记坐下记录,方便自己查阅. 开源地址https://github.com/dreamhead/moco , 到QuickStart ...
- 【从零开始学BPM,Day1】工作流管理平台架构学习
[课程主题] 主题:5天,一起从零开始学习BPM [课程形式] 1.为期5天的短任务学习 2.每天观看一个视频,视频学习时间自由安排. [第一天课程] Step 1 软件下载:H3 BPM10.0全开 ...
- 在WisOne平台上学习TypeScript
TypeScript是微软公司推出的开源的类型化脚本语言,目的是用于为弱类型的javaScript提供强类型的识别和感知功能,同时它提供了类.接口.继承等相关在javaScript中不容易实现的功能, ...
- 测试开发系列之Python开发mock接口(一)
什么是mock接口呢,举个栗子,你在一家电商公司,有查看商品.购物.支付.发 货.收获等等等一大堆功能,你是一个测试人员,测测测,测到支付功能的时候,你就要调用第三方支付接口了,真实支付,直接扣你支付 ...
- 基于Wiremock创建Mock Service平台
转载:http://blog.csdn.net/liuchunming033/article/details/52399397 1.Wiremock工具介绍 一般开发项目都会分模块进行,比如都会把前端 ...
- 测试开发系列之Python开发mock接口(二)
上一篇咱们已经把开发前的环境准备好了,还需要再做一点准备,你的账户信息是存在哪的呢,当然是存在数据库里的,咱们在去支付,扣钱的时候,肯定是从数据库里面操作的,去更新账户表里面的数据,所以咱们先要把数据 ...
- Android接口和框架学习
Android接口和框架学习 缩写: HAL:HardwareAbstraction Layer.硬件抽象层 CTS:CompatibilityTest Suite,兼容性測试套件 Android让你 ...
- 基于Wiremock创建Mock Service平台(转)
本文链接:https://blog.csdn.net/liuchunming033/article/details/52399397 ...
- 前后端分离&接口API设计学习报告
接口API设计学习报告 15331023 陈康怡 什么是API? API即Application Programming Interface.API是一种通道,负责一个程序与另一个程序的沟通.而对于w ...
随机推荐
- ZAB协议(Zookeeper atomic Broadcast)
一.简语: ZAB协议是Paxos算法的经典实现 二.ZAB协议的两种模式: 崩溃恢复: 1.每个server都有一张选票(myid,zxid),选票投给自己 2.收集所有server的投票 3.比较 ...
- Java 包装类笔记
@(JDK)[包装类] Java 包装类笔记 当需要使用到集合的时候,如果是基础数据类型,需要转换为包装类,再使用,在JDK1.5之前,每次使用的时候,都需要手动转换为包装类(称为装箱),然后在使用的 ...
- 关于loadrunner使用web_add_header添加HTTP信息头(比如Content-Type,token等)和使用
关于loadrunner使用web_add_header添加HTTP信息头(比如Content-Type,token等)和使用 1.web_add_header添加HTTP信息头(比如Content- ...
- python_code list_1
>>> def is_not_empty(s): return s and len(s.strip()) > 0 >>> filter(is_not_empt ...
- 学unity3d需要什么基础
学unity3d需要什么基础?在游戏业发展如火如荼的情境下,很多人开始转行投身于游戏程序开发,而unity3D游戏开发则是他们必须了解和会用的游戏开发工具.在学习之前又应该了解哪些内容呢? unity ...
- 使用 GNU profiler 来提高代码运行速度
各种软件对于性能的需求可能会有很大的区别,但是很多应用程序都有非常严格的性能需求,这一点并不奇怪.电影播放器就是一个很好的例子:如果一个电影播放器只能以所需要速度的 75% 来播放电影,那么它几乎就没 ...
- PAT1100:Mars Numbers
1100. Mars Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue People o ...
- String的valueOf()用于将其它类型转换为字符串
String的valueOf()重载方法可将double类型,int类型,boolean类型以及char数组类型等变量转换为String类变量. 注:String的valueOf()可将char数组转 ...
- Netty中的EventLoop和线程模型
一.前言 在学习了ChannelHandler和ChannelPipeline的有关细节后,接着学习Netty的EventLoop和线程模型. 二.EventLoop和线程模型 2.1. 线程模型 线 ...
- Hibernate中的多表查询及抓取策略
1.Hibernate中的多表查询 1.1SQL中的多表查询 [交叉连接] select * from A,B; [内连接] 显示内连接:inner join(inner 可以省略) Select * ...