This commit is contained in:
2025-12-01 17:21:38 +08:00
parent 32fee2b8ab
commit fab8c13cb3
7511 changed files with 996300 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
import json
from typing import Literal
import httpx
import pytest
from core.helper import ssrf_proxy
class MockedHttp:
@staticmethod
def httpx_request(
method: Literal["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"], url: str, **kwargs
) -> httpx.Response:
"""
Mocked httpx.request
"""
request = httpx.Request(
method, url, params=kwargs.get("params"), headers=kwargs.get("headers"), cookies=kwargs.get("cookies")
)
data = kwargs.get("data")
resp = json.dumps(data).encode("utf-8") if data else b"OK"
response = httpx.Response(
status_code=200,
request=request,
content=resp,
)
return response
@pytest.fixture
def setup_http_mock(request, monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(ssrf_proxy, "make_request", MockedHttp.httpx_request)
yield
monkeypatch.undo()

View File

@@ -0,0 +1,40 @@
from flask import Flask, request
from flask_restx import Api, Resource
app = Flask(__name__)
api = Api(app)
# Mock data
todos_data = {
"global": ["Buy groceries", "Finish project"],
"user1": ["Go for a run", "Read a book"],
}
class TodosResource(Resource):
def get(self, username):
todos = todos_data.get(username, [])
return {"todos": todos}
def post(self, username):
data = request.get_json()
new_todo = data.get("todo")
todos_data.setdefault(username, []).append(new_todo)
return {"message": "Todo added successfully"}
def delete(self, username):
data = request.get_json()
todo_idx = data.get("todo_idx")
todos = todos_data.get(username, [])
if 0 <= todo_idx < len(todos):
del todos[todo_idx]
return {"message": "Todo deleted successfully"}
return {"error": "Invalid todo index"}, 400
api.add_resource(TodosResource, "/todos/<string:username>")
if __name__ == "__main__":
app.run(port=5003, debug=True)

View File

@@ -0,0 +1,52 @@
from core.tools.__base.tool_runtime import ToolRuntime
from core.tools.custom_tool.tool import ApiTool
from core.tools.entities.common_entities import I18nObject
from core.tools.entities.tool_bundle import ApiToolBundle
from core.tools.entities.tool_entities import ToolEntity, ToolIdentity
from tests.integration_tests.tools.__mock.http import setup_http_mock
tool_bundle = {
"server_url": "http://www.example.com/{path_param}",
"method": "post",
"author": "",
"openapi": {
"parameters": [
{"in": "path", "name": "path_param"},
{"in": "query", "name": "query_param"},
{"in": "cookie", "name": "cookie_param"},
{"in": "header", "name": "header_param"},
],
"requestBody": {
"content": {"application/json": {"schema": {"properties": {"body_param": {"type": "string"}}}}}
},
},
"parameters": [],
}
parameters = {
"path_param": "p_param",
"query_param": "q_param",
"cookie_param": "c_param",
"header_param": "h_param",
"body_param": "b_param",
}
def test_api_tool(setup_http_mock):
tool = ApiTool(
entity=ToolEntity(
identity=ToolIdentity(provider="", author="", name="", label=I18nObject(en_US="test tool")),
),
api_bundle=ApiToolBundle.model_validate(tool_bundle),
runtime=ToolRuntime(tenant_id="", credentials={"auth_type": "none"}),
provider_id="test_tool",
)
headers = tool.assembling_request(parameters)
response = tool.do_http_request(tool.api_bundle.server_url, tool.api_bundle.method, headers, parameters)
assert response.status_code == 200
assert response.request.url.path == "/p_param"
assert response.request.url.query == b"query_param=q_param"
assert response.request.headers.get("header_param") == "h_param"
assert response.request.headers.get("content-type") == "application/json"
assert response.request.headers.get("cookie") == "cookie_param=c_param"
assert "b_param" in response.content.decode()