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
This commit is contained in:
Jan Boysen 2016-11-18 15:26:16 +01:00 committed by Andrew Godwin
parent 2e1cda8aad
commit fdc80cb269
3 changed files with 6 additions and 5 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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'")