mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-15 22:06:52 +08:00
chore: refactor the http executor node (#5212)
This commit is contained in:
36
api/tests/integration_tests/tools/__mock/http.py
Normal file
36
api/tests/integration_tests/tools/__mock/http.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import json
|
||||
from typing import Literal
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
||||
|
||||
class MockedHttp:
|
||||
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', None)
|
||||
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: MonkeyPatch):
|
||||
monkeypatch.setattr(httpx, "request", MockedHttp.httpx_request)
|
||||
yield
|
||||
monkeypatch.undo()
|
||||
39
api/tests/integration_tests/tools/api_tool/test_api_tool.py
Normal file
39
api/tests/integration_tests/tools/api_tool/test_api_tool.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from core.tools.tool.api_tool import ApiTool
|
||||
from core.tools.tool.tool import Tool
|
||||
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(api_bundle=tool_bundle, runtime=Tool.Runtime(credentials={'auth_type': 'none'}))
|
||||
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 '/p_param' == response.request.url.path
|
||||
assert b'query_param=q_param' == response.request.url.query
|
||||
assert 'h_param' == response.request.headers.get('header_param')
|
||||
assert 'application/json' == response.request.headers.get('content-type')
|
||||
assert 'cookie_param=c_param' == response.request.headers.get('cookie')
|
||||
assert 'b_param' in response.content.decode()
|
||||
Reference in New Issue
Block a user