File size: 1,495 Bytes
e40dce0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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()