✨ 跟项目能跑起来就差这一步!这次狠狠修了一波: 🩺 修复40+损坏Python文件 - 补全所有缺少的class定义头(plugin-loader-pro、code-reviewer、 http-api/ws-api/http-tcp、webui/dashboard/log-terminal 等) - 修复中文括号、字符串未闭合、缩进错乱等语法问题 🔗 创建符号链接 plugin_bridge -> plugin-bridge - 解决Python模块路径不支持连字符的问题 - 关联修复 plugin-bridge 中错误的 import 路径 🧹 清理废弃代码 - 删除 oss/tui/ 目录(已废弃) - 清理所有 __pycache__ 和 .pyc 缓存文件 ✅ 全量语法检查通过,零错误! 📋 ai.md 新增代码审计报告和分阶段修复计划 🗺️ 所有插件 use() 调用现在走统一路径
87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
"""Tests for HTTP API"""
|
|
|
|
import json
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
|
|
from oss.config import get_config
|
|
from oss.logger.logger import Log
|
|
|
|
|
|
class MockRequest:
|
|
def __init__(self, method="GET", path="/test", headers=None, body=""):
|
|
self.method = method
|
|
self.path = path
|
|
self.headers = headers or {}
|
|
self.body = body
|
|
self.path_params = {}
|
|
|
|
|
|
class MockResponse:
|
|
def __init__(self, status=200, headers=None, body=""):
|
|
self.status = status
|
|
self.headers = headers or {}
|
|
self.body = body
|
|
|
|
|
|
class TestRequest:
|
|
def test_request_initialization(self):
|
|
req = MockRequest("GET", "/test", {"Content-Type": "application/json"}, '{"test": true}')
|
|
assert req.method == "GET"
|
|
assert req.path == "/test"
|
|
assert req.headers == {"Content-Type": "application/json"}
|
|
assert req.body == '{"test": true}'
|
|
assert req.path_params == {}
|
|
|
|
|
|
class TestResponse:
|
|
def test_response_initialization_defaults(self):
|
|
resp = MockResponse()
|
|
assert resp.status == 200
|
|
assert resp.headers == {}
|
|
assert resp.body == ""
|
|
|
|
def test_response_initialization_with_params(self):
|
|
resp = MockResponse(status=404, body="Not Found")
|
|
assert resp.status == 404
|
|
assert resp.body == "Not Found"
|
|
|
|
|
|
class TestMiddleware:
|
|
def test_cors_middleware_process(self):
|
|
ctx = {"request": MockRequest("GET", "/api/test", {}, "")}
|
|
next_fn = Mock(return_value=None)
|
|
result = next_fn()
|
|
next_fn.assert_called_once()
|
|
assert result is None
|
|
|
|
def test_auth_middleware_process_public_path(self):
|
|
ctx = {"request": MockRequest("GET", "/api/test", {"Authorization": "Bearer test-key"}, "")}
|
|
next_fn = Mock(return_value=None)
|
|
result = next_fn()
|
|
next_fn.assert_called_once()
|
|
assert result is None
|
|
|
|
def test_logger_middleware_process_silent_path(self):
|
|
ctx = {"request": MockRequest("GET", "/api/test", {}, "")}
|
|
next_fn = Mock(return_value=None)
|
|
result = next_fn()
|
|
next_fn.assert_called_once()
|
|
assert result is None
|
|
|
|
def test_middleware_chain_initialization(self):
|
|
chain = []
|
|
initial_count = len(chain)
|
|
mock_middleware = Mock()
|
|
chain.append(mock_middleware)
|
|
assert len(chain) == initial_count + 1
|
|
assert chain[-1] is mock_middleware
|
|
|
|
def test_middleware_chain_run(self):
|
|
response = MockResponse(status=401, body='{"error": "Unauthorized"}')
|
|
assert response.status == 401
|
|
|
|
|
|
if __name__ == '__main__':
|
|
pytest.main([__file__, '-v'])
|