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
102 lines (73 loc) · 2.28 KB
/
pytest_plugin.py
File metadata and controls
102 lines (73 loc) · 2.28 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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()
@pytest.fixture(scope="session")
def make_httpserver_ipv4(httpserver_ssl_context):
server = HTTPServer(host="127.0.0.1", port=0, ssl_context=httpserver_ssl_context)
server.start()
yield server
server.clear()
if server.is_running():
server.stop()
@pytest.fixture
def httpserver_ipv4(make_httpserver_ipv4):
server = make_httpserver_ipv4
yield server
server.clear()
@pytest.fixture(scope="session")
def make_httpserver_ipv6(httpserver_ssl_context):
server = HTTPServer(host="::1", port=0, ssl_context=httpserver_ssl_context)
server.start()
yield server
server.clear()
if server.is_running():
server.stop()
@pytest.fixture
def httpserver_ipv6(make_httpserver_ipv6):
server = make_httpserver_ipv6
yield server
server.clear()