当前位置:网站首页>Moco of Mock tools use tutorial
Moco of Mock tools use tutorial
2022-08-03 01:42:00 【Programmer Uncle Yang】
目录
一、什么是Moco
在开发过程中,经常会使用到一些http网络接口,而这部分功能通常是由第三方开发团队或者是后端同事进行开发的,在我们开发时不能给我们提供服务,这为我们的联调和测试造成了麻烦,这个时候就需要使用到mock测试.
Moco是一个简单搭建模拟服务器的程序库/工具:Moco会根据一些配置,启动一个真正的HTTP服务(会监听本地的某个端口).当发起请求满足一个条件时,它就给回复一个应答.
二、安装&配置
Jar包下载:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/moco-runner-0.11.0-standalone.jar
Moco文档:https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md
启动命令:
java -jar ./moco-runner-0.11.1-standalone.jar http -p 9090 -c all.json
-p 就是port 端口号, -c The following is the configuration interface contentjson文件.
启动成功效果:

三、接口配置&测试
3.1 first simple request:
all.json文件中增加以下request&response内容
[
{
"description": "这是第一个demo",
"request": {
"uri":"/demo"
},
"response": {
"text":"这是第一个demo",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
注意:mocoSimulated request returns Chinese garbled problem,Add response header encoding format asgbk,Can solve the results returned Chinese garbled
"headers":{
"Content-Type":"text/html;charset=gbk"
}
Test interface request result:

3.2 Get请求:
{
"description": "这是一个带参数的Get请求的Demo",
"request": {
"uri": "/getWithParam",
"method": "get",
"queries": {
"name": "张三",
"sex": "20"
}
},
"response": {
"text": "我张三会来了",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
Test interface request result:http://localhost:9090/getWithParam?name=张三&sex=20

3.3 Post请求:
{
"description": "这是一个带参数的post请求",
"request": {
"uri": "/postWithParam",
"method": "post",
"forms": {
"name": "张三",
"sex": "man"
}
},
"response": {
"text": "我张三带着参数回来了",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
Test interface request result:

注意:Input parameter with Chinese value,如果换成x-www-form-urlencoded格式(postDefault format for requests),则会请求失败:


这是Moco目前的问题,没有解决,English entry is no problem:

3.4 返回值为Json格式的请求:
{
"description": "电话号码注册post请求",
"request": {
"uri": "/postWithPhone",
"method": "post",
"forms": {
"type": "1"
}
},
"response": {
"json":{
"code": "0000",
"msg": "请求成功",
"data": {
"userId": 111111,
"phone": "12011111111",
"cardNo": "8000000000"
}
}
}
}
Test interface request result:

3.5 带cookie信息才能访问的post请求:
{
"description": "这是一个需要带有cookie信息才能访问的post请求",
"request": {
"method": "post",
"uri": "/postWithCookie",
"cookies": {
"login":"true"
},
"json": {
"name": "zhangsan",
"age": "18"
}
},
"response": {
"status":200,
"json": {
"name":"张三",
"status": "1"
}
}
}
Test interface request result:


3.6 重定向请求:
{
"description": "重定向到百度",
"request": {
"uri":"/redirect"
},
"redirectTo":"http://www.baidu.com"
}
Test interface request result:

3.7 前后端联调mock实战:
- mocoAfter running locally,后端server地址: 127.0.0.1:9090
- Called by front-end page operation requestmockinterface test:

四、搭建Mocoserver for others to call
Since the local can be used,Then run on the server,more people(包括研发)使用,provide a simplemock服务.下载Mocothe package to the server and unpack it,然后运行:

访问Moco服务:http://服务器IP:9090/postDemo
服务器上mock返回日志:

注意点:
- As mentioned earlier, the local runtime,If the returned content has Chinese,Need to add a paragraphheader指定格式为gbk才能正常显示中文.Tests found running on the server,mock返回内容Json文件中,Instead, remove this paragraph,Returns the content to show Chinese properly,If there is this paragraph, it will display garbled characters in Chinese:
"headers":{
"Content-Type":"text/html;charset=gbk"
}
- If the server wants tomock服务一直运行,使用命令:
nohup java -jar ./moco-runner-0.11.1-standalone.jar http -p 9090 -c all.json > /dev/null 2> /dev/null &
If just in frontnohup,会报错:
nohup: ignoring input and appending output to ‘nohup.out’
原因是nohupExecution produces a log,默认是写到nohup.out文件中,文件没有写入权限.Need to cooperate with the following command > /dev/null 2> /dev/null & 将 nohup 的日志输出到 /dev/null,Similar to the directorylinuxof a black hole,will make all messages to it disappear automatically.
If you want to keep logs,可以新建一个文件,比如moco.log, 然后nohupWhen the specified log is written tomoco.log中:
nohup java -jar ./moco-runner-0.11.1-standalone.jar http -p 9090 -c all.json >moco.log &
The log information is recorded as follows:

=====================================================================================
以上就是本次的全部内容,如果对你有帮助,欢迎关注我的微信公众号:程序员杨叔,各类文章都会第一时间在上面发布,持续分享全栈测试知识干货,你的支持就是作者更新最大的动力~
边栏推荐
猜你喜欢

嵌入式分享合集26

精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队

What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?

B站回应HR称用户是Loser:涉事面试官去年底已被劝退

1 - vector R language self-study

CTF命令执行题目解题思路
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting

vant-swipe adaptive picture height + picture preview

记一次mysql查询慢的优化历程

Rebound shell principle and implementation
随机推荐
d合并json
MySQL最大建议行数2000w, 靠谱吗?
Rebound shell principle and implementation
WebShell 木马免杀过WAF
C语言函数详解(2)【函数参数——实际参数(实参)&形式参数(形参)】
DownMusic summary record
js基础知识整理之 —— 字符串
十年架构五年生活-03作为技术组长的困扰
CAS:474922-22-0,DSPE-PEG-MAL,磷脂-聚乙二醇-马来酰亚胺科研试剂供应
pytest-常用运行参数
Jmeter二次开发实现rsa加密
openssl源码下载
Directing a non-relational database introduction and deployment
Week 7 CNN Architectures - LeNet-5、AlexNet、VGGNet、GoogLeNet、ResNet
Test | ali internship 90 days in life: from the perspective of interns, talk about personal growth
IDO代币预售合约系统开发技术详细
程序员如何优雅地解决线上问题?
服务间歇性停顿问题优化|得物技术
同一份数据,Redis为什么要存两次?
数字化转型巨浪拍岸,成长型企业如何“渡河”?