mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-21 02:45:36 +00:00
update:说明文档添加测试用例
This commit is contained in:
@@ -107,3 +107,114 @@ context_providers:
|
||||
```
|
||||
|
||||
**注意**:如果您不需要使用此功能,可以选择**不配置任何上下文源**,也可以从提示词模板文件中**删除** `{{ dynamic_context }}` 占位符。
|
||||
|
||||
## 附录:Mock 测试服务示例
|
||||
|
||||
为了方便您测试和开发,我们提供了一个简单的 Python Mock Server 脚本。您可以运行此脚本在本地模拟 API 接口。
|
||||
|
||||
**mock_api_server.py**
|
||||
|
||||
```python
|
||||
import http.server
|
||||
import socketserver
|
||||
import json
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
|
||||
# 设置端口号
|
||||
PORT = 8081
|
||||
|
||||
class MockRequestHandler(http.server.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
# 解析路径和参数
|
||||
parsed_path = urlparse(self.path)
|
||||
path = parsed_path.path
|
||||
query = parse_qs(parsed_path.query)
|
||||
|
||||
response_data = {}
|
||||
status_code = 200
|
||||
|
||||
print(f"收到请求: {path}, 参数: {query}")
|
||||
|
||||
# Case 1: 模拟健康数据 (返回字典 Dict)
|
||||
# 路径参数风格: /health
|
||||
# device_id 从 Header 获取
|
||||
if path == "/health":
|
||||
device_id = self.headers.get("device_id", "unknown_device")
|
||||
print(f"device_id: {device_id}")
|
||||
response_data = {
|
||||
"code": 0,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"测试设备ID": device_id,
|
||||
"心率": "80 bpm",
|
||||
"血压": "120/80 mmHg",
|
||||
"状态": "良好"
|
||||
}
|
||||
}
|
||||
|
||||
# Case 2: 模拟新闻列表 (返回列表 List)
|
||||
# 无参数: /news/list
|
||||
elif path == "/news/list":
|
||||
response_data = {
|
||||
"code": 0,
|
||||
"msg": "success",
|
||||
"data": [
|
||||
"今日头条:Python 3.14 发布",
|
||||
"科技新闻:AI 助手改变生活",
|
||||
"本地新闻:明日有大雨,记得带伞"
|
||||
]
|
||||
}
|
||||
|
||||
# Case 3: 模拟天气简报 (返回字符串 String)
|
||||
# 无参数: /weather/simple
|
||||
elif path == "/weather/simple":
|
||||
response_data = {
|
||||
"code": 0,
|
||||
"msg": "success",
|
||||
"data": "今日晴转多云,气温 20-25 度,空气质量优,适合出行。"
|
||||
}
|
||||
|
||||
# Case 4: 模拟设备详情 (Query参数风格)
|
||||
# 参数风格: /device/info
|
||||
# device_id 从 Header 获取
|
||||
elif path == "/device/info":
|
||||
device_id = self.headers.get("device_id", "unknown_device")
|
||||
response_data = {
|
||||
"code": 0,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"查询方式": "Header参数",
|
||||
"设备ID": device_id,
|
||||
"电量": "85%",
|
||||
"固件": "v2.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
# Case 5: 404 Not Found
|
||||
else:
|
||||
status_code = 404
|
||||
response_data = {"error": "接口不存在"}
|
||||
|
||||
# 发送响应
|
||||
self.send_response(status_code)
|
||||
self.send_header('Content-type', 'application/json; charset=utf-8')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps(response_data, ensure_ascii=False).encode('utf-8'))
|
||||
|
||||
# 启动服务
|
||||
# 允许地址重用,防止快速重启报错
|
||||
socketserver.TCPServer.allow_reuse_address = True
|
||||
with socketserver.TCPServer(("", PORT), MockRequestHandler) as httpd:
|
||||
print(f"==================================================")
|
||||
print(f"Mock API Server 已启动: http://localhost:{PORT}")
|
||||
print(f"可用接口列表:")
|
||||
print(f"1. [字典] http://localhost:{PORT}/health")
|
||||
print(f"2. [列表] http://localhost:{PORT}/news/list")
|
||||
print(f"3. [文本] http://localhost:{PORT}/weather/simple")
|
||||
print(f"4. [参数] http://localhost:{PORT}/device/info")
|
||||
print(f"==================================================")
|
||||
try:
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
print("\n服务已停止")
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user