Skip to content

Commit a7b855c

Browse files
committed
plugin: allow setting host and port from envvar
Allow setting host and port from environment variable. Also, this is a refactor: * Change how the plugin works. Introduce a new class which register and de-register itself, so no need for extra workarounds. * Handling of stopped servers * Default listen host and port are moved to HTTPServer class attribute, so this can also be changed if desired by the user.
1 parent 3693c9a commit a7b855c

3 files changed

Lines changed: 59 additions & 11 deletions

File tree

pytest_httpserver/httpserver.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ class HTTPServer: # pylint: disable=too-many-instance-attributes
262262
of the server.
263263
"""
264264

265-
def __init__(self, host="localhost", port=4000, ssl_context=None):
265+
DEFAULT_LISTEN_HOST = "localhost"
266+
DEFAULT_LISTEN_PORT = 4000
267+
268+
def __init__(self, host=DEFAULT_LISTEN_HOST, port=DEFAULT_LISTEN_PORT, ssl_context=None):
266269
"""
267270
Initializes the instance.
268271

pytest_httpserver/pytest_plugin.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

22

3+
import os
4+
35
import pytest
46
from .httpserver import HTTPServer
57

@@ -8,21 +10,45 @@ class Plugin:
810
SERVER = None
911

1012

13+
class PluginHTTPServer(HTTPServer):
14+
def start(self):
15+
super().start()
16+
Plugin.SERVER = self
17+
18+
def stop(self):
19+
super().stop()
20+
Plugin.SERVER = None
21+
22+
23+
def get_httpserver_listen_address():
24+
listen_host = os.environ.get("PYTEST_HTTPSERVER_HOST")
25+
listen_port = os.environ.get("PYTEST_HTTPSERVER_PORT")
26+
if listen_port:
27+
listen_port = int(listen_port)
28+
29+
return (listen_host, listen_port)
30+
31+
1132
@pytest.fixture
1233
def httpserver():
13-
if Plugin.SERVER is None:
14-
server = HTTPServer()
15-
server.start()
16-
Plugin.SERVER = server
17-
else:
18-
server = Plugin.SERVER
19-
20-
server.clear()
34+
if Plugin.SERVER:
35+
Plugin.SERVER.clear()
36+
yield Plugin.SERVER
37+
return
38+
39+
host, port = get_httpserver_listen_address()
40+
if not host:
41+
host = HTTPServer.DEFAULT_LISTEN_HOST
42+
if not port:
43+
port = HTTPServer.DEFAULT_LISTEN_PORT
44+
45+
server = PluginHTTPServer(host=host, port=port)
46+
server.start()
2147
yield server
2248

2349

2450
def pytest_sessionfinish(session, exitstatus): # pylint: disable=unused-argument
2551
if Plugin.SERVER is not None:
2652
Plugin.SERVER.clear()
27-
Plugin.SERVER.stop()
28-
Plugin.SERVER = None
53+
if Plugin.SERVER.is_running():
54+
Plugin.SERVER.stop()

tests/test_port_changing.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import os
3+
import pytest
4+
from pytest_httpserver import HTTPServer
5+
6+
PORT_KEY = "PYTEST_HTTPSERVER_PORT"
7+
HOST_KEY = "PYTEST_HTTPSERVER_HOST"
8+
9+
10+
@pytest.mark.skipif(HOST_KEY not in os.environ,
11+
reason="requires {} environment variable".format(HOST_KEY))
12+
def test_host_changing_by_environment(httpserver: HTTPServer):
13+
assert httpserver.host == os.environ[HOST_KEY]
14+
15+
16+
@pytest.mark.skipif(PORT_KEY not in os.environ,
17+
reason="requires {} environment variable".format(PORT_KEY))
18+
def test_port_changing_by_environment(httpserver: HTTPServer):
19+
assert httpserver.port == int(os.environ[PORT_KEY])

0 commit comments

Comments
 (0)