def test_is_single_callable(): from asgiref.compatibility import is_double_callable wrapper = StaticFilesWrapper(None) assert not is_double_callable(wrapper), ( "StaticFilesWrapper should be recognized as a single callable by " "asgiref compatibility tools")
async def test_staticfiles_wrapper_calls_application_if_static_url_has_host( settings): settings.STATIC_URL = "http://hostname.com/mystatic/" wrapper = StaticFilesWrapper(MockApplication("application")) scope = request_for_path("/mystatic/match") assert await wrapper(scope, None, None) == "application", ( "StaticFilesWrapper should call inner application if STATIC_URL contains a " "host, even if path matches")
async def test_staticfiles_wrapper_calls_application_for_non_http_paths( settings): settings.STATIC_URL = "/mystatic/" wrapper = StaticFilesWrapper(MockApplication("application")) non_http_static_path = request_for_path("/mystatic/match", type="websocket") assert await wrapper(non_http_static_path, None, None) == "application", ( "StaticFilesWrapper should call inner application if path matches " "but type is not HTTP")
async def test_staticfiles_wrapper_calls_application_for_non_static_http_requests( ): wrapper = StaticFilesWrapper(MockApplication("application")) non_static_path = request_for_path("/path/to/non/static/resource") assert ( await wrapper(non_static_path, None, None) == "application" ), "StaticFilesWrapper should call inner application for non-static HTTP paths" non_http_path = request_for_path("/path/to/websocket", type="websocket") assert ( await wrapper(non_http_path, None, None) == "application" ), "StaticFilesWrapper should call inner application for non-HTTP paths"
def get_application(self, options: Scope): """ Returns the static files serving application wrapping the default application, if static files should be served. Otherwise just returns the default handler. """ staticfiles_installed = apps.is_installed("django.contrib.staticfiles") use_static_handler = options.get("use_static_handler", staticfiles_installed) insecure_serving = options.get("insecure_serving", False) if use_static_handler and (settings.DEBUG or insecure_serving): return StaticFilesWrapper(get_default_application()) else: return get_default_application()
async def test_staticfiles_wrapper_serves_static_http_requests(settings): settings.STATIC_URL = "/mystatic/" application = MockApplication("application") wrapper = StaticFilesWrapper(application, staticfiles_handler=MockStaticHandler) scope = request_for_path("/mystatic/image.png") assert ( await wrapper(scope, None, None) == "/mystatic/image.png" ), "StaticFilesWrapper should serve paths under the STATIC_URL path" assert ( not application.was_called ), "The inner application should not be called when serving static files"
async def test_staticfiles_handler_can_generate_file_path(): """ StaticFilesHandler.file_path must not rely on scope being assigned to self. """ class MockedHandler(StaticFilesHandler): async def __call__(self, scope, receive, send): # Equivalent setUp from real __call__. request = self.request_class(scope, "") self.static_base_url = scope["static_base_url"][2] # Method under test. return self.file_path(request.path) wrapper = StaticFilesWrapper(MockApplication("application"), staticfiles_handler=MockedHandler) scope = request_for_path("/static/image.png") scope["method"] = "GET" assert ( await wrapper(scope, None, None) == "/image.png" ), "StaticFilesWrapper should serve paths under the STATIC_URL path"
def main(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "stellaru.settings_release") django.setup() django.core.management.call_command("makemigrations") django.core.management.call_command("migrate") port = find_port() from stellaru import routing as stellaru_app application = StaticFilesWrapper(stellaru_app.application) endpoints = build_endpoint_description_strings( host=DEFAULT_HOST, port=port, unix_socket=None, file_descriptor=None, ) server = Server(application=application, endpoints=endpoints, server_name='Stellaru') print('Starting Stellaru') thread = threading.Thread(target=open_browser, args=(port, )) thread.run() server.run()
""" ASGI entrypoint. Configures Django and then runs the application defined in the ASGI_APPLICATION setting. """ import os import django from channels.routing import get_default_application from channels.staticfiles import StaticFilesWrapper os.environ.setdefault("DJANGO_SETTINGS_MODULE", "openvpnathome.settings.settings") django.setup() application = StaticFilesWrapper(get_default_application())
from channels.routing import ProtocolTypeRouter, URLRouter from channels.staticfiles import StaticFilesWrapper import drunkpoker.main.routing from channels.sessions import SessionMiddlewareStack application = ProtocolTypeRouter({ "http": SessionMiddlewareStack( StaticFilesWrapper( URLRouter( drunkpoker.main.routing.http_urlpatterns ) ) ), "websocket": SessionMiddlewareStack( URLRouter( drunkpoker.main.routing.websocket_patterns ) ) })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.staticfiles import StaticFilesWrapper from django.conf.urls import url from .consumers import GraphqlSubcriptionConsumer, MainConsumer application = StaticFilesWrapper( ProtocolTypeRouter({ "websocket": AuthMiddlewareStack( URLRouter([ url("^ws/$", GraphqlSubcriptionConsumer), url("^direct/$", MainConsumer), ])), }))