mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-06-04 11:14:10 +08:00
fix(http_request): improve parameter initialization and reorganize tests (#10297)
This commit is contained in:
parent
68e0b0ac84
commit
ae254f0a10
@ -88,8 +88,10 @@ class Executor:
|
|||||||
self.url = self.variable_pool.convert_template(self.node_data.url).text
|
self.url = self.variable_pool.convert_template(self.node_data.url).text
|
||||||
|
|
||||||
def _init_params(self):
|
def _init_params(self):
|
||||||
params = self.variable_pool.convert_template(self.node_data.params).text
|
params = _plain_text_to_dict(self.node_data.params)
|
||||||
self.params = _plain_text_to_dict(params)
|
for key in params:
|
||||||
|
params[key] = self.variable_pool.convert_template(params[key]).text
|
||||||
|
self.params = params
|
||||||
|
|
||||||
def _init_headers(self):
|
def _init_headers(self):
|
||||||
headers = self.variable_pool.convert_template(self.node_data.headers).text
|
headers = self.variable_pool.convert_template(self.node_data.headers).text
|
||||||
|
@ -0,0 +1,198 @@
|
|||||||
|
from core.workflow.entities.variable_pool import VariablePool
|
||||||
|
from core.workflow.nodes.http_request import (
|
||||||
|
BodyData,
|
||||||
|
HttpRequestNodeAuthorization,
|
||||||
|
HttpRequestNodeBody,
|
||||||
|
HttpRequestNodeData,
|
||||||
|
)
|
||||||
|
from core.workflow.nodes.http_request.entities import HttpRequestNodeTimeout
|
||||||
|
from core.workflow.nodes.http_request.executor import Executor
|
||||||
|
|
||||||
|
|
||||||
|
def test_executor_with_json_body_and_number_variable():
|
||||||
|
# Prepare the variable pool
|
||||||
|
variable_pool = VariablePool(
|
||||||
|
system_variables={},
|
||||||
|
user_inputs={},
|
||||||
|
)
|
||||||
|
variable_pool.add(["pre_node_id", "number"], 42)
|
||||||
|
|
||||||
|
# Prepare the node data
|
||||||
|
node_data = HttpRequestNodeData(
|
||||||
|
title="Test JSON Body with Number Variable",
|
||||||
|
method="post",
|
||||||
|
url="https://api.example.com/data",
|
||||||
|
authorization=HttpRequestNodeAuthorization(type="no-auth"),
|
||||||
|
headers="Content-Type: application/json",
|
||||||
|
params="",
|
||||||
|
body=HttpRequestNodeBody(
|
||||||
|
type="json",
|
||||||
|
data=[
|
||||||
|
BodyData(
|
||||||
|
key="",
|
||||||
|
type="text",
|
||||||
|
value='{"number": {{#pre_node_id.number#}}}',
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Initialize the Executor
|
||||||
|
executor = Executor(
|
||||||
|
node_data=node_data,
|
||||||
|
timeout=HttpRequestNodeTimeout(connect=10, read=30, write=30),
|
||||||
|
variable_pool=variable_pool,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check the executor's data
|
||||||
|
assert executor.method == "post"
|
||||||
|
assert executor.url == "https://api.example.com/data"
|
||||||
|
assert executor.headers == {"Content-Type": "application/json"}
|
||||||
|
assert executor.params == {}
|
||||||
|
assert executor.json == {"number": 42}
|
||||||
|
assert executor.data is None
|
||||||
|
assert executor.files is None
|
||||||
|
assert executor.content is None
|
||||||
|
|
||||||
|
# Check the raw request (to_log method)
|
||||||
|
raw_request = executor.to_log()
|
||||||
|
assert "POST /data HTTP/1.1" in raw_request
|
||||||
|
assert "Host: api.example.com" in raw_request
|
||||||
|
assert "Content-Type: application/json" in raw_request
|
||||||
|
assert '{"number": 42}' in raw_request
|
||||||
|
|
||||||
|
|
||||||
|
def test_executor_with_json_body_and_object_variable():
|
||||||
|
# Prepare the variable pool
|
||||||
|
variable_pool = VariablePool(
|
||||||
|
system_variables={},
|
||||||
|
user_inputs={},
|
||||||
|
)
|
||||||
|
variable_pool.add(["pre_node_id", "object"], {"name": "John Doe", "age": 30, "email": "john@example.com"})
|
||||||
|
|
||||||
|
# Prepare the node data
|
||||||
|
node_data = HttpRequestNodeData(
|
||||||
|
title="Test JSON Body with Object Variable",
|
||||||
|
method="post",
|
||||||
|
url="https://api.example.com/data",
|
||||||
|
authorization=HttpRequestNodeAuthorization(type="no-auth"),
|
||||||
|
headers="Content-Type: application/json",
|
||||||
|
params="",
|
||||||
|
body=HttpRequestNodeBody(
|
||||||
|
type="json",
|
||||||
|
data=[
|
||||||
|
BodyData(
|
||||||
|
key="",
|
||||||
|
type="text",
|
||||||
|
value="{{#pre_node_id.object#}}",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Initialize the Executor
|
||||||
|
executor = Executor(
|
||||||
|
node_data=node_data,
|
||||||
|
timeout=HttpRequestNodeTimeout(connect=10, read=30, write=30),
|
||||||
|
variable_pool=variable_pool,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check the executor's data
|
||||||
|
assert executor.method == "post"
|
||||||
|
assert executor.url == "https://api.example.com/data"
|
||||||
|
assert executor.headers == {"Content-Type": "application/json"}
|
||||||
|
assert executor.params == {}
|
||||||
|
assert executor.json == {"name": "John Doe", "age": 30, "email": "john@example.com"}
|
||||||
|
assert executor.data is None
|
||||||
|
assert executor.files is None
|
||||||
|
assert executor.content is None
|
||||||
|
|
||||||
|
# Check the raw request (to_log method)
|
||||||
|
raw_request = executor.to_log()
|
||||||
|
assert "POST /data HTTP/1.1" in raw_request
|
||||||
|
assert "Host: api.example.com" in raw_request
|
||||||
|
assert "Content-Type: application/json" in raw_request
|
||||||
|
assert '"name": "John Doe"' in raw_request
|
||||||
|
assert '"age": 30' in raw_request
|
||||||
|
assert '"email": "john@example.com"' in raw_request
|
||||||
|
|
||||||
|
|
||||||
|
def test_executor_with_json_body_and_nested_object_variable():
|
||||||
|
# Prepare the variable pool
|
||||||
|
variable_pool = VariablePool(
|
||||||
|
system_variables={},
|
||||||
|
user_inputs={},
|
||||||
|
)
|
||||||
|
variable_pool.add(["pre_node_id", "object"], {"name": "John Doe", "age": 30, "email": "john@example.com"})
|
||||||
|
|
||||||
|
# Prepare the node data
|
||||||
|
node_data = HttpRequestNodeData(
|
||||||
|
title="Test JSON Body with Nested Object Variable",
|
||||||
|
method="post",
|
||||||
|
url="https://api.example.com/data",
|
||||||
|
authorization=HttpRequestNodeAuthorization(type="no-auth"),
|
||||||
|
headers="Content-Type: application/json",
|
||||||
|
params="",
|
||||||
|
body=HttpRequestNodeBody(
|
||||||
|
type="json",
|
||||||
|
data=[
|
||||||
|
BodyData(
|
||||||
|
key="",
|
||||||
|
type="text",
|
||||||
|
value='{"object": {{#pre_node_id.object#}}}',
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Initialize the Executor
|
||||||
|
executor = Executor(
|
||||||
|
node_data=node_data,
|
||||||
|
timeout=HttpRequestNodeTimeout(connect=10, read=30, write=30),
|
||||||
|
variable_pool=variable_pool,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check the executor's data
|
||||||
|
assert executor.method == "post"
|
||||||
|
assert executor.url == "https://api.example.com/data"
|
||||||
|
assert executor.headers == {"Content-Type": "application/json"}
|
||||||
|
assert executor.params == {}
|
||||||
|
assert executor.json == {"object": {"name": "John Doe", "age": 30, "email": "john@example.com"}}
|
||||||
|
assert executor.data is None
|
||||||
|
assert executor.files is None
|
||||||
|
assert executor.content is None
|
||||||
|
|
||||||
|
# Check the raw request (to_log method)
|
||||||
|
raw_request = executor.to_log()
|
||||||
|
assert "POST /data HTTP/1.1" in raw_request
|
||||||
|
assert "Host: api.example.com" in raw_request
|
||||||
|
assert "Content-Type: application/json" in raw_request
|
||||||
|
assert '"object": {' in raw_request
|
||||||
|
assert '"name": "John Doe"' in raw_request
|
||||||
|
assert '"age": 30' in raw_request
|
||||||
|
assert '"email": "john@example.com"' in raw_request
|
||||||
|
|
||||||
|
|
||||||
|
def test_extract_selectors_from_template_with_newline():
|
||||||
|
variable_pool = VariablePool()
|
||||||
|
variable_pool.add(("node_id", "custom_query"), "line1\nline2")
|
||||||
|
node_data = HttpRequestNodeData(
|
||||||
|
title="Test JSON Body with Nested Object Variable",
|
||||||
|
method="post",
|
||||||
|
url="https://api.example.com/data",
|
||||||
|
authorization=HttpRequestNodeAuthorization(type="no-auth"),
|
||||||
|
headers="Content-Type: application/json",
|
||||||
|
params="test: {{#node_id.custom_query#}}",
|
||||||
|
body=HttpRequestNodeBody(
|
||||||
|
type="none",
|
||||||
|
data=[],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
executor = Executor(
|
||||||
|
node_data=node_data,
|
||||||
|
timeout=HttpRequestNodeTimeout(connect=10, read=30, write=30),
|
||||||
|
variable_pool=variable_pool,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert executor.params == {"test": "line1\nline2"}
|
@ -1,5 +1,3 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
from core.app.entities.app_invoke_entities import InvokeFrom
|
from core.app.entities.app_invoke_entities import InvokeFrom
|
||||||
@ -16,8 +14,7 @@ from core.workflow.nodes.http_request import (
|
|||||||
HttpRequestNodeBody,
|
HttpRequestNodeBody,
|
||||||
HttpRequestNodeData,
|
HttpRequestNodeData,
|
||||||
)
|
)
|
||||||
from core.workflow.nodes.http_request.entities import HttpRequestNodeTimeout
|
from core.workflow.nodes.http_request.executor import _plain_text_to_dict
|
||||||
from core.workflow.nodes.http_request.executor import Executor, _plain_text_to_dict
|
|
||||||
from models.enums import UserFrom
|
from models.enums import UserFrom
|
||||||
from models.workflow import WorkflowNodeExecutionStatus, WorkflowType
|
from models.workflow import WorkflowNodeExecutionStatus, WorkflowType
|
||||||
|
|
||||||
@ -203,167 +200,3 @@ def test_http_request_node_form_with_file(monkeypatch):
|
|||||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||||
assert result.outputs is not None
|
assert result.outputs is not None
|
||||||
assert result.outputs["body"] == ""
|
assert result.outputs["body"] == ""
|
||||||
|
|
||||||
|
|
||||||
def test_executor_with_json_body_and_number_variable():
|
|
||||||
# Prepare the variable pool
|
|
||||||
variable_pool = VariablePool(
|
|
||||||
system_variables={},
|
|
||||||
user_inputs={},
|
|
||||||
)
|
|
||||||
variable_pool.add(["pre_node_id", "number"], 42)
|
|
||||||
|
|
||||||
# Prepare the node data
|
|
||||||
node_data = HttpRequestNodeData(
|
|
||||||
title="Test JSON Body with Number Variable",
|
|
||||||
method="post",
|
|
||||||
url="https://api.example.com/data",
|
|
||||||
authorization=HttpRequestNodeAuthorization(type="no-auth"),
|
|
||||||
headers="Content-Type: application/json",
|
|
||||||
params="",
|
|
||||||
body=HttpRequestNodeBody(
|
|
||||||
type="json",
|
|
||||||
data=[
|
|
||||||
BodyData(
|
|
||||||
key="",
|
|
||||||
type="text",
|
|
||||||
value='{"number": {{#pre_node_id.number#}}}',
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Initialize the Executor
|
|
||||||
executor = Executor(
|
|
||||||
node_data=node_data,
|
|
||||||
timeout=HttpRequestNodeTimeout(connect=10, read=30, write=30),
|
|
||||||
variable_pool=variable_pool,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Check the executor's data
|
|
||||||
assert executor.method == "post"
|
|
||||||
assert executor.url == "https://api.example.com/data"
|
|
||||||
assert executor.headers == {"Content-Type": "application/json"}
|
|
||||||
assert executor.params == {}
|
|
||||||
assert executor.json == {"number": 42}
|
|
||||||
assert executor.data is None
|
|
||||||
assert executor.files is None
|
|
||||||
assert executor.content is None
|
|
||||||
|
|
||||||
# Check the raw request (to_log method)
|
|
||||||
raw_request = executor.to_log()
|
|
||||||
assert "POST /data HTTP/1.1" in raw_request
|
|
||||||
assert "Host: api.example.com" in raw_request
|
|
||||||
assert "Content-Type: application/json" in raw_request
|
|
||||||
assert '{"number": 42}' in raw_request
|
|
||||||
|
|
||||||
|
|
||||||
def test_executor_with_json_body_and_object_variable():
|
|
||||||
# Prepare the variable pool
|
|
||||||
variable_pool = VariablePool(
|
|
||||||
system_variables={},
|
|
||||||
user_inputs={},
|
|
||||||
)
|
|
||||||
variable_pool.add(["pre_node_id", "object"], {"name": "John Doe", "age": 30, "email": "john@example.com"})
|
|
||||||
|
|
||||||
# Prepare the node data
|
|
||||||
node_data = HttpRequestNodeData(
|
|
||||||
title="Test JSON Body with Object Variable",
|
|
||||||
method="post",
|
|
||||||
url="https://api.example.com/data",
|
|
||||||
authorization=HttpRequestNodeAuthorization(type="no-auth"),
|
|
||||||
headers="Content-Type: application/json",
|
|
||||||
params="",
|
|
||||||
body=HttpRequestNodeBody(
|
|
||||||
type="json",
|
|
||||||
data=[
|
|
||||||
BodyData(
|
|
||||||
key="",
|
|
||||||
type="text",
|
|
||||||
value="{{#pre_node_id.object#}}",
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Initialize the Executor
|
|
||||||
executor = Executor(
|
|
||||||
node_data=node_data,
|
|
||||||
timeout=HttpRequestNodeTimeout(connect=10, read=30, write=30),
|
|
||||||
variable_pool=variable_pool,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Check the executor's data
|
|
||||||
assert executor.method == "post"
|
|
||||||
assert executor.url == "https://api.example.com/data"
|
|
||||||
assert executor.headers == {"Content-Type": "application/json"}
|
|
||||||
assert executor.params == {}
|
|
||||||
assert executor.json == {"name": "John Doe", "age": 30, "email": "john@example.com"}
|
|
||||||
assert executor.data is None
|
|
||||||
assert executor.files is None
|
|
||||||
assert executor.content is None
|
|
||||||
|
|
||||||
# Check the raw request (to_log method)
|
|
||||||
raw_request = executor.to_log()
|
|
||||||
assert "POST /data HTTP/1.1" in raw_request
|
|
||||||
assert "Host: api.example.com" in raw_request
|
|
||||||
assert "Content-Type: application/json" in raw_request
|
|
||||||
assert '"name": "John Doe"' in raw_request
|
|
||||||
assert '"age": 30' in raw_request
|
|
||||||
assert '"email": "john@example.com"' in raw_request
|
|
||||||
|
|
||||||
|
|
||||||
def test_executor_with_json_body_and_nested_object_variable():
|
|
||||||
# Prepare the variable pool
|
|
||||||
variable_pool = VariablePool(
|
|
||||||
system_variables={},
|
|
||||||
user_inputs={},
|
|
||||||
)
|
|
||||||
variable_pool.add(["pre_node_id", "object"], {"name": "John Doe", "age": 30, "email": "john@example.com"})
|
|
||||||
|
|
||||||
# Prepare the node data
|
|
||||||
node_data = HttpRequestNodeData(
|
|
||||||
title="Test JSON Body with Nested Object Variable",
|
|
||||||
method="post",
|
|
||||||
url="https://api.example.com/data",
|
|
||||||
authorization=HttpRequestNodeAuthorization(type="no-auth"),
|
|
||||||
headers="Content-Type: application/json",
|
|
||||||
params="",
|
|
||||||
body=HttpRequestNodeBody(
|
|
||||||
type="json",
|
|
||||||
data=[
|
|
||||||
BodyData(
|
|
||||||
key="",
|
|
||||||
type="text",
|
|
||||||
value='{"object": {{#pre_node_id.object#}}}',
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Initialize the Executor
|
|
||||||
executor = Executor(
|
|
||||||
node_data=node_data,
|
|
||||||
timeout=HttpRequestNodeTimeout(connect=10, read=30, write=30),
|
|
||||||
variable_pool=variable_pool,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Check the executor's data
|
|
||||||
assert executor.method == "post"
|
|
||||||
assert executor.url == "https://api.example.com/data"
|
|
||||||
assert executor.headers == {"Content-Type": "application/json"}
|
|
||||||
assert executor.params == {}
|
|
||||||
assert executor.json == {"object": {"name": "John Doe", "age": 30, "email": "john@example.com"}}
|
|
||||||
assert executor.data is None
|
|
||||||
assert executor.files is None
|
|
||||||
assert executor.content is None
|
|
||||||
|
|
||||||
# Check the raw request (to_log method)
|
|
||||||
raw_request = executor.to_log()
|
|
||||||
assert "POST /data HTTP/1.1" in raw_request
|
|
||||||
assert "Host: api.example.com" in raw_request
|
|
||||||
assert "Content-Type: application/json" in raw_request
|
|
||||||
assert '"object": {' in raw_request
|
|
||||||
assert '"name": "John Doe"' in raw_request
|
|
||||||
assert '"age": 30' in raw_request
|
|
||||||
assert '"email": "john@example.com"' in raw_request
|
|
Loading…
x
Reference in New Issue
Block a user