chore: refactor the http executor node (#5212)

This commit is contained in:
非法操作
2024-06-24 16:14:59 +08:00
committed by GitHub
parent 1e28a8c033
commit f7900f298f
8 changed files with 249 additions and 230 deletions

View 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()

View 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()

View File

@@ -2,84 +2,52 @@ import os
from json import dumps
from typing import Literal
import httpx._api as httpx
import httpx
import pytest
import requests.api as requests
from _pytest.monkeypatch import MonkeyPatch
from httpx import Request as HttpxRequest
from requests import Response as RequestsResponse
from yarl import URL
MOCK = os.getenv('MOCK_SWITCH', 'false') == 'true'
class MockedHttp:
def requests_request(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(method: Literal['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
def httpx_request(method: Literal['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'],
url: str, **kwargs) -> httpx.Response:
"""
Mocked httpx.request
"""
response = httpx.Response(
status_code=200,
request=HttpxRequest(method, url)
)
response.headers = kwargs.get('headers', {})
if url == 'http://404.com':
response.status_code = 404
response.content = b'Not Found'
response = httpx.Response(
status_code=404,
request=httpx.Request(method, url),
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:
elif files is not None:
resp = dumps(files).encode('utf-8')
else:
resp = b'OK'
response.status_code = 200
response._content = resp
response = httpx.Response(
status_code=200,
request=httpx.Request(method, url),
headers=kwargs.get('headers', {}),
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()
monkeypatch.undo()

View File

@@ -1,3 +1,5 @@
from urllib.parse import urlencode
import pytest
from core.app.entities.app_invoke_entities import InvokeFrom
@@ -20,6 +22,7 @@ pool = VariablePool(system_variables={}, user_inputs={})
pool.append_variable(node_id='a', variable_key_list=['b123', 'args1'], value=1)
pool.append_variable(node_id='a', variable_key_list=['b123', 'args2'], value=2)
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
def test_get(setup_http_mock):
node = HttpRequestNode(config={
@@ -33,7 +36,7 @@ def test_get(setup_http_mock):
'type': 'api-key',
'config': {
'type': 'basic',
'api_key':'ak-xxx',
'api_key': 'ak-xxx',
'header': 'api-key',
}
},
@@ -52,6 +55,7 @@ def test_get(setup_http_mock):
assert 'api-key: Basic ak-xxx' in data
assert 'X-Header: 123' in data
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
def test_no_auth(setup_http_mock):
node = HttpRequestNode(config={
@@ -78,6 +82,7 @@ def test_no_auth(setup_http_mock):
assert '?A=b' in data
assert 'X-Header: 123' in data
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
def test_custom_authorization_header(setup_http_mock):
node = HttpRequestNode(config={
@@ -110,6 +115,7 @@ def test_custom_authorization_header(setup_http_mock):
assert 'X-Header: 123' in data
assert 'X-Auth: Auth' in data
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
def test_template(setup_http_mock):
node = HttpRequestNode(config={
@@ -123,7 +129,7 @@ def test_template(setup_http_mock):
'type': 'api-key',
'config': {
'type': 'basic',
'api_key':'ak-xxx',
'api_key': 'ak-xxx',
'header': 'api-key',
}
},
@@ -143,6 +149,7 @@ def test_template(setup_http_mock):
assert 'X-Header: 123' in data
assert 'X-Header2: 2' in data
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
def test_json(setup_http_mock):
node = HttpRequestNode(config={
@@ -156,7 +163,7 @@ def test_json(setup_http_mock):
'type': 'api-key',
'config': {
'type': 'basic',
'api_key':'ak-xxx',
'api_key': 'ak-xxx',
'header': 'api-key',
}
},
@@ -177,6 +184,7 @@ def test_json(setup_http_mock):
assert 'api-key: Basic ak-xxx' in data
assert 'X-Header: 123' in data
def test_x_www_form_urlencoded(setup_http_mock):
node = HttpRequestNode(config={
'id': '1',
@@ -189,7 +197,7 @@ def test_x_www_form_urlencoded(setup_http_mock):
'type': 'api-key',
'config': {
'type': 'basic',
'api_key':'ak-xxx',
'api_key': 'ak-xxx',
'header': 'api-key',
}
},
@@ -210,6 +218,7 @@ def test_x_www_form_urlencoded(setup_http_mock):
assert 'api-key: Basic ak-xxx' in data
assert 'X-Header: 123' in data
def test_form_data(setup_http_mock):
node = HttpRequestNode(config={
'id': '1',
@@ -222,7 +231,7 @@ def test_form_data(setup_http_mock):
'type': 'api-key',
'config': {
'type': 'basic',
'api_key':'ak-xxx',
'api_key': 'ak-xxx',
'header': 'api-key',
}
},
@@ -246,6 +255,7 @@ def test_form_data(setup_http_mock):
assert 'api-key: Basic ak-xxx' in data
assert 'X-Header: 123' in data
def test_none_data(setup_http_mock):
node = HttpRequestNode(config={
'id': '1',
@@ -258,7 +268,7 @@ def test_none_data(setup_http_mock):
'type': 'api-key',
'config': {
'type': 'basic',
'api_key':'ak-xxx',
'api_key': 'ak-xxx',
'header': 'api-key',
}
},
@@ -278,3 +288,59 @@ def test_none_data(setup_http_mock):
assert 'api-key: Basic ak-xxx' in data
assert 'X-Header: 123' in data
assert '123123123' not in data
def test_mock_404(setup_http_mock):
node = HttpRequestNode(config={
'id': '1',
'data': {
'title': 'http',
'desc': '',
'method': 'get',
'url': 'http://404.com',
'authorization': {
'type': 'no-auth',
'config': None,
},
'body': None,
'params': '',
'headers': 'X-Header:123',
'mask_authorization_header': False,
}
}, **BASIC_NODE_DATA)
result = node.run(pool)
resp = result.outputs
assert 404 == resp.get('status_code')
assert 'Not Found' in resp.get('body')
def test_multi_colons_parse(setup_http_mock):
node = HttpRequestNode(config={
'id': '1',
'data': {
'title': 'http',
'desc': '',
'method': 'get',
'url': 'http://example.com',
'authorization': {
'type': 'no-auth',
'config': None,
},
'params': 'Referer:http://example1.com\nRedirect:http://example2.com',
'headers': 'Referer:http://example3.com\nRedirect:http://example4.com',
'body': {
'type': 'form-data',
'data': 'Referer:http://example5.com\nRedirect:http://example6.com'
},
'mask_authorization_header': False,
}
}, **BASIC_NODE_DATA)
result = node.run(pool)
resp = result.outputs
assert urlencode({'Redirect': 'http://example2.com'}) in result.process_data.get('request')
assert 'form-data; name="Redirect"\n\nhttp://example6.com' in result.process_data.get('request')
assert 'http://example3.com' == resp.get('headers').get('referer')