forked from csernazs/pytest-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytest_plugin.py
More file actions
68 lines (47 loc) · 1.49 KB
/
pytest_plugin.py
File metadata and controls
68 lines (47 loc) · 1.49 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import pytest
from .httpserver import HTTPServer
class Plugin:
SERVER = None
class PluginHTTPServer(HTTPServer):
def start(self):
super().start()
Plugin.SERVER = self
def stop(self):
super().stop()
Plugin.SERVER = None
def get_httpserver_listen_address():
listen_host = os.environ.get("PYTEST_HTTPSERVER_HOST")
listen_port = os.environ.get("PYTEST_HTTPSERVER_PORT")
if listen_port:
listen_port = int(listen_port)
return listen_host, listen_port
@pytest.fixture(scope="session")
def httpserver_listen_address():
return get_httpserver_listen_address()
@pytest.fixture(scope="session")
def httpserver_ssl_context():
return None
@pytest.fixture(scope="session")
def make_httpserver(httpserver_listen_address, httpserver_ssl_context):
host, port = httpserver_listen_address
if not host:
host = HTTPServer.DEFAULT_LISTEN_HOST
if not port:
port = HTTPServer.DEFAULT_LISTEN_PORT
server = HTTPServer(host=host, port=port, ssl_context=httpserver_ssl_context)
server.start()
yield server
server.clear()
if server.is_running():
server.stop()
def pytest_sessionfinish(session, exitstatus): # pylint: disable=unused-argument
if Plugin.SERVER is not None:
Plugin.SERVER.clear()
if Plugin.SERVER.is_running():
Plugin.SERVER.stop()
@pytest.fixture
def httpserver(make_httpserver):
server = make_httpserver
yield server
server.clear()