Skip to content

Commit bd0154c

Browse files
author
Tom Softreck
committed
update
1 parent 65ca5ac commit bd0154c

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

tests/unit/test_http_simple.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""Simplified test for HTTPDestination."""
2+
import asyncio
3+
import pytest
4+
from unittest.mock import AsyncMock, patch, MagicMock
5+
6+
# Import only what we need
7+
import sys
8+
import os
9+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../src')))
10+
11+
from dialogchain.connectors import HTTPDestination
12+
13+
class AsyncMockContext:
14+
def __init__(self, return_value):
15+
self.return_value = return_value
16+
17+
async def __aenter__(self):
18+
return self.return_value
19+
20+
async def __aexit__(self, exc_type, exc_val, exc_tb):
21+
pass
22+
23+
class TestHTTPSimple:
24+
"""Simplified test for HTTPDestination."""
25+
26+
@pytest.fixture
27+
def http_dest(self):
28+
"""Create an HTTPDestination instance for testing."""
29+
return HTTPDestination("http://example.com/webhook")
30+
31+
@pytest.mark.asyncio
32+
async def test_send_http_request(self, http_dest, capsys):
33+
"""Test sending an HTTP request."""
34+
# Create a mock response
35+
mock_response = AsyncMock()
36+
mock_response.status = 200
37+
mock_response.text = AsyncMock(return_value="OK")
38+
39+
# Patch the ClientSession.post method
40+
with patch('aiohttp.ClientSession.post',
41+
return_value=AsyncMockContext(mock_response)) as mock_post:
42+
# Test with dict message
43+
await http_dest.send({"key": "value"})
44+
45+
# Check output
46+
captured = capsys.readouterr()
47+
assert "🌐 HTTP sent to http://example.com/webhook" in captured.out
48+
49+
# Verify the post was called correctly
50+
mock_post.assert_called_once_with(
51+
'http://example.com/webhook',
52+
json={"key": "value"}
53+
)
54+
55+
@pytest.mark.asyncio
56+
async def test_send_string_message(self, http_dest, capsys):
57+
"""Test sending a string message."""
58+
# Create a mock response
59+
mock_response = AsyncMock()
60+
mock_response.status = 200
61+
mock_response.text = AsyncMock(return_value="OK")
62+
63+
# Patch the ClientSession.post method
64+
with patch('aiohttp.ClientSession.post',
65+
return_value=AsyncMockContext(mock_response)) as mock_post:
66+
# Test with string message
67+
await http_dest.send("test message")
68+
69+
# Check output
70+
captured = capsys.readouterr()
71+
assert "🌐 HTTP sent to http://example.com/webhook" in captured.out
72+
73+
# Verify the post was called correctly
74+
mock_post.assert_called_once_with(
75+
'http://example.com/webhook',
76+
json={"data": "test message"}
77+
)
78+
79+
@pytest.mark.asyncio
80+
async def test_http_error(self, http_dest, capsys):
81+
"""Test handling of HTTP errors."""
82+
# Create a mock response with error status
83+
mock_response = AsyncMock()
84+
mock_response.status = 400
85+
mock_response.text = AsyncMock(return_value="Bad Request")
86+
87+
# Patch the ClientSession.post method
88+
with patch('aiohttp.ClientSession.post',
89+
return_value=AsyncMockContext(mock_response)) as mock_post:
90+
# Test with dict message
91+
await http_dest.send({"key": "value"})
92+
93+
# Check error output
94+
captured = capsys.readouterr()
95+
assert "❌ HTTP error 400: Bad Request" in captured.out

0 commit comments

Comments
 (0)