|
|
|
|
import json
|
|
|
|
|
import requests
|
|
|
|
|
from lib.gzip_base64 import ungzip_base64_dh2cmc
|
|
|
|
|
from lib.redis_util import redis_set_key
|
|
|
|
|
from lib.db import Result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def req(name: str, url: str, method: str, params: dict, data: dict, case_id=None):
|
|
|
|
|
print('-' * 100 + '\n' + f'请求{name}接口')
|
|
|
|
|
print('请求参数', data)
|
|
|
|
|
if method == 'GET':
|
|
|
|
|
params = {**params, **data}
|
|
|
|
|
data = {}
|
|
|
|
|
response = requests.post(url, params=params)
|
|
|
|
|
else:
|
|
|
|
|
response = requests.post(url, params=params, data=data)
|
|
|
|
|
response_json = response.text
|
|
|
|
|
|
|
|
|
|
if 'status' not in response_json:
|
|
|
|
|
print(f"-- gzip加密结果 --\n{response_json}")
|
|
|
|
|
response_json = ungzip_base64_dh2cmc(response_json)
|
|
|
|
|
response_obj = json.loads(response_json)
|
|
|
|
|
|
|
|
|
|
# 打印信息
|
|
|
|
|
print("-- 打印请求URL --\n", response.request.url)
|
|
|
|
|
print("-- 打印请求头 --\n", response.request.headers)
|
|
|
|
|
print("-- 打印请求体 --\n", response.request.body)
|
|
|
|
|
print("-- Json结果 --\n", response_json)
|
|
|
|
|
print("-- Obj结果 --\n", response_obj)
|
|
|
|
|
|
|
|
|
|
# 返回结果写缓存
|
|
|
|
|
redis_set_key(f'json_{name}', response_json)
|
|
|
|
|
|
|
|
|
|
# 记录测试结果
|
|
|
|
|
result = Result()
|
|
|
|
|
result.save((name, 'test', case_id, None, None, None, None, method, json.dumps(params), json.dumps(data), response_json, None))
|
|
|
|
|
|
|
|
|
|
return params, data, response_obj
|