Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/tools/step_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def add_body_to_step(
request_step_body["body"] = safe_json
except json.JSONDecodeError as e:
return BaseResult(error=f"Invalid JSON content provided for body_content: {str(e)}")
request_headers["Content-Type"] = "application/json"
request_headers["Content-Type"] = ["application/json"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


case "xml":
try:
Expand All @@ -101,7 +101,7 @@ async def add_body_to_step(
return BaseResult(error=f"Invalid XML content provided for body_content: {str(e)}")
except Exception as e:
return BaseResult(error=f"Error processing XML content: {str(e)}")
request_headers["Content-Type"] = "application/xml"
request_headers["Content-Type"] = ["application/xml"]
Comment on lines 93 to +104

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


case "html":
try:
Expand All @@ -110,7 +110,7 @@ async def add_body_to_step(
request_step_body["body"] = safe_html
except Exception as e:
return BaseResult(error=f"Error processing HTML content: {str(e)}")
request_headers["Content-Type"] = "text/html"
request_headers["Content-Type"] = ["text/html"]

case "text":
# Remove any null bytes and control characters except newlines/tabs
Expand All @@ -121,9 +121,9 @@ async def add_body_to_step(
request_step_body["body"] = safe_text
except Exception as e:
return BaseResult(error=f"Error processing text content: {str(e)}")
request_headers["Content-Type"] = "text/plain"
request_headers["Content-Type"] = ["text/plain"]
case _:
BaseResult(
return BaseResult(
error=f"Unsupported body_type {body_type}. Supported types are: json, xml, html, text"
)

Expand Down Expand Up @@ -156,7 +156,9 @@ async def add_assertion_to_step(
) -> BaseResult:
request_result = await self.read(bucket_key, test_id, step_id, result_formatter=None)
if not request_result or request_result.get("step_type") != "request":
return BaseResult(error=f"Step {step_id} is not a request step and cannot have a body added.")
return BaseResult(
error=f"Step {step_id} is not a request step and cannot have an assertion added."
)
if "assertions" not in request_result or not isinstance(request_result["assertions"], list):
request_result["assertions"] = []
new_assertion = {"source": assertion_source, "comparison": assertion_comparison}
Expand Down
44 changes: 44 additions & 0 deletions tests/test_step_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ async def test_add_body_to_step_json(self, mock_token, mock_context):
)

assert result.error is None
# Header values must be arrays per the REST API contract
put_payload = mock_api.call_args.kwargs["json"]
assert put_payload["headers"]["Content-Type"] == ["application/json"]

async def test_add_body_to_step_xml(self, mock_token, mock_context):
"""Test adding XML body to step"""
Expand Down Expand Up @@ -182,6 +185,23 @@ async def test_add_body_to_step_invalid_xml(self, mock_token, mock_context):
assert result.error is not None
assert "Invalid XML" in result.error

async def test_add_body_to_step_unsupported_type(self, mock_token, mock_context):
"""Test adding body with unsupported body_type returns error without calling the API"""
manager = StepManager(mock_token, mock_context)

with patch("src.tools.step_manager.api_request") as mock_api:
result = await manager.add_body_to_step(
"bucket_abc",
"test_123",
"step_123",
"yaml",
"key: value"
)

assert result.error is not None
assert "Unsupported body_type" in result.error
mock_api.assert_not_called()

async def test_add_assertion_to_step(self, mock_token, mock_context):
"""Test adding assertion to step"""
manager = StepManager(mock_token, mock_context)
Expand Down Expand Up @@ -213,3 +233,27 @@ async def test_add_assertion_to_step(self, mock_token, mock_context):

assert result.error is None

async def test_add_assertion_to_non_request_step(self, mock_token, mock_context):
"""Test adding assertion to a non-request step returns error"""
manager = StepManager(mock_token, mock_context)

with patch.object(manager, 'read') as mock_read:
mock_read.return_value = {
"id": "step_123",
"step_type": "pause",
"duration": 5
}

result = await manager.add_assertion_to_step(
"bucket_abc",
"test_123",
"step_123",
"response_status",
"equals",
None,
"200"
)

assert result.error is not None
assert "cannot have an assertion added" in result.error

Loading