Skip to content

Commit

Permalink
'--live-server-host' option added
Browse files Browse the repository at this point in the history
  • Loading branch information
o1da committed Oct 15, 2018
1 parent ed538e1 commit b827081
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pytest_flask/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,24 @@ class LiveServer(object):
stopping application in a separate process.
:param app: The application to run.
:param host: The host where to listen (default localhost).
:param port: The port to run application.
"""

def __init__(self, app, port, clean_stop=False):
def __init__(self, app, host, port, clean_stop=False):
self.app = app
self.port = port
self.host = host
self.clean_stop = clean_stop
self._process = None

def start(self):
"""Start application in a separate process."""
def worker(app, port):
app.run(port=port, use_reloader=False, threaded=True)
def worker(app, host, port):
app.run(host=host, port=port, use_reloader=False, threaded=True)
self._process = multiprocessing.Process(
target=worker,
args=(self.app, self.port)
args=(self.app, self.host, self.port)
)
self._process.start()

Expand All @@ -82,7 +84,7 @@ def worker(app, port):

def url(self, url=''):
"""Returns the complete url based on server options."""
return 'http://localhost:%d%s' % (self.port, url)
return 'http://%s:%d%s' % (self.host, self.port, url)

def stop(self):
"""Stop application process."""
Expand Down Expand Up @@ -143,14 +145,16 @@ def test_server_is_up_and_running(live_server):
port = s.getsockname()[1]
s.close()

host = pytestconfig.getvalue('live_server_host')

# Explicitly set application ``SERVER_NAME`` for test suite
# and restore original value on test teardown.
server_name = app.config['SERVER_NAME'] or 'localhost'
monkeypatch.setitem(app.config, 'SERVER_NAME',
_rewrite_server_name(server_name, str(port)))

clean_stop = request.config.getvalue('live_server_clean_stop')
server = LiveServer(app, port, clean_stop)
server = LiveServer(app, host, port, clean_stop)
if request.config.getvalue('start_live_server'):
server.start()

Expand Down
2 changes: 2 additions & 0 deletions pytest_flask/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def pytest_addoption(parser):
group.addoption('--no-live-server-clean-stop',
action="store_false", dest="live_server_clean_stop",
help="terminate the server forcefully after stop.")
group.addoption('--live-server-host', action='store', default='localhost', type=str,
help='use a host where to listen (default localhost).')
group.addoption('--live-server-port', action='store', default=0, type=int,
help='use a fixed port for the live_server fixture.')

Expand Down
12 changes: 12 additions & 0 deletions tests/test_live_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,15 @@ def test_port(live_server):
result = appdir.runpytest('-v', '--live-server-port', str(port))
result.stdout.fnmatch_lines(['*PASSED*'])
assert result.ret == 0

@pytest.mark.parametrize('host', ['127.0.0.1', '0.0.0.0'])
def test_live_server_fixed_host(self, host, appdir):
appdir.create_test_module('''
import pytest
def test_port(live_server):
assert live_server.host == '%s'
''' % host)
result = appdir.runpytest('-v', '--live-server-host', str(host))
result.stdout.fnmatch_lines(['*PASSED*'])
assert result.ret == 0

0 comments on commit b827081

Please sign in to comment.