44from unittest .mock import AsyncMock , patch
55from dialogchain .connectors import HTTPDestination
66
7+ class AsyncMockContext :
8+ def __init__ (self , return_value ):
9+ self .return_value = return_value
10+
11+ async def __aenter__ (self ):
12+ return self .return_value
13+
14+ async def __aexit__ (self , exc_type , exc_val , exc_tb ):
15+ pass
16+
717class TestHTTPDestination :
818 """Test HTTP destination connector."""
919
@@ -15,23 +25,14 @@ def http_dest(self):
1525 @pytest .mark .asyncio
1626 async def test_send_http_request (self , http_dest , capsys ):
1727 """Test sending an HTTP request."""
18- # Create a mock response for successful request
28+ # Create a mock response
1929 mock_response = AsyncMock ()
2030 mock_response .status = 200
2131 mock_response .text = AsyncMock (return_value = "OK" )
2232
23- # Create a mock post method
24- async def mock_post (* args , ** kwargs ):
25- return mock_response
26-
27- # Create a mock session
28- mock_session = AsyncMock ()
29- mock_session .post .side_effect = mock_post
30- mock_session .__aenter__ .return_value = mock_session
31- mock_session .__aexit__ .return_value = None
32-
33- # Patch the ClientSession
34- with patch ('aiohttp.ClientSession' , return_value = mock_session ):
33+ # Patch the ClientSession.post method directly
34+ with patch ('aiohttp.ClientSession.post' ,
35+ return_value = AsyncMockContext (mock_response )) as mock_post :
3536 # Test with dict message
3637 await http_dest .send ({"key" : "value" })
3738
@@ -40,7 +41,7 @@ async def mock_post(*args, **kwargs):
4041 assert "🌐 HTTP sent to http://example.com/webhook" in captured .out
4142
4243 # Verify the post was called correctly
43- mock_session . post .assert_called_once_with (
44+ mock_post .assert_called_once_with (
4445 'http://example.com/webhook' ,
4546 json = {"key" : "value" }
4647 )
@@ -53,18 +54,9 @@ async def test_send_string_message(self, http_dest, capsys):
5354 mock_response .status = 200
5455 mock_response .text = AsyncMock (return_value = "OK" )
5556
56- # Create a mock post method
57- async def mock_post (* args , ** kwargs ):
58- return mock_response
59-
60- # Create a mock session
61- mock_session = AsyncMock ()
62- mock_session .post .side_effect = mock_post
63- mock_session .__aenter__ .return_value = mock_session
64- mock_session .__aexit__ .return_value = None
65-
66- # Patch the ClientSession
67- with patch ('aiohttp.ClientSession' , return_value = mock_session ):
57+ # Patch the ClientSession.post method directly
58+ with patch ('aiohttp.ClientSession.post' ,
59+ return_value = AsyncMockContext (mock_response )) as mock_post :
6860 # Test with string message
6961 await http_dest .send ("test message" )
7062
@@ -73,7 +65,7 @@ async def mock_post(*args, **kwargs):
7365 assert "🌐 HTTP sent to http://example.com/webhook" in captured .out
7466
7567 # Verify the post was called correctly
76- mock_session . post .assert_called_once_with (
68+ mock_post .assert_called_once_with (
7769 'http://example.com/webhook' ,
7870 json = {"data" : "test message" }
7971 )
@@ -86,18 +78,9 @@ async def test_http_error(self, http_dest, capsys):
8678 mock_response .status = 400
8779 mock_response .text = AsyncMock (return_value = "Bad Request" )
8880
89- # Create a mock post method
90- async def mock_post (* args , ** kwargs ):
91- return mock_response
92-
93- # Create a mock session
94- mock_session = AsyncMock ()
95- mock_session .post .side_effect = mock_post
96- mock_session .__aenter__ .return_value = mock_session
97- mock_session .__aexit__ .return_value = None
98-
99- # Patch the ClientSession
100- with patch ('aiohttp.ClientSession' , return_value = mock_session ):
81+ # Patch the ClientSession.post method directly
82+ with patch ('aiohttp.ClientSession.post' ,
83+ return_value = AsyncMockContext (mock_response )) as mock_post :
10184 # Test with dict message
10285 await http_dest .send ({"key" : "value" })
10386
0 commit comments