import os import subprocess import time import pytest @pytest.mark.skipif( not os.environ.get('RUN_OTREE_HTTP_TESTS'), reason='Set RUN_OTREE_HTTP_TESTS=1 to enable devserver HTTP checks', ) def test_custom_css_served_over_http(tmp_path): import requests from pathlib import Path project = Path(__file__).resolve().parents[1] otree = project.parents[1] / 'venv' / 'bin' / 'otree' port = os.environ.get('OTREE_TEST_PORT', '8081') env = os.environ.copy() env['OTREE_DATABASE_URL'] = f"sqlite:///{tmp_path/'http_test.sqlite3'}" # Start devserver proc = subprocess.Popen( [str(otree), 'devserver', '--port', port], cwd=str(project), env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, ) try: # Wait briefly for server to start deadline = time.time() + 10 base = f"http://127.0.0.1:{port}" url = f"{base}/_static/custom.css" ok = False while time.time() < deadline: try: r = requests.get(url, timeout=0.5) if r.status_code == 200 and 'body' in r.text: ok = True break except Exception: pass time.sleep(0.5) assert ok, f"Did not get 200 for {url} within timeout" finally: proc.terminate() try: proc.wait(timeout=5) except Exception: proc.kill()