From fdc80cb26978ed66d0aa2b853136d7a0cd8b15c6 Mon Sep 17 00:00:00 2001 From: Jan Boysen Date: Fri, 18 Nov 2016 15:26:16 +0100 Subject: [PATCH] runserver should respect FORCE_SCRIPT_NAME setting (#435) * Pass FORCE_SCRIPT_NAME to Daphne server when set FORCE_SCRIPT_NAME seems not to be honored any more with build-in runserver after activating channels app. The normal behavior of Django is the FORCE_SCRIPT_NAME is used as prefix when set while generating URLs so its possible to create a path prefix and determine different Django installations based on the path rather than hostname without having to prefix all paths in urls.py. * Only strip script_name from path if it starts with it * make tests happy again after setting kwarg root_path --- channels/handler.py | 2 +- channels/management/commands/runserver.py | 1 + channels/tests/test_management.py | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/channels/handler.py b/channels/handler.py index e6d5734..f43da22 100644 --- a/channels/handler.py +++ b/channels/handler.py @@ -44,7 +44,7 @@ class AsgiRequest(http.HttpRequest): # Path info self.path = self.message['path'] self.script_name = self.message.get('root_path', '') - if self.script_name: + if self.script_name and self.path.startswith(self.script_name): # TODO: Better is-prefix checking, slash handling? self.path_info = self.path[len(self.script_name):] else: diff --git a/channels/management/commands/runserver.py b/channels/management/commands/runserver.py index ac1d783..062ac46 100644 --- a/channels/management/commands/runserver.py +++ b/channels/management/commands/runserver.py @@ -84,6 +84,7 @@ class Command(RunserverCommand): action_logger=self.log_action, http_timeout=self.http_timeout, ws_protocols=getattr(settings, 'CHANNELS_WS_PROTOCOLS', None), + root_path=getattr(settings, 'FORCE_SCRIPT_NAME', ''), ).run() self.logger.debug("Daphne exited") except KeyboardInterrupt: diff --git a/channels/tests/test_management.py b/channels/tests/test_management.py index 6c3f78e..6213caa 100644 --- a/channels/tests/test_management.py +++ b/channels/tests/test_management.py @@ -87,7 +87,7 @@ class RunServerTests(TestCase): call_command('runserver', '--noreload') mocked_server.assert_called_with(port=8000, signal_handlers=True, http_timeout=60, host='127.0.0.1', action_logger=mock.ANY, channel_layer=mock.ANY, - ws_protocols=None) + ws_protocols=None, root_path=None) @mock.patch('channels.management.commands.runserver.sys.stdout', new_callable=StringIO) @mock.patch('channels.management.commands.runserver.Server') @@ -101,12 +101,12 @@ class RunServerTests(TestCase): call_command('runserver', '--noreload') mocked_server.assert_called_with(port=8000, signal_handlers=True, http_timeout=60, host='127.0.0.1', action_logger=mock.ANY, channel_layer=mock.ANY, - ws_protocols=None) + ws_protocols=None, root_path=None) call_command('runserver', '--noreload', 'localhost:8001') mocked_server.assert_called_with(port=8001, signal_handlers=True, http_timeout=60, host='localhost', action_logger=mock.ANY, channel_layer=mock.ANY, - ws_protocols=None) + ws_protocols=None, root_path=None) self.assertFalse(mocked_worker.called, "The worker should not be called with '--noworker'") @@ -121,7 +121,7 @@ class RunServerTests(TestCase): call_command('runserver', '--noreload', '--noworker') mocked_server.assert_called_with(port=8000, signal_handlers=True, http_timeout=60, host='127.0.0.1', action_logger=mock.ANY, channel_layer=mock.ANY, - ws_protocols=None) + ws_protocols=None, root_path=None) self.assertFalse(mocked_worker.called, "The worker should not be called with '--noworker'")