当前位置:网站首页>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:
=====================================================================================
以上就是本次的全部内容,如果对你有帮助,欢迎关注我的微信公众号:程序员杨叔,各类文章都会第一时间在上面发布,持续分享全栈测试知识干货,你的支持就是作者更新最大的动力~
边栏推荐
猜你喜欢
高数---二重积分
基于STM32设计的老人防摔倒报警设备(OneNet)
基于飞腾平台的嵌入式解决方案案例集 1.0 正式发布!
华为设备配置BFD与接口联动(触发与BFD联动的接口物理状态变为Down)
No-code development platform form styling steps introductory course
VMware workstation 程序启动慢
B站回应HR称用户是Loser:涉事面试官去年底已被劝退
程序员如何优雅地解决线上问题?
Directing a non-relational database introduction and deployment
markdown语法
随机推荐
嵌入式分享合集26
Swift中的类型相关内容
聚乙二醇衍生物4-Arm PEG-DSPE,四臂-聚乙二醇-磷脂
The CTF command execution subject their thinking
解决错误:Optional int parameter ‘pageSize‘ is present but cannot be translated into a null value due to
Database auditing - an essential part of network security
nmap: Bad CPU type in executable
最新真实软件测试面试题分享,收藏了还怕进入不了大厂?
VMware workstation program starts slowly
华为设备配置BFD与接口联动(触发与BFD联动的接口物理状态变为Down)
msys2下载地址
Apache Doris 1.1 特性揭秘:Flink 实时写入如何兼顾高吞吐和低延时
js基础知识整理之 —— 字符串
B站回应HR称用户是Loser:涉事面试官去年底已被劝退
【代码扫描修复】MD5加密弱HASH漏洞
D with json
无代码开发平台数据ID入门教程
No-code development platform form styling steps introductory course
APT level comprehensive free kill with Shell
十年架构五年生活-05第一次出差