corp20250923
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
测试update_query_result接口的脚本
|
||||
用于模拟发送请求并获取详细的错误信息
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
|
||||
# 配置日志
|
||||
handlers = [logging.FileHandler('test_api.log'), logging.StreamHandler()]
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s',
|
||||
handlers=handlers
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# API地址
|
||||
BASE_URL = 'http://localhost:99'
|
||||
API_ENDPOINT = f'{BASE_URL}/api/update-query-result'
|
||||
|
||||
def test_update_query_result():
|
||||
"""
|
||||
测试update_query_result接口
|
||||
"""
|
||||
logger.info(f"开始测试API接口: {API_ENDPOINT}")
|
||||
|
||||
# 测试数据
|
||||
test_data = {
|
||||
'id': 1, # 假设query_history表中有id为1的记录
|
||||
'results_count': 1,
|
||||
'cars_info': [
|
||||
{
|
||||
'plate_number': '测试车牌1',
|
||||
'phone': '13800138001',
|
||||
'name': '测试姓名1',
|
||||
'email': 'test1@example.com',
|
||||
'address': '测试地址1'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
try:
|
||||
logger.info(f"发送测试请求,数据: {json.dumps(test_data, ensure_ascii=False)}")
|
||||
|
||||
# 发送POST请求
|
||||
start_time = time.time()
|
||||
response = requests.post(
|
||||
API_ENDPOINT,
|
||||
json=test_data,
|
||||
headers={'Content-Type': 'application/json'},
|
||||
timeout=30
|
||||
)
|
||||
end_time = time.time()
|
||||
|
||||
logger.info(f"请求完成,耗时: {end_time - start_time:.2f}秒,状态码: {response.status_code}")
|
||||
|
||||
# 输出响应内容
|
||||
if response.status_code == 200:
|
||||
try:
|
||||
result = response.json()
|
||||
logger.info(f"响应数据: {json.dumps(result, ensure_ascii=False)}")
|
||||
except json.JSONDecodeError:
|
||||
logger.warning(f"响应不是有效的JSON格式: {response.text}")
|
||||
else:
|
||||
logger.error(f"请求失败,状态码: {response.status_code},响应内容: {response.text}")
|
||||
|
||||
return response.status_code
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error(f"请求异常: {str(e)}")
|
||||
return None
|
||||
|
||||
def main():
|
||||
"""
|
||||
主函数
|
||||
"""
|
||||
logger.info("测试脚本启动")
|
||||
|
||||
# 执行测试
|
||||
status_code = test_update_query_result()
|
||||
|
||||
if status_code == 200:
|
||||
logger.info("测试成功")
|
||||
elif status_code is not None:
|
||||
logger.error(f"测试失败,状态码: {status_code}")
|
||||
else:
|
||||
logger.error("测试失败,请求异常")
|
||||
|
||||
logger.info("测试脚本结束")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user