@@ -99,7 +99,7 @@ async def test_mock_server_endpoints(mock_server, config):
9999 assert "echo" in echo_data , f"Response missing 'echo' key: { echo_data } "
100100 # The echo endpoint returns the processed data as a string
101101 assert isinstance (echo_data ["echo" ], str ), f"Expected string response, got { type (echo_data ['echo' ])} "
102- assert "Processed: " in echo_data ["echo" ], f"Expected 'Processed: ' in response: { echo_data [ 'echo' ] } "
102+ assert "Processed: " in echo_data ["echo" ], f"Expected 'Processed: ' in response: { echo_data } "
103103
104104 # Test GET /api/events
105105 async with session .get (f"{ base_url } /api/events" ) as response :
@@ -119,12 +119,25 @@ async def __aenter__(self):
119119 async def __aexit__ (self , exc_type , exc_val , exc_tb ):
120120 pass
121121
122- async def receive (self ):
123- """Return test data"""
124- return [{
122+ def __aiter__ (self ):
123+ self ._data = [{
125124 'data' : {'test' : 'data' },
126125 'metadata' : {'url' : 'mock://test' , 'status' : 200 }
127126 }]
127+ self ._index = 0
128+ return self
129+
130+ async def __anext__ (self ):
131+ if self ._index >= len (self ._data ):
132+ raise StopAsyncIteration
133+ result = self ._data [self ._index ]
134+ self ._index += 1
135+ return result
136+
137+ # For backward compatibility with the engine
138+ def receive (self ):
139+ """Return self as an async iterator"""
140+ return self
128141
129142class TestCamelRouterEngine (CamelRouterEngine ):
130143 """Test engine that uses our mock source"""
@@ -158,21 +171,49 @@ async def test_dialogchain_with_mock_server(mock_server, config):
158171 # Initialize our test engine with the config
159172 engine = TestCamelRouterEngine (test_config , verbose = True )
160173
161- # Test the route processing
162- async with aiohttp .ClientSession () as session :
163- # Get data from the mock server to verify it's working
164- async with session .get (f"http://localhost:{ port } /api/data" ) as response :
165- assert response .status == 200 , f"Failed to GET /api/data: { await response .text ()} "
166- data = await response .json ()
167- assert "data" in data , f"Unexpected response format: { data } "
174+ # Create a mock destination to capture the output
175+ class MockDestination :
176+ def __init__ (self ):
177+ self .received_messages = []
168178
169- # Run the route configuration
170- await engine .run_route_config (test_config ['routes' ][0 ])
171-
172- # Verify the data was processed and sent to the echo endpoint
173- # We'll check the mock server's echo endpoint to see the processed data
174- async with session .get (f"http://localhost:{ port } /api/echo" ) as echo_response :
175- assert echo_response .status == 200 , f"Failed to GET /api/echo: { await echo_response .text ()} "
176- echo_data = await echo_response .json ()
177- assert "echo" in echo_data , f"Unexpected response format from echo: { echo_data } "
178- assert "Processed: " in str (echo_data ["echo" ]), f"Expected 'Processed: ' in response: { echo_data } "
179+ async def __aenter__ (self ):
180+ return self
181+
182+ async def __aexit__ (self , exc_type , exc_val , exc_tb ):
183+ pass
184+
185+ async def send (self , message ):
186+ self .received_messages .append (message )
187+ return [{"status" : "success" }]
188+
189+ # Create a mock destination
190+ mock_dest = MockDestination ()
191+
192+ # Patch the engine's create_destination method to return our mock
193+ original_create_dest = engine .create_destination
194+
195+ def patched_create_dest (uri ):
196+ if uri == "mock://destination" :
197+ return mock_dest
198+ return original_create_dest (uri )
199+
200+ engine .create_destination = patched_create_dest
201+
202+ # Run the route configuration
203+ await engine .run_route_config (test_config ['routes' ][0 ])
204+
205+ # Verify the destination received the processed message
206+ assert len (mock_dest .received_messages ) > 0 , "No messages were sent to the destination"
207+ message = mock_dest .received_messages [0 ]
208+
209+ # The message should have the original data and metadata, plus the processed message
210+ assert "data" in message , f"Message missing 'data' key: { message } "
211+ assert "metadata" in message , f"Message missing 'metadata' key: { message } "
212+ assert "message" in message , f"Message missing 'message' key: { message } "
213+
214+ # Check the processed message
215+ assert message ["message" ] == "Processed: data" , f"Unexpected processed message: { message } "
216+
217+ # Check the original data is preserved
218+ assert message ["data" ] == {"test" : "data" }, f"Unexpected data: { message ['data' ]} "
219+ assert message ["metadata" ]["url" ] == "mock://test" , f"Unexpected URL: { message ['metadata' ]['url' ]} "
0 commit comments