0
点赞
收藏
分享

微信扫一扫

Moco框架

王远洋 2022-12-24 阅读 169

About

 

mock除了用在单元测试过程中,还有一个用途,当前端开发在开发页面的时候,需要服务端提供API接口。
此时服务端没开发完成,或者说没搭建测试环境,这个时候前端开发会自己mock一个api服务端,自己给自己提供调用接口的返回数据。
moco框架用途就是开发在开发的过程中,需要依赖一部分的接口,但是对方没有提供或者环境等等情况。
moco框架支持API和独立运行两种方式,API的方式通常运行在Junit等java的测试框架中,而独立的运行方式是通过jar包开启服务,可以模拟HTTP,HTTPS,socket协议。这里介绍标准用法。
更多使用方法参考:​​​https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md​​另外,jar包的运行依赖于java虚拟机,这里我们还需要安装java的JDK:

ar包下载

  • github官网:​​https://github.com/dreamhead/moco​​
  • moco-runner:​​http://central.maven.org/maven2/com/github/dreamhead/moco-runner/1.0.0/moco-runner-1.0.0-standalone.jar​​​这个jar包只提供服务,我们还需要配置文件,配置我们自己定义的规则信息。
    将配置文件和下载的jar放在同一目录下。配置规则的文件,是json类型的文件,比如叫做​​configure.json​​文件。记得,每次修改了配置文件,就要重新运行jar包。

M:\test\moco-runner\
├─moco-runner-1.0.0-standalone.jar
└─configure.json

如何运行:

M:\tests\moco-runner>java -jar moco-runner-0.12.0-standalone.jar start -p 8888 -c config.json

访问:

http://127.0.0.1:8888/login

快速上手

 

再次强调:

  • 配置文件修改后,就要重新运行jar包。
  • 配置文件为json类型,对格式的要求比较高,要注意点!

开搞!
get请求

​configure.json​​:

[
{
"description": "一个简单的get请求",
"request": {
"method": "get",
"uri": "/login"
},
"response": {
"text": "我是login get method",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
},
{
"description": "带参数的get请求,p1和p2是两个参数",
"request": {
"method": "get",
"uri": "/reg",
"queries": {
"p1": "v1",
"p2": "v2"
}
},
"response": {
"text": "带参数的get请求",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
},
{
"description": "get请求返回json类型数据",
"request": {
"method": "get",
"uri": "/login_json"
},
"response": {
"json": {
"key":"value",
"请求方式是get":"相应结果为json类型"
},
"headers": {
"Content-Type": "application/json;charset=gbk"
}
}
}
]

各个参数无需多言吧,简单明了。在响应体加入​​headers​​​指定​​Content-Type​​​编码格式为gbk是为了防止中文乱码问题。
另外可以根据响应文本内容指定​​​application/json​​​或者​​text/html​​。

浏览器地址栏输入:

http://127.0.0.1:8888/login
http://127.0.0.1:8888/reg?p1=v1&p2=v2
http://127.0.0.1:8888/login_json

post请求
​​​configure.json​​:

[
{
"description": "post请求,请求参数为json格式,响应格式为json",
"request": {
"method": "post",
"uri": "/post_json",
"json": {
"login_status": "successful"
}
},
"response": {
"json": {
"login": "ok"
},
"headers": {
"Content-Type": "application/json;charset=gbk"
}
}
},
{
"description": "post请求,请求及响应都为json,并且请求带cookies",
"request": {
"method": "post",
"uri": "/post_cookie",
"json": {
"login_status": "successful"
},
"cookies":{
"user_id":"xsdaqawea"
}
},
"response": {
"json": {
"login": "ok"
},
"headers": {
"Content-Type": "application/json;charset=gbk"
}
}
},
{
"description": "post请求,请求及响应都为json,并且请求带cookies和headers",
"request": {
"method": "post",
"uri": "/post_cookie",
"json": {
"login_status": "successful"
},
"cookies": {
"user_id": "xsdaqawea"
},
"headers":{
"Content-Type":"application/json"
}
},
"response": {
"json": {
"login": "ok"
},
"headers": {
"Content-Type": "application/json;charset=gbk"
}
}
}
]

发送请求及结果:

import requests

res1 = requests.post(url='http://127.0.0.1:8888/post_json', json={"login_status": "successful"})

print(res1.json()) # {'login': 'ok'}


res2 = requests.post(url='http://127.0.0.1:8888/post_cookie', cookies={"user_id": "xsdaqawea"}, json={"login_status": "successful"})

print(res2.json()) # {'login': 'ok'}

请求重定向
​​​confiure.json​​:

[
{
"description":"重定向到指定网站",
"request":{
"method":"get",
"uri":"/login_redirect"
},
"redirectTo":"https://www.baidu.com"
}
]

发送请求及结果:

http://127.0.0.1:8888/login_json




About

 

mock除了用在单元测试过程中,还有一个用途,当前端开发在开发页面的时候,需要服务端提供API接口。
此时服务端没开发完成,或者说没搭建测试环境,这个时候前端开发会自己mock一个api服务端,自己给自己提供调用接口的返回数据。
moco框架用途就是开发在开发的过程中,需要依赖一部分的接口,但是对方没有提供或者环境等等情况。
moco框架支持API和独立运行两种方式,API的方式通常运行在Junit等java的测试框架中,而独立的运行方式是通过jar包开启服务,可以模拟HTTP,HTTPS,socket协议。这里介绍标准用法。
更多使用方法参考:​​​https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md​​另外,jar包的运行依赖于java虚拟机,这里我们还需要安装java的JDK:

jar包下载

  • github官网:​​https://github.com/dreamhead/moco​​
  • moco-runner:​​http://central.maven.org/maven2/com/github/dreamhead/moco-runner/1.0.0/moco-runner-1.0.0-standalone.jar​​​这个jar包只提供服务,我们还需要配置文件,配置我们自己定义的规则信息。
    将配置文件和下载的jar放在同一目录下。配置规则的文件,是json类型的文件,比如叫做​​configure.json​​文件。记得,每次修改了配置文件,就要重新运行jar包。

M:\test\moco-runner\
├─moco-runner-1.0.0-standalone.jar
└─configure.json

如何运行:

M:\tests\moco-runner>java -jar moco-runner-0.12.0-standalone.jar start -p 8888 -c config.json

访问:

http://127.0.0.1:8888/login

快速上手

 

再次强调:

  • 配置文件修改后,就要重新运行jar包。
  • 配置文件为json类型,对格式的要求比较高,要注意点!

开搞!
get请求

​configure.json​​:

[
{
"description": "一个简单的get请求",
"request": {
"method": "get",
"uri": "/login"
},
"response": {
"text": "我是login get method",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
},
{
"description": "带参数的get请求,p1和p2是两个参数",
"request": {
"method": "get",
"uri": "/reg",
"queries": {
"p1": "v1",
"p2": "v2"
}
},
"response": {
"text": "带参数的get请求",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
},
{
"description": "get请求返回json类型数据",
"request": {
"method": "get",
"uri": "/login_json"
},
"response": {
"json": {
"key":"value",
"请求方式是get":"相应结果为json类型"
},
"headers": {
"Content-Type": "application/json;charset=gbk"
}
}
}
]

各个参数无需多言吧,简单明了。在响应体加入​​headers​​​指定​​Content-Type​​​编码格式为gbk是为了防止中文乱码问题。
另外可以根据响应文本内容指定​​​application/json​​​或者​​text/html​​。

浏览器地址栏输入:

http://127.0.0.1:8888/login
http://127.0.0.1:8888/reg?p1=v1&p2=v2
http://127.0.0.1:8888/login_json

post请求
​​​configure.json​​:

[
{
"description": "post请求,请求参数为json格式,响应格式为json",
"request": {
"method": "post",
"uri": "/post_json",
"json": {
"login_status": "successful"
}
},
"response": {
"json": {
"login": "ok"
},
"headers": {
"Content-Type": "application/json;charset=gbk"
}
}
},
{
"description": "post请求,请求及响应都为json,并且请求带cookies",
"request": {
"method": "post",
"uri": "/post_cookie",
"json": {
"login_status": "successful"
},
"cookies":{
"user_id":"xsdaqawea"
}
},
"response": {
"json": {
"login": "ok"
},
"headers": {
"Content-Type": "application/json;charset=gbk"
}
}
},
{
"description": "post请求,请求及响应都为json,并且请求带cookies和headers",
"request": {
"method": "post",
"uri": "/post_cookie",
"json": {
"login_status": "successful"
},
"cookies": {
"user_id": "xsdaqawea"
},
"headers":{
"Content-Type":"application/json"
}
},
"response": {
"json": {
"login": "ok"
},
"headers": {
"Content-Type": "application/json;charset=gbk"
}
}
}
]

发送请求及结果:

import requests

res1 = requests.post(url='http://127.0.0.1:8888/post_json', json={"login_status": "successful"})

print(res1.json()) # {'login': 'ok'}


res2 = requests.post(url='http://127.0.0.1:8888/post_cookie', cookies={"user_id": "xsdaqawea"}, json={"login_status": "successful"})

print(res2.json()) # {'login': 'ok'}

请求重定向
​​​confiure.json​​:

[
{
"description":"重定向到指定网站",
"request":{
"method":"get",
"uri":"/login_redirect"
},
"redirectTo":"https://www.baidu.com"
}
]

发送请求及结果:

http://127.0.0.1:8888/login_json

举报

相关推荐

Moco使用教程

论文阅读——MoCo

0 条评论