Skip to content

Commit

Permalink
fix: fix variables bug, close proxy as default, remove requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
wuranxu committed Jan 25, 2024
1 parent be035bf commit 5458f8d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 25 deletions.
12 changes: 6 additions & 6 deletions app/core/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ async def parse_gconfig(self, data, type_, env, *fields):
for f in fields:
await self.parse_field(data, f, GconfigType.text(type_), env)

async def load_testcase_variables(self, data, type_, *fields):
async def load_testcase_variables(self, data, type_, params, *fields):
"""load_testcase_variables, include global variables"""
for f in fields:
self.append("解析{}: [{}]中的变量".format(GconfigType.text(type_), data, f))
origin_field = getattr(data, f)
# if not None or ""
if origin_field:
rendered = Render.render(self.glb, origin_field)
rendered = Render.render(params, origin_field)
if rendered != origin_field:
self.append("替换变量成功, [{}]:\n\n[{}] -> [{}]\n".format(f, origin_field, rendered))
setattr(data, f, rendered)
Expand Down Expand Up @@ -222,7 +222,7 @@ async def execute_constructors(self, env: int, path, case_info, params, req_para
for i, c in enumerate(constructors):
if c.suffix == suffix:
await self.execute_constructor(env, current, path, params, req_params, c)
self.replace_args(params, case_info, constructors, asserts)
# self.replace_args(params, case_info, constructors, asserts)
current += 1

async def execute_constructor(self, env, index, path, params, req_params, constructor: Constructor):
Expand Down Expand Up @@ -279,7 +279,7 @@ async def run(self, env: int, case_id: int, params_pool: dict = None, request_pa

# 挂载全局变量
case_params.update(self.glb)
request_param.update(self.glb)
req_params.update(self.glb)

try:
case_info = await TestCaseDao.async_query_test_case(case_id)
Expand All @@ -298,13 +298,13 @@ async def run(self, env: int, case_id: int, params_pool: dict = None, request_pa
out_parameters = await PityTestCaseOutParametersDao.select_list(case_id=case_id)

# Step5: 替换参数
self.replace_args(req_params, case_info, constructors, asserts)
# self.replace_args(req_params, case_info, constructors, asserts)

# Step6: 执行前置条件
await self.execute_constructors(env, path, case_info, case_params, req_params, constructors, asserts)

# 获取全局变量更新body url headers
await self.load_testcase_variables(case_info, GconfigType.case, *Executor.fields)
await self.load_testcase_variables(case_info, GconfigType.case, case_params, *Executor.fields)

if case_info.request_headers and case_info.request_headers != "":
headers = json.loads(case_info.request_headers)
Expand Down
1 change: 0 additions & 1 deletion app/core/msg/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from email.mime.text import MIMEText
from email.utils import make_msgid

import aioify
from awaits.awaitable import awaitable
from jinja2.environment import Template

Expand Down
11 changes: 2 additions & 9 deletions app/core/paramters/jsonpath_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
jsonpath parser
"""
import json
from functools import lru_cache
from typing import Any

import jsonpath
Expand All @@ -19,28 +18,22 @@ def get_source(cls, source):

@classmethod
def parse(cls, source: dict, expression: str = "", **kwargs) -> Any:
source = cls.get_source(source)
data = cls.get_source(source)
if not source or not expression:
raise CaseParametersError(f"parse out parameters failed, source or expression is empty")
try:
data = JSONPathParser.get_object(source)
results = jsonpath.jsonpath(data, expression)
if results is False:
if not data and expression == "$..*":
# 说明想要全匹配并且没数据,直接返回data
return json.dumps(data, ensure_ascii=False)
return data
raise CaseParametersError("jsonpath match failed, please check your response or jsonpath.")
return Parser.parse_result(results, "0")
except CaseParametersError as e:
raise e
except Exception as err:
raise CaseParametersError(f"parse json data error, please check jsonpath or json: {err}")

@staticmethod
@lru_cache()
def get_object(json_str):
return json.loads(json_str)


class BodyJSONPathParser(JSONPathParser):
@classmethod
Expand Down
8 changes: 4 additions & 4 deletions app/middleware/AsyncHttpClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ async def invoke(self, method: str):
# 修复bug,当http状态码不为200的时候给出提示
return await self.collect(False, self.get_data(self.kwargs), resp.status, msg="http状态码不为200")
cost = "%.0fms" % ((time.time() - start) * 1000)
response, json_format = await AsyncRequest.get_resp(resp)
response = await AsyncRequest.get_resp(resp)
cookie = self.get_cookie(session)
return await self.collect(True, self.get_data(self.kwargs), resp.status, response,
resp.headers, resp.request_info.headers, elapsed=cost,
cookies=cookie, json_format=json_format)
cookies=cookie)

async def download(self):
async with aiohttp.ClientSession(cookie_jar=aiohttp.CookieJar(unsafe=True)) as session:
Expand Down Expand Up @@ -99,11 +99,11 @@ async def get_resp(resp):
try:
data = await resp.json(encoding='utf-8')
# 说明是json格式
return json.dumps(data, ensure_ascii=False, indent=4), True
# return json.dumps(data, ensure_ascii=False, indent=4), True
except:
data = await resp.text()
# 说明不是json格式,我们不做loads操作了
return data, False
return data

@staticmethod
def get_request_data(body):
Expand Down
8 changes: 4 additions & 4 deletions conf/dev.env
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# MySQL config
MYSQL_HOST="localhost"
MYSQL_HOST="121.5.2.74"
MYSQL_PORT=3306
MYSQL_USER="root"
MYSQL_PWD=""
DBNAME="pity"

# Redis config
REDIS_ON=True
REDIS_HOST="localhost"
REDIS_HOST="121.5.2.74"
REDIS_PORT=7788
REDIS_DB=0
REDIS_PASSWORD=""
Expand All @@ -20,8 +20,8 @@ SECRET_KEY="6cb53ad7d135bb91a07f2deb7203d484741f1644"
SERVER_PORT=7777

# Mock server
MOCK_ON=True
PROXY_ON=True
MOCK_ON=False
PROXY_ON=False
PROXY_PORT=7778

# ETCD
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ yagmail
oss2~=2.15.0
python-multipart
awaits~=0.0.1
aioify~=0.4.0
grpc-requests
loguru~=0.6.0
qiniu~=7.6.0
Expand Down

0 comments on commit 5458f8d

Please sign in to comment.