From 3c3571713ea0b00f4662ba33c247e910e8da0d4b Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Thu, 14 Mar 2024 11:35:51 +0800 Subject: [PATCH] fix: http --- .../workflow/nodes/http_request/entities.py | 2 +- .../nodes/http_request/http_executor.py | 6 +- .../nodes/http_request/http_request_node.py | 2 +- .../workflow/nodes/test_http.py | 74 +++++++++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) diff --git a/api/core/workflow/nodes/http_request/entities.py b/api/core/workflow/nodes/http_request/entities.py index fbd4da3840..0683008954 100644 --- a/api/core/workflow/nodes/http_request/entities.py +++ b/api/core/workflow/nodes/http_request/entities.py @@ -33,7 +33,7 @@ class HttpRequestNodeData(BaseNodeData): return v class Body(BaseModel): - type: Literal[None, 'form-data', 'x-www-form-urlencoded', 'raw', 'json'] + type: Literal['none', 'form-data', 'x-www-form-urlencoded', 'raw', 'json'] data: Union[None, str] variables: list[VariableSelector] diff --git a/api/core/workflow/nodes/http_request/http_executor.py b/api/core/workflow/nodes/http_request/http_executor.py index c96d5f07d1..3d307be0d1 100644 --- a/api/core/workflow/nodes/http_request/http_executor.py +++ b/api/core/workflow/nodes/http_request/http_executor.py @@ -131,8 +131,6 @@ class HttpExecutor: self.headers['Content-Type'] = 'application/json' elif node_data.body.type == 'x-www-form-urlencoded': self.headers['Content-Type'] = 'application/x-www-form-urlencoded' - # elif node_data.body.type == 'form-data': - # self.headers['Content-Type'] = 'multipart/form-data' if node_data.body.type in ['form-data', 'x-www-form-urlencoded']: body = {} @@ -152,8 +150,10 @@ class HttpExecutor: } else: self.body = urlencode(body) - else: + elif node_data.body.type in ['json', 'raw']: self.body = original_body + elif node_data.body.type == 'none': + self.body = '' def _assembling_headers(self) -> dict[str, Any]: authorization = deepcopy(self.authorization) diff --git a/api/core/workflow/nodes/http_request/http_request_node.py b/api/core/workflow/nodes/http_request/http_request_node.py index c83e331fa8..a914ae13ff 100644 --- a/api/core/workflow/nodes/http_request/http_request_node.py +++ b/api/core/workflow/nodes/http_request/http_request_node.py @@ -42,7 +42,7 @@ class HttpRequestNode(BaseNode): inputs=variables, outputs={ 'status_code': response.status_code, - 'body': response, + 'body': response.body, 'headers': response.headers }, process_data={ diff --git a/api/tests/integration_tests/workflow/nodes/test_http.py b/api/tests/integration_tests/workflow/nodes/test_http.py index 584e1d80a5..8b94105b44 100644 --- a/api/tests/integration_tests/workflow/nodes/test_http.py +++ b/api/tests/integration_tests/workflow/nodes/test_http.py @@ -84,6 +84,41 @@ 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={ + 'id': '1', + 'data': { + 'title': 'http', + 'desc': '', + 'variables': [{ + 'variable': 'args1', + 'value_selector': ['1', '123', 'args1'], + }], + 'method': 'get', + 'url': 'http://example.com', + 'authorization': { + 'type': 'api-key', + 'config': { + 'type': 'custom', + 'api_key': 'Auth', + 'header': 'X-Auth', + }, + }, + 'headers': 'X-Header:123', + 'params': 'A:b', + 'body': None, + } + }, **BASIC_NODE_DATA) + + result = node.run(pool) + + data = result.process_data.get('request', '') + + assert '?A=b' in data + 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={ @@ -237,3 +272,42 @@ def test_form_data(setup_http_mock): assert '2' in data 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', + 'data': { + 'title': 'http', + 'desc': '', + 'variables': [{ + 'variable': 'args1', + 'value_selector': ['1', '123', 'args1'], + }, { + 'variable': 'args2', + 'value_selector': ['1', '123', 'args2'], + }], + 'method': 'post', + 'url': 'http://example.com', + 'authorization': { + 'type': 'api-key', + 'config': { + 'type': 'basic', + 'api_key':'ak-xxx', + 'header': 'api-key', + } + }, + 'headers': 'X-Header:123', + 'params': 'A:b', + 'body': { + 'type': 'none', + 'data': '123123123' + }, + } + }, **BASIC_NODE_DATA) + + result = node.run(pool) + data = result.process_data.get('request', '') + + assert 'api-key: Basic ak-xxx' in data + assert 'X-Header: 123' in data + assert '123123123' not in data \ No newline at end of file