#!/usr/bin/env python3 """测试合并状态码功能""" import sys import os sys.path.append(os.path.dirname(os.path.abspath(__file__))) from datetime import datetime from api_config import API_CONFIG, RETRY_CONFIG import requests import logging # 配置日志 logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def test_status_code_handling(): """测试不同状态码的处理""" print("测试合并状态码 200, 201, 204 的处理") print("=" * 40) current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 测试几个主要的API test_apis = ['main_api', 'health_check', 'get_api'] for api_name in test_apis: if api_name in API_CONFIG: api_config = API_CONFIG[api_name] api_url = api_config['url'] headers = api_config['headers'] timeout = api_config['timeout'] payload = api_config['body'] method = api_config['method'].upper() try: print(f"\n🔧 测试 {api_name}: {method} {api_url}") # 根据method选择请求方式 if method == 'POST': response = requests.post(api_url, headers=headers, json=payload, timeout=timeout) elif method == 'GET': response = requests.get(api_url, headers=headers, params=payload, timeout=timeout) elif method == 'PUT': response = requests.put(api_url, headers=headers, json=payload, timeout=timeout) elif method == 'DELETE': response = requests.delete(api_url, headers=headers, timeout=timeout) print(f"📊 状态码: {response.status_code}") # 使用新的合并状态码逻辑 if response.status_code in [200, 201, 204]: # 成功状态码 try: result_data = response.json() except: result_data = {'response': response.text} # 构建成功消息 if response.status_code == 201: print(f"✅ 成功 (创建) - ID: {result_data.get('id')}") elif response.status_code == 200: print(f"✅ 成功 (OK) - 数据长度: {len(str(result_data))}") elif response.status_code == 204: print(f"✅ 成功 (无内容)") else: print(f"❌ 失败 - 状态码: {response.status_code}") except requests.exceptions.RequestException as e: print(f"❌ 网络异常: {str(e)}") continue # 继续测试下一个API except Exception as e: print(f"❌ 其他异常: {str(e)}") continue # 继续测试下一个API if __name__ == "__main__": test_status_code_handling()