mirror of
https://github.com/langgenius/dify.git
synced 2026-04-27 05:58:14 +08:00
feat: http
This commit is contained in:
82
api/tests/integration_tests/workflow/nodes/__mock/http.py
Normal file
82
api/tests/integration_tests/workflow/nodes/__mock/http.py
Normal file
@ -0,0 +1,82 @@
|
||||
import os
|
||||
import pytest
|
||||
import requests.api as requests
|
||||
import httpx._api as httpx
|
||||
from requests import Response as RequestsResponse
|
||||
from yarl import URL
|
||||
|
||||
from typing import Literal
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
from json import dumps
|
||||
|
||||
MOCK = os.getenv('MOCK_SWITCH', 'false') == 'true'
|
||||
|
||||
class MockedHttp:
|
||||
def requests_request(self, method: Literal['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
|
||||
url: str, **kwargs) -> RequestsResponse:
|
||||
"""
|
||||
Mocked requests.request
|
||||
"""
|
||||
response = RequestsResponse()
|
||||
response.url = str(URL(url) % kwargs.get('params', {}))
|
||||
response.headers = kwargs.get('headers', {})
|
||||
|
||||
if url == 'http://404.com':
|
||||
response.status_code = 404
|
||||
response._content = b'Not Found'
|
||||
return response
|
||||
|
||||
# get data, files
|
||||
data = kwargs.get('data', None)
|
||||
files = kwargs.get('files', None)
|
||||
|
||||
if data is not None:
|
||||
resp = dumps(data).encode('utf-8')
|
||||
if files is not None:
|
||||
resp = dumps(files).encode('utf-8')
|
||||
else:
|
||||
resp = b'OK'
|
||||
|
||||
response.status_code = 200
|
||||
response._content = resp
|
||||
return response
|
||||
|
||||
def httpx_request(self, method: Literal['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
|
||||
url: str, **kwargs) -> httpx.Response:
|
||||
"""
|
||||
Mocked httpx.request
|
||||
"""
|
||||
response = httpx.Response()
|
||||
response.url = str(URL(url) % kwargs.get('params', {}))
|
||||
response.headers = kwargs.get('headers', {})
|
||||
|
||||
if url == 'http://404.com':
|
||||
response.status_code = 404
|
||||
response.content = b'Not Found'
|
||||
return response
|
||||
|
||||
# get data, files
|
||||
data = kwargs.get('data', None)
|
||||
files = kwargs.get('files', None)
|
||||
|
||||
if data is not None:
|
||||
resp = dumps(data).encode('utf-8')
|
||||
if files is not None:
|
||||
resp = dumps(files).encode('utf-8')
|
||||
else:
|
||||
resp = b'OK'
|
||||
|
||||
response.status_code = 200
|
||||
response.content = resp
|
||||
return response
|
||||
|
||||
@pytest.fixture
|
||||
def setup_http_mock(request, monkeypatch: MonkeyPatch):
|
||||
if not MOCK:
|
||||
yield
|
||||
return
|
||||
|
||||
monkeypatch.setattr(requests, "request", MockedHttp.requests_request)
|
||||
monkeypatch.setattr(httpx, "request", MockedHttp.httpx_request)
|
||||
yield
|
||||
monkeypatch.undo()
|
||||
51
api/tests/integration_tests/workflow/nodes/test_http.py
Normal file
51
api/tests/integration_tests/workflow/nodes/test_http.py
Normal file
@ -0,0 +1,51 @@
|
||||
from calendar import c
|
||||
import pytest
|
||||
from core.app.entities.app_invoke_entities import InvokeFrom
|
||||
from core.workflow.entities.variable_pool import VariablePool
|
||||
from core.workflow.nodes.http_request.entities import HttpRequestNodeData
|
||||
from core.workflow.nodes.http_request.http_request_node import HttpRequestNode
|
||||
|
||||
from tests.integration_tests.workflow.nodes.__mock.http import setup_http_mock
|
||||
|
||||
BASIC_NODE_DATA = {
|
||||
'tenant_id': '1',
|
||||
'app_id': '1',
|
||||
'workflow_id': '1',
|
||||
'user_id': '1',
|
||||
'user_from': InvokeFrom.WEB_APP,
|
||||
}
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={}, user_inputs={})
|
||||
pool.append_variable(node_id='1', variable_key_list=['123', 'args1'], value=1)
|
||||
pool.append_variable(node_id='1', variable_key_list=['123', 'args2'], value=2)
|
||||
|
||||
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
|
||||
def test_get_param(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'variables': [],
|
||||
'method': 'get',
|
||||
'url': 'http://example.com',
|
||||
'authorization': {
|
||||
'type': 'api-key',
|
||||
'config': {
|
||||
'type': 'basic',
|
||||
'api_key':'ak-xxx',
|
||||
'header': 'api-key',
|
||||
}
|
||||
},
|
||||
'headers': '',
|
||||
'params': '',
|
||||
'body': None,
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
print(result)
|
||||
|
||||
assert 1==2
|
||||
Reference in New Issue
Block a user