test: Replace http server for recipes with nginx container

This commit is contained in:
Chris Coutinho
2025-10-17 04:30:03 +02:00
parent 2999d4b65e
commit 27519d0f62
5 changed files with 41 additions and 95 deletions
-74
View File
@@ -1319,77 +1319,3 @@ async def test_user_in_group(nc_client: NextcloudClient, test_user, test_group):
logger.debug(f"Added user {user_config['userid']} to group {groupid}")
yield (user_config, groupid)
@pytest.fixture(scope="session")
def test_recipe_server():
"""
Fixture to create a local HTTP server serving test recipe HTML pages.
This serves static HTML files with schema.org Recipe JSON-LD data for testing
recipe import functionality without relying on external websites.
Yields the server URL (e.g., "http://localhost:8082")
"""
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer
from pathlib import Path
httpd = None
server_thread = None
# Get the path to the fixtures directory
fixtures_dir = Path(__file__).parent / "fixtures"
class RecipeServerHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args):
# Suppress default HTTP logging
pass
def do_GET(self):
# Map URL paths to fixture files
if self.path == "/black-pepper-tofu":
file_path = fixtures_dir / "test_recipe.html"
else:
# 404 for unknown paths
self.send_response(404)
self.end_headers()
return
if file_path.exists():
with open(file_path, "rb") as f:
content = f.read()
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(content)))
self.end_headers()
self.wfile.write(content)
else:
self.send_response(404)
self.end_headers()
try:
# Start the HTTP server on all interfaces (0.0.0.0) so Docker can reach it
httpd = HTTPServer(("0.0.0.0", 8082), RecipeServerHandler)
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.daemon = True
server_thread.start()
logger.info(
"Test recipe server started on http://0.0.0.0:8082 (accessible from Docker)"
)
# Yield the server URL (use localhost for test code, will be replaced with Docker-accessible IP)
yield "http://localhost:8082"
finally:
# Clean up the server
if httpd:
logger.info("Shutting down test recipe server...")
shutdown_thread = threading.Thread(target=httpd.shutdown)
shutdown_thread.start()
shutdown_thread.join(timeout=2)
httpd.server_close()
logger.info("Test recipe server shut down successfully")
if server_thread:
server_thread.join(timeout=1)