Skip to content

Commit

Permalink
Allow setting of local HTTP server address and port
Browse files Browse the repository at this point in the history
Fixes #73
  • Loading branch information
jordan-zilch authored and moggers87 committed Dec 18, 2019
1 parent e810020 commit 7b9873e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
7 changes: 5 additions & 2 deletions exhibition/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ def gen():


@exhibition.command(short_help="Serve site locally")
def serve():
@click.option("-s", "--server", default="localhost", help="Hostname to serve the site at.")
@click.option("-p", "--port", default=8000, type=int, help="Port to serve the site at.")
def serve(server, port):
"""
Serve files from deploy_path as a webserver would
"""
settings = config.Config.from_path(config.SITE_YAML_PATH)
httpd, thread = utils.serve(settings)
server_address = (server, port)
httpd, thread = utils.serve(settings, server_address)

try:
thread.join()
Expand Down
24 changes: 22 additions & 2 deletions exhibition/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def test_serve(self, config_mock, serve_mock):

self.assertEqual(result.exit_code, 0)
self.assertEqual(serve_mock.call_count, 1)
self.assertEqual(serve_mock.call_args, ((config_mock.return_value,), {}))
expected_args = ((config_mock.return_value, ("localhost", 8000)), {})
self.assertEqual(serve_mock.call_args, expected_args)

self.assertEqual(config_mock.call_args, ((config.SITE_YAML_PATH,), {}))
self.assertEqual(serve_mock.return_value[0].shutdown.call_count, 0)
Expand All @@ -59,12 +60,31 @@ def test_serve(self, config_mock, serve_mock):
result = runner.invoke(command.exhibition, ["serve"])
self.assertEqual(result.exit_code, 0)
self.assertEqual(serve_mock.call_count, 2)
self.assertEqual(serve_mock.call_args, ((config_mock.return_value,), {}))
expected_args = ((config_mock.return_value, ("localhost", 8000)), {})
self.assertEqual(serve_mock.call_args, expected_args)

self.assertEqual(config_mock.call_args, ((config.SITE_YAML_PATH,), {}))
self.assertEqual(serve_mock.return_value[0].shutdown.call_count, 1)
self.assertEqual(serve_mock.return_value[1].join.call_count, 2)

# Undoing the KeyboardInterrupt
serve_mock.return_value[1].join.side_effect = None
# Now testing w args
config_mock.reset_mock()
serve_mock.reset_mock()
serve_mock.return_value[0].reset_mock()
serve_mock.return_value[1].reset_mock()
addr_options = ["serve", "--server", "localhost", "--port", "8001"]
result = runner.invoke(command.exhibition, addr_options)

self.assertEqual(result.exit_code, 0)
self.assertEqual(serve_mock.call_count, 1)
expected_args = ((config_mock.return_value, ("localhost", 8001)), {})
self.assertEqual(serve_mock.call_args, expected_args)

self.assertEqual(serve_mock.return_value[0].shutdown.call_count, 0)
self.assertEqual(serve_mock.return_value[1].join.call_count, 1)

@mock.patch.object(command, "logger")
def test_exhibition(self, log_mock):
@command.exhibition.command()
Expand Down
3 changes: 2 additions & 1 deletion exhibition/tests/test_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def tearDown(self):
self.server.server_close()

def get_server(self, settings):
httpd, thread = serve(settings)
server_address = ("localhost", 8000)
httpd, thread = serve(settings, server_address)
self.server = httpd

def test_fetch_file(self):
Expand Down
4 changes: 1 addition & 3 deletions exhibition/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ def end_headers(self):
return super().end_headers()


def serve(settings):
def serve(settings, server_address):
"""
Serves the generated site from ``deploy_path``
Respects settings like ``base_url`` if present.
"""
logger = logging.getLogger("exhibition.server")

server_address = ('localhost', 8000)

# this is quite ewwww, but whatever.
class ExhibitionHTTPRequestHandler(ExhibitionBaseHTTPRequestHandler):
_settings = settings
Expand Down

0 comments on commit 7b9873e

Please sign in to comment.