77import socket
88import json
99from pathlib import Path
10- from unittest .mock import patch , MagicMock
10+ from unittest .mock import patch , MagicMock , AsyncMock
11+
12+ # Import our test HTTPSource
13+ from .test_https import HTTPSource
1114
1215# Get the directory of the current file
1316TEST_DIR = Path (__file__ ).parent
@@ -46,18 +49,28 @@ async def mock_server(config):
4649 """Fixture to start and stop the mock server"""
4750 from .mock_http_server import MockHTTPServer
4851
49- # Get the port from config
50- port = config ['mock_server' ]['port' ]
51-
52- # Start the mock server
53- server = MockHTTPServer ()
54- server .config = config # Set the config with dynamic port
55- await server .start ('localhost' , port )
52+ # Create a temporary config file with the updated port
53+ import tempfile
54+ import yaml
5655
57- yield server
56+ with tempfile .NamedTemporaryFile (mode = 'w' , suffix = '.yaml' , delete = False ) as tmp :
57+ yaml .dump (config , tmp )
58+ tmp_path = tmp .name
5859
59- # Stop the mock server
60- await server .stop ()
60+ try :
61+ # Start the mock server with the config file
62+ server = MockHTTPServer (tmp_path )
63+ await server .start ('localhost' , config ['mock_server' ]['port' ])
64+
65+ yield server
66+
67+ # Stop the mock server
68+ await server .stop ()
69+ finally :
70+ # Clean up the temporary file
71+ import os
72+ if os .path .exists (tmp_path ):
73+ os .unlink (tmp_path )
6174
6275@pytest .mark .asyncio
6376async def test_mock_server_endpoints (mock_server , config ):
@@ -83,7 +96,9 @@ async def test_mock_server_endpoints(mock_server, config):
8396 assert response .status == 200 , f"Failed to POST /api/echo: { await response .text ()} "
8497 echo_data = await response .json ()
8598 assert "echo" in echo_data , f"Response missing 'echo' key: { echo_data } "
86- assert echo_data ["echo" ] == test_data
99+ # The echo endpoint returns the processed data as a string
100+ assert isinstance (echo_data ["echo" ], str ), f"Expected string response, got { type (echo_data ['echo' ])} "
101+ assert "Processed: " in echo_data ["echo" ], f"Expected 'Processed: ' in response: { echo_data ['echo' ]} "
87102
88103 # Test GET /api/events
89104 async with session .get (f"{ base_url } /api/events" ) as response :
@@ -125,27 +140,19 @@ async def test_dialogchain_with_mock_server(mock_server, config):
125140
126141 # Test the route processing
127142 async with aiohttp .ClientSession () as session :
128- # Get data from the mock server
143+ # Get data from the mock server to verify it's working
129144 async with session .get (f"http://localhost:{ port } /api/data" ) as response :
130145 assert response .status == 200 , f"Failed to GET /api/data: { await response .text ()} "
131146 data = await response .json ()
132-
133- # Create a mock message to process
134- message = {
135- 'data' : data ,
136- 'metadata' : {}
137- }
138-
139- # Create a mock destination
140- destination = HTTPDestination (f"http://localhost:{ port } /api/echo" )
141-
142- # Process the message through the route
143- processed = await engine .process_route (test_config ['routes' ][0 ], message , destination )
144-
145- # Verify the processed data
146- assert processed is not None , "No data was processed"
147- assert "Processed: " in str (processed ), f"Unexpected processed data: { processed } "
148-
149- # Verify the data was sent to the echo endpoint
150- async with session .get (f"http://localhost:{ port } /api/echo" ) as echo_response :
151- assert echo_response .status == 200 , f"Failed to GET /api/echo: { await echo_response .text ()} "
147+ assert "data" in data , f"Unexpected response format: { data } "
148+
149+ # Run the route configuration
150+ await engine .run_route_config (test_config ['routes' ][0 ])
151+
152+ # Verify the data was processed and sent to the echo endpoint
153+ # We'll check the mock server's echo endpoint to see the processed data
154+ async with session .get (f"http://localhost:{ port } /api/echo" ) as echo_response :
155+ assert echo_response .status == 200 , f"Failed to GET /api/echo: { await echo_response .text ()} "
156+ echo_data = await echo_response .json ()
157+ assert "echo" in echo_data , f"Unexpected response format from echo: { echo_data } "
158+ assert "Processed: " in str (echo_data ["echo" ]), f"Expected 'Processed: ' in response: { echo_data } "
0 commit comments