diff --git a/src/tools/step_manager.py b/src/tools/step_manager.py index 46ef149..2a56fac 100644 --- a/src/tools/step_manager.py +++ b/src/tools/step_manager.py @@ -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"] case "xml": try: @@ -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"] case "html": try: @@ -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 @@ -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" ) @@ -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} diff --git a/tests/test_step_manager.py b/tests/test_step_manager.py index 2e14d9d..6fe33be 100644 --- a/tests/test_step_manager.py +++ b/tests/test_step_manager.py @@ -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""" @@ -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) @@ -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 +