1
- import os
2
-
3
1
import pytest
4
-
5
- from channels .staticfiles import StaticFilesHandler , StaticFilesWrapper
2
+ from django .contrib .staticfiles .handlers import ASGIStaticFilesHandler
6
3
7
4
8
5
@pytest .fixture (autouse = True )
@@ -21,39 +18,16 @@ async def __call__(self, scope, receive, send):
21
18
return self .return_value
22
19
23
20
24
- class MockStaticHandler :
25
- async def __call__ (self , scope , receive , send ):
26
- return scope ["path" ]
27
-
28
-
29
21
def request_for_path (path , type = "http" ):
30
22
return {
31
23
"type" : type ,
32
24
"path" : path ,
33
25
}
34
26
35
27
36
- @pytest .mark .asyncio
37
- @pytest .mark .filterwarnings ("ignore::DeprecationWarning" )
38
- async def test_staticfiles_wrapper_serves_static_http_requests (settings ):
39
- settings .STATIC_URL = "/mystatic/"
40
-
41
- application = MockApplication ("application" )
42
-
43
- wrapper = StaticFilesWrapper (application , staticfiles_handler = MockStaticHandler )
44
-
45
- scope = request_for_path ("/mystatic/image.png" )
46
- assert (
47
- await wrapper (scope , None , None ) == "/mystatic/image.png"
48
- ), "StaticFilesWrapper should serve paths under the STATIC_URL path"
49
- assert (
50
- not application .was_called
51
- ), "The inner application should not be called when serving static files"
52
-
53
-
54
28
@pytest .mark .asyncio
55
29
async def test_staticfiles_wrapper_calls_application_for_non_static_http_requests ():
56
- wrapper = StaticFilesWrapper (MockApplication ("application" ))
30
+ wrapper = ASGIStaticFilesHandler (MockApplication ("application" ))
57
31
58
32
non_static_path = request_for_path ("/path/to/non/static/resource" )
59
33
assert (
@@ -70,7 +44,7 @@ async def test_staticfiles_wrapper_calls_application_for_non_static_http_request
70
44
async def test_staticfiles_wrapper_calls_application_for_non_http_paths (settings ):
71
45
settings .STATIC_URL = "/mystatic/"
72
46
73
- wrapper = StaticFilesWrapper (MockApplication ("application" ))
47
+ wrapper = ASGIStaticFilesHandler (MockApplication ("application" ))
74
48
75
49
non_http_static_path = request_for_path ("/mystatic/match" , type = "websocket" )
76
50
assert await wrapper (non_http_static_path , None , None ) == "application" , (
@@ -83,7 +57,7 @@ async def test_staticfiles_wrapper_calls_application_for_non_http_paths(settings
83
57
async def test_staticfiles_wrapper_calls_application_if_static_url_has_host (settings ):
84
58
settings .STATIC_URL = "http://hostname.com/mystatic/"
85
59
86
- wrapper = StaticFilesWrapper (MockApplication ("application" ))
60
+ wrapper = ASGIStaticFilesHandler (MockApplication ("application" ))
87
61
88
62
scope = request_for_path ("/mystatic/match" )
89
63
assert await wrapper (scope , None , None ) == "application" , (
@@ -95,33 +69,9 @@ async def test_staticfiles_wrapper_calls_application_if_static_url_has_host(sett
95
69
def test_is_single_callable ():
96
70
from asgiref .compatibility import is_double_callable
97
71
98
- wrapper = StaticFilesWrapper (None )
72
+ wrapper = ASGIStaticFilesHandler (None )
99
73
100
74
assert not is_double_callable (wrapper ), (
101
75
"StaticFilesWrapper should be recognized as a single callable by "
102
76
"asgiref compatibility tools"
103
77
)
104
-
105
-
106
- @pytest .mark .asyncio
107
- async def test_staticfiles_handler_can_generate_file_path ():
108
- """
109
- StaticFilesHandler.file_path must not rely on scope being assigned to self.
110
- """
111
-
112
- class MockedHandler (StaticFilesHandler ):
113
- async def __call__ (self , scope , receive , send ):
114
- # Equivalent setUp from real __call__.
115
- request = self .request_class (scope , "" )
116
- self .static_base_url = scope ["static_base_url" ][2 ]
117
- # Method under test.
118
- return self .file_path (request .path )
119
-
120
- wrapper = StaticFilesWrapper (
121
- MockApplication ("application" ), staticfiles_handler = MockedHandler
122
- )
123
- scope = request_for_path ("/static/image.png" )
124
- scope ["method" ] = "GET"
125
- assert await wrapper (scope , None , None ) == os .path .normpath (
126
- "/image.png"
127
- ), "StaticFilesWrapper should serve paths under the STATIC_URL path"
0 commit comments