Skip to content

Commit 438bfc9

Browse files
author
Tom Softreck
committed
update
1 parent b9e8f5c commit 438bfc9

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

tests/integration/test_https.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,35 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
2222
if self.session:
2323
await self.session.close()
2424

25-
async def receive(self):
26-
"""Receive data from the HTTP endpoint"""
25+
async def _fetch_data(self):
26+
"""Fetch data from the HTTP endpoint"""
2727
if not self.session:
2828
self.session = aiohttp.ClientSession()
2929

3030
async with self.session.get(self.url) as response:
3131
if response.status == 200:
3232
data = await response.json()
33-
return [{'data': data, 'metadata': {'url': self.url, 'status': response.status}}]
33+
return {'data': data, 'metadata': {'url': self.url, 'status': response.status}}
3434
else:
35-
return [{'error': f"HTTP {response.status}", 'metadata': {'url': self.url, 'status': response.status}}]
35+
return {'error': f"HTTP {response.status}", 'metadata': {'url': self.url, 'status': response.status}}
36+
37+
async def receive(self):
38+
"""Return an async iterator that yields messages"""
39+
class HTTPSourceIterator:
40+
def __init__(self, source):
41+
self.source = source
42+
self._data_received = False
43+
44+
def __aiter__(self):
45+
return self
46+
47+
async def __anext__(self):
48+
if self._data_received:
49+
raise StopAsyncIteration
50+
self._data_received = True
51+
return await self.source._fetch_data()
3652

37-
# Return an empty list if no data was received
38-
return []
53+
return HTTPSourceIterator(self)
3954

4055
# Patch the engine to use our test HTTPSource
4156
@pytest.fixture(autouse=True)

tests/integration/test_mock_server.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
# Import our test HTTPSource
1313
from .test_https import HTTPSource
14+
from dialogchain.engine import CamelRouterEngine
1415

1516
# Get the directory of the current file
1617
TEST_DIR = Path(__file__).parent
@@ -109,10 +110,18 @@ async def test_mock_server_endpoints(mock_server, config):
109110
assert "event" in events[0], f"Event missing 'event' key: {events[0]}"
110111
assert "value" in events[0], f"Event missing 'value' key: {events[0]}"
111112

113+
class TestCamelRouterEngine(CamelRouterEngine):
114+
"""Test engine that can handle HTTP sources"""
115+
116+
async def create_source(self, uri: str):
117+
"""Create source connector from URI"""
118+
if uri.startswith('http'):
119+
return HTTPSource(uri)
120+
return await super().create_source(uri)
121+
112122
@pytest.mark.asyncio
113123
async def test_dialogchain_with_mock_server(mock_server, config):
114124
"""Test DialogChain with the mock server"""
115-
from dialogchain.engine import CamelRouterEngine
116125
from dialogchain.connectors import HTTPDestination
117126

118127
# Get the port from config
@@ -135,18 +144,8 @@ async def test_dialogchain_with_mock_server(mock_server, config):
135144
]
136145
}
137146

138-
# Initialize the engine with test config
139-
engine = CamelRouterEngine(test_config, verbose=True)
140-
141-
# Patch the create_source method to use our HTTPSource
142-
original_create_source = engine.create_source
143-
144-
def patched_create_source(uri):
145-
if uri.startswith('http'):
146-
return HTTPSource(uri)
147-
return original_create_source(uri)
148-
149-
engine.create_source = patched_create_source
147+
# Initialize our test engine with the config
148+
engine = TestCamelRouterEngine(test_config, verbose=True)
150149

151150
# Test the route processing
152151
async with aiohttp.ClientSession() as session:

0 commit comments

Comments
 (0)