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学习的更多相关文章

  1. 接口测试 mock server 工具moco学习笔记

    看过乙醇分享的接口测试,自己练习了moco,这里呢,吧一些练习的笔记坐下记录,方便自己查阅. 开源地址https://github.com/dreamhead/moco  ,  到QuickStart ...

  2. 【从零开始学BPM,Day1】工作流管理平台架构学习

    [课程主题] 主题:5天,一起从零开始学习BPM [课程形式] 1.为期5天的短任务学习 2.每天观看一个视频,视频学习时间自由安排. [第一天课程] Step 1 软件下载:H3 BPM10.0全开 ...

  3. 在WisOne平台上学习TypeScript

    TypeScript是微软公司推出的开源的类型化脚本语言,目的是用于为弱类型的javaScript提供强类型的识别和感知功能,同时它提供了类.接口.继承等相关在javaScript中不容易实现的功能, ...

  4. 测试开发系列之Python开发mock接口(一)

    什么是mock接口呢,举个栗子,你在一家电商公司,有查看商品.购物.支付.发 货.收获等等等一大堆功能,你是一个测试人员,测测测,测到支付功能的时候,你就要调用第三方支付接口了,真实支付,直接扣你支付 ...

  5. 基于Wiremock创建Mock Service平台

    转载:http://blog.csdn.net/liuchunming033/article/details/52399397 1.Wiremock工具介绍 一般开发项目都会分模块进行,比如都会把前端 ...

  6. 测试开发系列之Python开发mock接口(二)

    上一篇咱们已经把开发前的环境准备好了,还需要再做一点准备,你的账户信息是存在哪的呢,当然是存在数据库里的,咱们在去支付,扣钱的时候,肯定是从数据库里面操作的,去更新账户表里面的数据,所以咱们先要把数据 ...

  7. Android接口和框架学习

    Android接口和框架学习 缩写: HAL:HardwareAbstraction Layer.硬件抽象层 CTS:CompatibilityTest Suite,兼容性測试套件 Android让你 ...

  8. 基于Wiremock创建Mock Service平台(转)

    本文链接:https://blog.csdn.net/liuchunming033/article/details/52399397                                   ...

  9. 前后端分离&接口API设计学习报告

    接口API设计学习报告 15331023 陈康怡 什么是API? API即Application Programming Interface.API是一种通道,负责一个程序与另一个程序的沟通.而对于w ...

随机推荐

  1. Using RequireJS in AngularJS Applications

    http://www.sitepoint.com/using-requirejs-AngularJS-applications/ While writing large JavaScript appl ...

  2. java-随机生成用户名(中文版及英文版)

    开发中遇到用户名随机生成的问题,总结了两个(中文版和英文版),相关方法在此,方便直接调用. 如下: //自动生成名字(中文) public static String getRandomJianHan ...

  3. 如何将代码提交到git上

    http://blog.csdn.net/laozitianxia/article/details/50682100 这个博客介绍的很详细.

  4. UML语言中五大视图和九种图形纵览

    UML语言纵览 视图 UML语言中的视图大致分为如下5种: 1.用例视图.用例视图强调从系统的外部参与者(主要是用户)的角度看到的或需要的系统功能. 2.逻辑视图.逻辑视图从系统的静态结构和动态行为角 ...

  5. IDEA安装教程

    1.下载安装程序A,链接:https://pan.baidu.com/s/1IAsGDbApfyNsHuS7_m0rdw 密码:fthp 2.下载一个配置程序B,下载安装之后,暂时不用管,之后会用到. ...

  6. 云计算一:VMware workstation的安装和使用教程

    VMware workstation的安装和使用教程 一.VMware 安装 1.从网上找到VMware的安装包以及要安装的映像文件,下载到本地,然后备份一份存储到百度云盘. 链接:http://pa ...

  7. 【转】利用 three.js 开发微信小游戏的尝试

    前言 这是一次利用 three.js 开发微信小游戏的尝试,并不能算作是教程,只能算是一篇笔记吧. 微信 WeChat 6.6.1 开始引入了微信小游戏,初期上线了一批质量相当不错的小游戏.我在查阅各 ...

  8. Android开发学习总结(三)——appcompat_v7项目说明

    一.appcompat_v7项目说明 今天来说一下appcompat_v7项目的问题,使用eclipse创建Android项目时,发现project列表中会多创建出一个appcompat_v7项目,这 ...

  9. Python初级教程

    Python语言的特点 优点: - 简单 - 易学 - 免费,开源 - 高层语言 - 可移植性(可再多平台运行) - 解释性(不需要编译,可直接运行) - 面向对象 - 可扩展性(缺点:运行效率相对较 ...

  10. 『网络の转载』px与em的区别

    这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章,题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用p ...