mirror of
https://gitee.com/mao-peng/MangoTestingPlatform.git
synced 2025-12-06 11:59:15 +08:00
修改API请求的问题
This commit is contained in:
4
MangoServer/.gitignore
vendored
4
MangoServer/.gitignore
vendored
@@ -8,4 +8,6 @@ mango-file/
|
||||
failed_screenshot/
|
||||
src/settings/database.json
|
||||
src/auto_test/auto_pytest/mango_pytest/
|
||||
mango_pytest/
|
||||
mango_pytest/
|
||||
download/
|
||||
upload/
|
||||
@@ -17,16 +17,23 @@ from src.models.api_model import RequestModel, ResponseModel
|
||||
class ApiInfoBase(PublicBase):
|
||||
""" API INFO"""
|
||||
|
||||
def api_request(self, api_info_id: int, request_model: RequestModel = None, is_error=True) -> ResponseModel:
|
||||
def api_request(self, api_info_id: int, request_model: RequestModel = None, is_error=True,
|
||||
is_merge_headers=False) -> ResponseModel:
|
||||
log.api.debug(f'执行API接口-1->ID:{api_info_id}')
|
||||
api_info = ApiInfo.objects.get(id=api_info_id)
|
||||
self.project_product_id = api_info.project_product.id
|
||||
|
||||
if is_merge_headers and api_info.header:
|
||||
headers = self.init_headers()
|
||||
headers.update(api_info.header)
|
||||
elif api_info.header is not None:
|
||||
headers = api_info.header
|
||||
else:
|
||||
headers = self.init_headers()
|
||||
if request_model is None:
|
||||
request_model = self.request_data_clean(RequestModel(
|
||||
method=MethodEnum(api_info.method).name,
|
||||
url=urljoin(self.test_object.value, api_info.url),
|
||||
headers=api_info.header if api_info.header is not None else self.init_headers(),
|
||||
headers=headers,
|
||||
params=api_info.params,
|
||||
data=api_info.data,
|
||||
json=api_info.json,
|
||||
@@ -58,7 +65,6 @@ class ApiInfoBase(PublicBase):
|
||||
def api_info_posterior_json_re(self, posterior_re: str, response: ResponseModel):
|
||||
log.api.debug(f'执行API接口-3->后置正则:{posterior_re}')
|
||||
|
||||
|
||||
@staticmethod
|
||||
def analytic_func(func_str, func_name='func'):
|
||||
log.api.debug(f'执行API接口-4->后置函数:{func_str}')
|
||||
|
||||
@@ -21,7 +21,7 @@ class BaseRequest:
|
||||
|
||||
def http(self, request_data: RequestModel) -> ResponseModel:
|
||||
try:
|
||||
log.api.debug(f'开始执行接口:{request_data.model_dump_json()}')
|
||||
log.api.debug(f'开始执行接口:{request_data}')
|
||||
s = time.time()
|
||||
response = requests.request(
|
||||
method=request_data.method,
|
||||
@@ -50,6 +50,11 @@ class BaseRequest:
|
||||
response = ResponseModel(
|
||||
code=response.status_code,
|
||||
time=end,
|
||||
request_headers=request_data.headers,
|
||||
request_params=request_data.params,
|
||||
request_data=request_data.data,
|
||||
request_json=request_data.json,
|
||||
request_file=str(request_data.file),
|
||||
headers=response.headers,
|
||||
json=response_json,
|
||||
text=response.text
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
# @Description:
|
||||
# @Time : 2023-11-30 12:34
|
||||
# @Author : 毛鹏
|
||||
import os.path
|
||||
import traceback
|
||||
from typing import Optional
|
||||
|
||||
from mangotools.assertion import PublicAssertion
|
||||
@@ -19,7 +21,7 @@ from src.enums.tools_enum import StatusEnum, AutoTypeEnum
|
||||
from src.exceptions import *
|
||||
from src.models.api_model import RequestModel
|
||||
from src.tools.obtain_test_data import ObtainTestData
|
||||
|
||||
from src.tools import project_dir
|
||||
|
||||
class PublicBase(BaseRequest, PublicAssertion):
|
||||
""" 公共参数设置"""
|
||||
@@ -91,15 +93,15 @@ class PublicBase(BaseRequest, PublicAssertion):
|
||||
i: dict = i
|
||||
for k, v in i.items():
|
||||
file_name = self.test_data.identify_parentheses(v)[0].replace('(', '').replace(')', '')
|
||||
path = self.test_data.replace(v)
|
||||
file.append((k, (file_name, open(path, 'rb'))))
|
||||
file.append((k, (file_name, open(self.test_data.replace(v), 'rb'))))
|
||||
request_data_model.file = file
|
||||
else:
|
||||
value = self.test_data.replace(value)
|
||||
setattr(request_data_model, key, value)
|
||||
except MangoToolsError as error:
|
||||
print(traceback.print_exc())
|
||||
raise ApiError(error.code, error.msg)
|
||||
log.api.debug(f'清洗请求数据-2->{request_data_model.model_dump_json()}')
|
||||
log.api.debug(f'清洗请求数据-2->{request_data_model}')
|
||||
return request_data_model
|
||||
|
||||
def __custom(self, api_public_obj: ApiPublic):
|
||||
|
||||
@@ -22,7 +22,7 @@ class TestApiInfo(ApiInfoBase):
|
||||
api_info.save()
|
||||
self.init_test_object(api_info.project_product_id)
|
||||
self.init_public(api_info.project_product_id)
|
||||
response = self.api_request(api_info.id)
|
||||
response = self.api_request(api_info.id, is_merge_headers=True)
|
||||
api_info.status = TaskEnum.SUCCESS.value
|
||||
api_info.save()
|
||||
return self.save_api_info(api_info, response)
|
||||
@@ -39,7 +39,6 @@ class TestApiInfo(ApiInfoBase):
|
||||
api_info.status = StatusEnum.FAIL.value
|
||||
res = response.model_dump()
|
||||
res['name'] = api_info.name
|
||||
|
||||
res['cache_all'] = self.test_data.get_all()
|
||||
api_info.result_data = res
|
||||
api_info.save()
|
||||
|
||||
@@ -21,13 +21,13 @@ ERROR_MSG_0015 = (1015, '请先到 系统管理->配置管理中配置git请求
|
||||
ERROR_MSG_0016 = (1016, '')
|
||||
ERROR_MSG_0017 = (1017, '拉取代码失败')
|
||||
ERROR_MSG_0018 = (1018, '不是有效的git地址,如果确认地址有效则联系管理员!')
|
||||
ERROR_MSG_0019 = (1019, '')
|
||||
ERROR_MSG_0020 = (1020, '')
|
||||
ERROR_MSG_0019 = (1019, '找不到下载的目录,请检查logs中是否存在:download,这个目录')
|
||||
ERROR_MSG_0020 = (1020, 'minio下载文件失败,请检查配置是否正确')
|
||||
ERROR_MSG_0021 = (1021, '该项目未配置mysql,但是开启了数据库操作')
|
||||
ERROR_MSG_0022 = (1022, 'Mysql配置错误,请先在数据库中检查mysql配置是否正确')
|
||||
ERROR_MSG_0023 = (1023, '只有json格式的返回数据才可以使用jsonpath的后置')
|
||||
ERROR_MSG_0024 = (1024, '')
|
||||
ERROR_MSG_0025 = (1025, '')
|
||||
ERROR_MSG_0024 = (1024, 'minio下载文件失败,请检查文件名称是否正确,现在的文件名称:{}')
|
||||
ERROR_MSG_0025 = (1025, 'minio没有进行配置,所以无法测试上传文件的用例')
|
||||
ERROR_MSG_0026 = (1026, 'web端不提供该方法,您已经成功了,请直接开始使用该方法进行测试,文件名称:{}')
|
||||
ERROR_MSG_0027 = (1027, '您要设置的用户open状态失败,可能用户没有登录执行器,请先登录之后再进行设置状态')
|
||||
ERROR_MSG_0028 = (1028, '发送任务失败,请确保{}已连接{}')
|
||||
|
||||
@@ -24,6 +24,11 @@ class RequestModel(BaseModel):
|
||||
class ResponseModel(BaseModel):
|
||||
code: int
|
||||
time: float
|
||||
request_headers: dict | None = None
|
||||
request_params: dict | list | str | None = None
|
||||
request_data: dict | list | None = None
|
||||
request_json: dict | list | None = None
|
||||
request_file: list[dict] | Any | None = None
|
||||
headers: dict
|
||||
json: dict | list | None = None
|
||||
text: str | None = None
|
||||
|
||||
@@ -32,6 +32,7 @@ if not IS_MINIO:
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'mango-file')
|
||||
else:
|
||||
DEFAULT_FILE_STORAGE = 'minio_storage.storage.MinioMediaStorage'
|
||||
|
||||
# **********************************************************************************************************************
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
@@ -13,7 +13,7 @@ from src.settings import BASE_DIR
|
||||
class ProjectDir:
|
||||
|
||||
def __init__(self):
|
||||
self.folder_list = ['logs', 'mango-file', 'failed_screenshot', 'upload_template', 'download']
|
||||
self.folder_list = ['logs', 'mango-file', 'failed_screenshot', 'upload_template', 'download', 'upload']
|
||||
self._root_path = BASE_DIR
|
||||
self.init_folder()
|
||||
|
||||
@@ -46,6 +46,10 @@ class ProjectDir:
|
||||
def download(self, folder_name='download'):
|
||||
return os.path.join(self._root_path, folder_name)
|
||||
|
||||
def upload(self, folder_name='upload'):
|
||||
return os.path.join(self._root_path, folder_name)
|
||||
|
||||
|
||||
project_dir = ProjectDir()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -3,17 +3,51 @@
|
||||
# @Description:
|
||||
# @Time : 2024-11-19 11:36
|
||||
# @Author : 毛鹏
|
||||
import os
|
||||
import shutil
|
||||
from urllib.parse import urljoin
|
||||
|
||||
import requests
|
||||
from mangotools.data_processor import DataProcessor
|
||||
from src.exceptions import ERROR_MSG_0026, ToolsError
|
||||
from requests.exceptions import MissingSchema
|
||||
|
||||
from src.auto_test.auto_system.models import FileData
|
||||
from src.exceptions import ERROR_MSG_0025, ERROR_MSG_0024, ToolsError, ERROR_MSG_0019, ERROR_MSG_0020
|
||||
from src.settings import IS_MINIO
|
||||
from src.tools import project_dir
|
||||
|
||||
|
||||
class ObtainTestData(DataProcessor):
|
||||
|
||||
@classmethod
|
||||
def get_file(cls, **kwargs) -> None:
|
||||
def get_file(cls, **kwargs) -> str:
|
||||
"""传入文件名称,返回文件对象"""
|
||||
file_name = kwargs.get('data')
|
||||
raise ToolsError(*ERROR_MSG_0026, value=(file_name,))
|
||||
if IS_MINIO:
|
||||
from src.settings import MINIO_STORAGE_ENDPOINT
|
||||
minio_host = MINIO_STORAGE_ENDPOINT
|
||||
if minio_host is None:
|
||||
raise ToolsError(*ERROR_MSG_0025)
|
||||
url = urljoin(minio_host, f'/mango-file/test_file/{file_name}')
|
||||
response = requests.get(url, proxies={'http': None, 'https': None}, )
|
||||
try:
|
||||
if response.status_code != 200:
|
||||
raise ToolsError(*ERROR_MSG_0024, value=(file_name,))
|
||||
file_path = os.path.join(project_dir.upload(), file_name)
|
||||
with open(file_path, 'wb') as f:
|
||||
f.write(response.content)
|
||||
return str(file_path)
|
||||
except FileNotFoundError:
|
||||
raise ToolsError(*ERROR_MSG_0020)
|
||||
except MissingSchema:
|
||||
raise ToolsError(*ERROR_MSG_0019)
|
||||
else:
|
||||
file_data = FileData.objects.get(name=file_name)
|
||||
shutil.copy2(
|
||||
str(os.path.join(project_dir.root_path(), 'mango-file', file_data.test_file.path)),
|
||||
str(os.path.join(project_dir.upload(), file_name))
|
||||
)
|
||||
return str(os.path.join(project_dir.upload(), file_name))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -134,26 +134,60 @@
|
||||
/>
|
||||
</a-tab-pane>
|
||||
<a-tab-pane key="8" title="响应结果">
|
||||
<a-space direction="vertical">
|
||||
<a-space>
|
||||
<a-tag color="orange">响 应 码</a-tag>
|
||||
<span>{{ data.result_data?.code }}</span>
|
||||
</a-space>
|
||||
<a-space>
|
||||
<a-tag color="orange">响应时间</a-tag>
|
||||
<span>{{ data.result_data?.time }}</span>
|
||||
</a-space>
|
||||
<a-space>
|
||||
<a-tag color="orange">缓存数据</a-tag>
|
||||
<span>{{ data.result_data?.cache_all }}</span>
|
||||
</a-space>
|
||||
<a-space>
|
||||
<a-tag color="orange">响 应 体</a-tag>
|
||||
<pre>{{
|
||||
strJson(data.result_data?.json ? data.result_data?.json : data.result_data?.text)
|
||||
}}</pre>
|
||||
</a-space>
|
||||
</a-space>
|
||||
<a-tabs default-active-key="1">
|
||||
<a-tab-pane key="1" title="基础信息">
|
||||
<a-space direction="vertical">
|
||||
<a-space>
|
||||
<a-tag color="orange">响 应 码</a-tag>
|
||||
<span>{{ data.result_data?.code }}</span>
|
||||
</a-space>
|
||||
<a-space>
|
||||
<a-tag color="orange">响应时间</a-tag>
|
||||
<span>{{ data.result_data?.time }}</span>
|
||||
</a-space>
|
||||
<a-space>
|
||||
<a-tag color="orange">缓存数据</a-tag>
|
||||
<pre>{{ data.result_data?.cache_all }}</pre>
|
||||
</a-space>
|
||||
</a-space>
|
||||
</a-tab-pane>
|
||||
<a-tab-pane key="2" title="请求信息">
|
||||
<a-space direction="vertical">
|
||||
<a-space>
|
||||
<a-tag color="orange" v-if="data.result_data?.request_headers">请求头</a-tag>
|
||||
<pre>{{ data.result_data?.request_headers }}</pre>
|
||||
</a-space>
|
||||
<a-space>
|
||||
<a-tag color="orange" v-if="data.result_data?.request_params">参数</a-tag>
|
||||
<pre>{{ data.result_data?.request_params }}</pre>
|
||||
</a-space>
|
||||
<a-space>
|
||||
<a-tag color="orange" v-if="data.result_data?.request_data">表单</a-tag>
|
||||
<pre>{{ data.result_data?.request_data }}</pre>
|
||||
</a-space>
|
||||
<a-space>
|
||||
<a-tag color="orange" v-if="data.result_data?.request_json">json</a-tag>
|
||||
<pre>{{ data.result_data?.request_json }}</pre>
|
||||
</a-space>
|
||||
<a-space>
|
||||
<a-tag color="orange" v-if="data.result_data?.request_file">file</a-tag>
|
||||
<pre>{{ data.result_data?.request_file }}</pre>
|
||||
</a-space>
|
||||
</a-space>
|
||||
</a-tab-pane>
|
||||
<a-tab-pane key="3" title="响应信息">
|
||||
<a-space direction="vertical">
|
||||
<a-space>
|
||||
<a-tag color="orange">响 应 体</a-tag>
|
||||
<pre>{{
|
||||
strJson(
|
||||
data.result_data?.json ? data.result_data?.json : data.result_data?.text
|
||||
)
|
||||
}}</pre>
|
||||
</a-space>
|
||||
</a-space>
|
||||
</a-tab-pane>
|
||||
</a-tabs>
|
||||
</a-tab-pane>
|
||||
</a-tabs>
|
||||
</a-card>
|
||||
@@ -168,7 +202,6 @@
|
||||
import { useEnum } from '@/store/modules/get-enum'
|
||||
import useUserStore from '@/store/modules/user'
|
||||
import { strJson } from '@/utils/tools'
|
||||
import CodeEditor from '@/components/CodeEditor.vue'
|
||||
|
||||
const enumStore = useEnum()
|
||||
const userStore = useUserStore()
|
||||
@@ -301,6 +334,7 @@
|
||||
|
||||
onMounted(() => {
|
||||
nextTick(async () => {
|
||||
doRefresh()
|
||||
switchPageType()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user