From 5648da5d34f01d0d7dcaa6731ef199bc3bdbdf1a Mon Sep 17 00:00:00 2001 From: Artem Malyshev Date: Thu, 13 Apr 2017 00:01:25 +0300 Subject: [PATCH] Provide a way to disable static files serving in the live server test case. (#603) This allows it to be turned off (it's on by default) --- channels/test/liveserver.py | 14 ++++++++++---- docs/testing.rst | 13 +++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/channels/test/liveserver.py b/channels/test/liveserver.py index f942ca5..caa4020 100644 --- a/channels/test/liveserver.py +++ b/channels/test/liveserver.py @@ -2,6 +2,7 @@ import multiprocessing import django from daphne.server import Server +from django.apps import apps from django.core.exceptions import ImproperlyConfigured from django.db import connections from django.db.utils import load_backend @@ -67,13 +68,14 @@ class ProcessSetup(multiprocessing.Process): class WorkerProcess(ProcessSetup): def __init__(self, is_ready, n_threads, overridden_settings, - modified_settings, databases): + modified_settings, databases, serve_static): self.is_ready = is_ready self.n_threads = n_threads self.overridden_settings = overridden_settings self.modified_settings = modified_settings self.databases = databases + self.serve_static = serve_static super(WorkerProcess, self).__init__() self.daemon = True @@ -82,9 +84,11 @@ class WorkerProcess(ProcessSetup): try: self.common_setup() channel_layers = ChannelLayerManager() - channel_layers[DEFAULT_CHANNEL_LAYER].router.check_default( - http_consumer=StaticFilesConsumer(), - ) + check_default = channel_layers[DEFAULT_CHANNEL_LAYER].router.check_default + if self.serve_static and apps.is_installed('django.contrib.staticfiles'): + check_default(http_consumer=StaticFilesConsumer()) + else: + check_default() if self.n_threads == 1: self.worker = Worker( channel_layer=channel_layers[DEFAULT_CHANNEL_LAYER], @@ -153,6 +157,7 @@ class ChannelLiveServerTestCase(TransactionTestCase): ProtocolServerProcess = DaphneProcess WorkerProcess = WorkerProcess worker_threads = 1 + serve_static = True @property def live_server_url(self): @@ -204,6 +209,7 @@ class ChannelLiveServerTestCase(TransactionTestCase): self._overridden_settings, self._modified_settings, connections.databases, + self.serve_static, ) self._worker_process.start() worker_ready.wait() diff --git a/docs/testing.rst b/docs/testing.rst index 945bf03..09584c0 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -325,3 +325,16 @@ infrastructure is ready default web browser will be also started. You can open your website in the real browser which can execute JavaScript and operate on WebSockets. ``live_server_ws_url`` property is also provided if you decide to run messaging directly from Python. + +By default live server test case will serve static files. To disable +this feature override `serve_static` class attribute. + +.. code:: python + + class IntegrationTest(ChannelLiveServerTestCase): + + serve_static = False + + def test_websocket_message(self): + # JS and CSS are not available in this test. + ...