chore: refactor the http executor node (#5212)
This commit is contained in:
@@ -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()
|
||||
|
@@ -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')
|
||||
|
Reference in New Issue
Block a user