api-tester
النجوم١٣
التفرعات١
آخر تحديث٢١ ديسمبر ٢٠٢٥ في ٠٩:٠٥
API 测试技能,帮助测试和调试 HTTP API 接口
التثبيت
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
SKILL.md
readonlyالقائمة
API 测试技能,帮助测试和调试 HTTP API 接口
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
代码审查技能,帮助审查代码质量、发现潜在问题并提供改进建议
数据分析技能,帮助分析数据、生成可视化图表和提供数据洞察
文档写作技能,帮助撰写技术文档、API 文档、用户手册等
HITL 人机交互测试技能,用于测试和演示多步人机交互功能(选择、确认、输入、表单)
SQL 专家技能,帮助编写、优化和调试 SQL 查询
| name | api-tester |
| description | API 测试技能,帮助测试和调试 HTTP API 接口 |
| category | testing |
| tags | ["api","testing","http","rest","debug"] |
这个技能帮助你测试和调试 HTTP API:
GET 请求
import requests
response = requests.get(
'https://api.example.com/users',
headers={'Authorization': 'Bearer token'},
params={'page': 1, 'limit': 10}
)
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
POST 请求
response = requests.post(
'https://api.example.com/users',
headers={
'Authorization': 'Bearer token',
'Content-Type': 'application/json'
},
json={
'name': '张三',
'email': 'zhangsan@example.com'
}
)
文件上传
with open('file.pdf', 'rb') as f:
response = requests.post(
'https://api.example.com/upload',
files={'file': f},
data={'description': '文件描述'}
)
def validate_response(response, expected_status=200):
"""验证 API 响应"""
# 状态码检查
assert response.status_code == expected_status, \
f"Expected {expected_status}, got {response.status_code}"
# JSON 格式检查
try:
data = response.json()
except ValueError:
raise AssertionError("Response is not valid JSON")
# 响应时间检查
assert response.elapsed.total_seconds() < 2, \
f"Response too slow: {response.elapsed.total_seconds()}s"
return data
# 使用
data = validate_response(response)
assert 'id' in data, "Missing 'id' field"
assert data['status'] == 'success', f"Unexpected status: {data['status']}"
import unittest
import requests
class APITestCase(unittest.TestCase):
BASE_URL = 'https://api.example.com'
TOKEN = 'your-token'
def setUp(self):
self.session = requests.Session()
self.session.headers.update({
'Authorization': f'Bearer {self.TOKEN}',
'Content-Type': 'application/json'
})
def test_get_users(self):
"""测试获取用户列表"""
response = self.session.get(f'{self.BASE_URL}/users')
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertIn('users', data)
self.assertIsInstance(data['users'], list)
def test_create_user(self):
"""测试创建用户"""
payload = {
'name': 'Test User',
'email': 'test@example.com'
}
response = self.session.post(
f'{self.BASE_URL}/users',
json=payload
)
self.assertEqual(response.status_code, 201)
data = response.json()
self.assertEqual(data['name'], payload['name'])
def test_invalid_request(self):
"""测试无效请求"""
response = self.session.post(
f'{self.BASE_URL}/users',
json={} # 缺少必填字段
)
self.assertEqual(response.status_code, 400)
if __name__ == '__main__':
unittest.main()
根据请求参数生成 cURL 命令:
# GET 请求
curl -X GET 'https://api.example.com/users?page=1&limit=10' \
-H 'Authorization: Bearer token' \
-H 'Content-Type: application/json'
# POST 请求
curl -X POST 'https://api.example.com/users' \
-H 'Authorization: Bearer token' \
-H 'Content-Type: application/json' \
-d '{"name": "张三", "email": "zhangsan@example.com"}'
# 文件上传
curl -X POST 'https://api.example.com/upload' \
-H 'Authorization: Bearer token' \
-F 'file=@/path/to/file.pdf' \
-F 'description=文件描述'
测试结果应包含:
## API 测试报告
### 请求信息
- URL: [完整 URL]
- Method: [HTTP 方法]
- Headers: [请求头]
- Body: [请求体]
### 响应信息
- Status: [状态码] [状态文本]
- Time: [响应时间]
- Headers: [响应头]
- Body:
```json
[响应体]
[可复制的 cURL 命令]
## 常见问题排查
1. **401 Unauthorized**: 检查 Token 是否有效
2. **403 Forbidden**: 检查权限配置
3. **404 Not Found**: 检查 URL 路径
4. **500 Internal Server Error**: 查看服务器日志
5. **超时**: 检查网络连接和服务器状态