Mock-Server
一 下载地址
moco框架下载地址
https://github.com/dreamhead/moco github项目地址
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/ jar包下载地址
https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md#query-parameter 文档
测试一下
如下图创建一个mock_server的文件夹
把下载的jar包放在mock_server文件夹下面
创建一个test.json文件,内容如下:
[{
"description": "模拟一个没有参数的get请求。",
"request": {
"uri": "/api",
"method": "get"
},
"response": {
"text": "hello get request !"
}
}
]
切换到mock_server目录执行命令
java -jar ./moco-runner-0.11.0-standalone.jar http -p 7666 -c test.json
成功的截图:能看到server us started at 端口号
**get请求浏览器输入 ** http://localhost:7666/api
如果响应正文是有乱码怎么办,加个headers
[{
"description": "模拟一个没有参数的get请求。",
"request": {
"uri": "/api",
"method": "get"
},
"response": {
"text": "hello get request !中国",
"headers": {
"Content-Type":"text/html;charset=gbk"
}
}
}
]
二 基本操作
get请求
get请求:没有请求参数
[{
"description": "模拟一个没有参数的get请求。",
"request": {
"uri": "/api",
"method": "get"
},
"response": {
"text": "hello get request !中国",
"headers": {
"Content-Type":"text/html;charset=gbk"
}
}
}
]
import requests
base_url = "http://127.0.0.1:7666"
res = requests.request("get", base_url + "/api")
print(res.text)
get请求:有参数
[
{
"description": "模拟一个没有参数的get请求。",
"request": {
"uri": "/api/moco/get/demo",
"method": "get"
},
"response": {
"text": "hello get request !"
}
},
{
"description": "模拟一个带参数的get请求。",
"request": {
"uri": "/api/moco/get/param/demo",
"method": "get",
"queries": {
"name": "xiaoming",
"age": "18"
}
},
"response": {
"text": "hello xiaoming !"
}
}
]
import requests
base_url = "http://127.0.0.1:7666"
data = {
"name": "xiaoming",
"age": "18"
}
res = requests.request("get", base_url + "/api/moco/get/param/demo", params=data)
print(res.text)
print(res.status_code)
post请求,cookies,表单传参
[
{
"description": "模拟一个带cookie的post请求。",
"request": {
"uri": "/api/moco/post/cookies/demo",
"method": "post",
"cookies": {
"login": "true"
},
"forms": { # post请求的参数也可以用json格式的数据进行传输
"name": "xiaoming",
"age": "18"
}
},
"response": {
"status": 201,
"json": {
"text": "hello post cookies !"
}
}
}
]
import requests
base_url = "http://127.0.0.1:7666"
data = {
"name": "xiaoming",
"age": "18"
}
cookies = {
"login": "true"
}
res = requests.request("post", base_url + "/api/moco/post/cookies/demo", data=data,cookies=cookies)
print(res.text)
print(res.json())
print(res.status_code)
post请求+cookies+json传参
[
{
"description": "模拟一个带cookie的post请求。",
"request": {
"uri": "/api/moco/post/cookies/demo",
"method": "post",
"cookies": {
"login": "true"
},
"json": {
"name": "xiaoming",
"age": "18"
}
},
"response": {
"status": 201,
"json": {
"text": "hello post cookies !"
}
}
}
]
import requests
base_url = "http://127.0.0.1:7666"
data = {
"name": "xiaoming",
"age": "18"
}
cookies = {
"login": "true"
}
res = requests.request("post", base_url + "/api/moco/post/cookies/demo", json=data,cookies=cookies)
print(res.text)
print(res.json())
print(res.status_code)
post请求有headers
[
{
"description": "模拟一个带cookie的post请求。",
"request": {
"uri": "/api/moco/post/cookies/demo",
"method": "post",
"cookies": {
"login": "true"
},
"header": {
"content-type": "application/json"
},
"json": {
"name": "xiaoming",
"age": "18"
}
},
"response": {
"status": 201,
"json": {
"text": "hello post cookies !"
}
}
}
]
import requests
base_url = "http://127.0.0.1:7666"
data = {
"name": "xiaoming",
"age": "18"
}
cookies = {
"login": "true"
}
headers = {
"content-type": "application/json"
}
res = requests.request("post", base_url + "/api/moco/post/cookies/demo", json=data,cookies=cookies,headers=headers)
print(res.text)
print(res.json())
print(res.status_code)
模拟重定向
[
{
"description": "重定向",
"request": {
"uri": "/api/moco/redirect/demo",
"method": "get"
},
"redirectTo": "/api/moco/redirect/new/demo"
},
{
"description": "重定向到自己的接口",
"request": {
"uri": "/api/moco/redirect/new/demo",
"method": "get"
},
"response": {
"status": 301,
"text": "hello redirectTo !"
}
}
]
import requests
base_url = "http://127.0.0.1:7666"
res = requests.request("get", base_url + "/api/moco/redirect/demo")
print(res.text)
print(res.status_code)
在浏览器中输入 http://127.0.0.1:7666/api/moco/redirect/demo 直接跳转到如下图
三 项目全局配置Mock
上面的例子所有的接口,和数据都放在一个json文件里面,如果有100个接口,很难维护这个json文件。下面我们用一个实际的接口文档来Mock,全局配置。接口文档如下
1.1 注册
接口地址:http://127.0.0.1:8082/erp/regist
请求方式:POST
POST 请求格式:application/json
响应格式:application/json
返回示例:
{
"name": "zhang",
"pwd": "111",
"age": null,
"email": null,
"phone": null
}
1.2 登录
接口地址:http://127.0.0.1:8080/erp/loginIn
请求方法:POST
POST 请求格式:application/json
响应格式:application/json
返回示例:
{
"code": "200",
"message": "登录成功",
"token": "3768e8cf0c1bd64028eee6c2d44e100a340c914c68e71cb76c93b4931259c3aa"
}
1.3 查询用户信息
业务描述:查询接口,是根据请求头中传过来的 Token 值,系统自动判断,然后返回登录用户的信息
接口地址:http://127.0.0.1:8080/erp/user
请求方法:get
请求头:Token
响应格式:application/json
参数:无
返回示例:
{
"name": "zhang",
"pwd": "111",
"age": null,
"email": null,
"phone": null
}
准备
在项目根目录,建立一个mock-server文件夹。
根据接口文档,一个接口一个文件夹,文件夹里面,再创建1个发请求格式的json文件,一个响应返回数据的json文件。
创建一个register文件夹,里面创建一个register.json, register_response.json.
创建一个login文件夹,里面创建一个login.json, login_response.json.
创建一个user文件夹,里面创建一个user.json, user_response.json.
login登录成功的token,user查询用户接口需要传到headers里面
register.json数据,根据接口文档编写
[
{
"description": "模拟一个注册接口",
"request": {
"uri": "/erp/regist",
"method": "post",
"headers": {
"content-type": "application/json"
},
"json": {
"name": "adeng",
"pwd": "a12345678"
}
},
"response": {
"status": 201,
"headers":{
"content-type": "application/json"
},
"file" : "register/register_response.json"
}
}
]
register_response.json 注册返回的响应正文数据
{
"name": "adeng",
"pwd": "a12345678",
"age": null,
"email": null,
"phone": null
}
**login.son **
[
{
"description": "模拟一个登录接口",
"request": {
"uri": "/erp/loginIn",
"method": "post",
"headers": {
"content-type": "application/json"
},
"json": {
"name": "adeng",
"pwd": "a12345678"
}
},
"response": {
"status": 201,
"headers":{
"content-type": "application/json"
},
"file" : "./login/login_response.json"
}
}
]
login_response.json
{
"code": "200",
"message": "登录成功",
"token": "3768e8cf0c1bd64028eee6c2d44e100a340c914c68e71cb76c93b4931259c3aa"
}
user.josn
[
{
"description": "模拟一个查询接口",
"request": {
"uri": "/erp/user",
"method": "get",
"headers": {
"token": "3768e8cf0c1bd64028eee6c2d44e100a340c914c68e71cb76c93b4931259c3aa"
}
},
"response": {
"status": 201,
"headers":{
"content-type": "application/json"
},
"file" : "./user/user_response.json"
}
}
]
user_response.json
{
"name": "adeng",
"pwd": "a12345678",
"age": null,
"email": null,
"phone": null
}
注意请求的json,response,file的值
file的值是mock_server目录下每一个接口的 response.json相对于mock_server的路径
演练步骤
切换到mock_server目录
执行java -jar ./moco-runner-0.11.0-standalone.jar start -p 4111 -g settings.json
编写python模拟发送请求
注册接口
import requests
base_url = "http://127.0.0.1:4111"
headers = {
"content-type": "application/json"
}
# 参数
data = {
"name": "adeng",
"pwd": "a12345678"
}
res = requests.request("post", base_url + "/erp/regist",headers=headers,json=data)
print(res.text)
print(res.status_code)
print(res.json())
登录接口
import requests
base_url = "http://127.0.0.1:4111"
headers = {
"token": "3768e8cf0c1bd64028eee6c2d44e100a340c914c68e71cb76c93b4931259c3aa"
}
res = requests.request("get", base_url + "/erp/user",headers=headers)
print(res.text)
print(res.status_code)
print(res.json())
查询用户接口
import requests
base_url = "http://127.0.0.1:4111"
headers = {
"token": "3768e8cf0c1bd64028eee6c2d44e100a340c914c68e71cb76c93b4931259c3aa"
}
res = requests.request("get", base_url + "/erp/user",headers=headers)
print(res.text)
print(res.status_code)
print(res.json())
CMD命令启动和停止
-s port
是关闭的端口号
java -jar moco-runner-0.11.0-standalone.jar http -p 7666 -c test.json -s 9527
java -jar moco-runner-0.11.0-standalone.jar shutdown -s 9527
发送 https
制作cert证书
通过jdk命令生成证书
keytool -genkey -alias adeng -keypass 123456 -keyalg RSA -keysize 1024 -validity 3650 -keystore i:/adeng.keystore -storepass 123456
keytool -genkey -alias adeng -keypass 123456 -keyalg RSA -keysize 1024 -validity 3650 -keystore i:/adeng.keystore -storepass 123456
说明
- -alias adeng (别名)
- -keypass 123456(别名密码)
- -keyalg RSA(生证书的算法名称,RSA是一种非对称加密算法)
- -keysize 1024(密钥长度,证书大小)
- -validity 365(证书有效期,天单位)
- -keystore e:/zhengshu.keystore (指定生成证书的位置和证书名称)
- -storepass 123456(获取keystore信息的密码)
然后将生成的证书adeng.keystore放在mock-server目录下
test.json文件内容
[{
"description": "模拟一个没有参数的get请求。",
"request": {
"uri": "/api",
"method": "get"
},
"response": {
"text": "hello get request !"
}
}
]
启动https mock-server
java -jar moco-runner-1.2.0-standalone.jar https -p 4118 -c test.json --https adeng.keystore --cert 123456 --keystore 123456 -s 6543
java -jar moco-runner-1.2.0-standalone.jar https -p 4118 -c test.json --https adeng.keystore --cert 123456 --keystore 123456 -s 6543
requests模拟发送请求
verify = False
记得设置这个参数默认为None
import requests
import os
# https 需要取消认证 verify=false
res = requests.request("get","https://127.0.0.1:4118/api",verify=False)
print(res.text)
# 关闭mock服务器
os.system("java -jar moco-runner-1.2.0-standalone.jar shutdown -s 6543")
参考文献
https://blog.csdn.net/shensky711/article/details/52770686