1111
1212import pytest
1313
14- try :
15- import httpx
16- except ImportError :
17- httpx = None # type: ignore
14+ from falcon import testing
15+
16+ from . import _asgi_test_app
17+
18+
19+ @pytest .fixture (scope = 'session' )
20+ def httpx ():
21+ return pytest .importorskip ('httpx' )
22+
23+
24+ @pytest .fixture (scope = 'session' )
25+ def requests ():
26+ return pytest .importorskip ('requests' )
1827
19- try :
20- import requests
21- import requests .exceptions
22- except ImportError :
23- requests = None # type: ignore
2428
2529try :
2630 import websockets
3034 websockets = None # type: ignore
3135
3236
33- from falcon import testing
34-
35- from . import _asgi_test_app
36-
3737_MODULE_DIR = os .path .abspath (os .path .dirname (__file__ ))
3838
3939_PYPY = platform .python_implementation () == 'PyPy'
4949_REQUEST_TIMEOUT = 10
5050
5151
52- @pytest .mark .skipif (
53- requests is None , reason = 'requests module is required for this test'
54- )
5552class TestASGIServer :
56- def test_get (self , server_base_url ):
53+ def test_get (self , server_base_url , requests ):
5754 resp = requests .get (server_base_url , timeout = _REQUEST_TIMEOUT )
5855 assert resp .status_code == 200
5956 assert resp .text == '127.0.0.1'
6057
61- def test_put (self , server_base_url ):
58+ def test_put (self , server_base_url , requests ):
6259 body = '{}'
6360 resp = requests .put (server_base_url , data = body , timeout = _REQUEST_TIMEOUT )
6461 assert resp .status_code == 200
6562 assert resp .text == '{}'
6663
67- def test_head_405 (self , server_base_url ):
64+ def test_head_405 (self , server_base_url , requests ):
6865 body = '{}'
6966 resp = requests .head (server_base_url , data = body , timeout = _REQUEST_TIMEOUT )
7067 assert resp .status_code == 405
7168
72- def test_post_multipart_form (self , server_base_url ):
69+ def test_post_multipart_form (self , server_base_url , requests ):
7370 size = random .randint (16 * _SIZE_1_MB , 32 * _SIZE_1_MB )
7471 data = os .urandom (size )
7572 digest = hashlib .sha1 (data ).hexdigest ()
@@ -93,7 +90,7 @@ def test_post_multipart_form(self, server_base_url):
9390 },
9491 }
9592
96- def test_post_multiple (self , server_base_url ):
93+ def test_post_multiple (self , server_base_url , requests ):
9794 body = testing .rand_string (_SIZE_1_KB // 2 , _SIZE_1_KB )
9895 resp = requests .post (server_base_url , data = body , timeout = _REQUEST_TIMEOUT )
9996 assert resp .status_code == 200
@@ -105,7 +102,7 @@ def test_post_multiple(self, server_base_url):
105102 resp = requests .post (server_base_url , data = body , timeout = _REQUEST_TIMEOUT )
106103 assert resp .headers ['X-Counter' ] == '2002'
107104
108- def test_post_invalid_content_length (self , server_base_url ):
105+ def test_post_invalid_content_length (self , server_base_url , requests ):
109106 headers = {'Content-Length' : 'invalid' }
110107
111108 try :
@@ -124,15 +121,15 @@ def test_post_invalid_content_length(self, server_base_url):
124121 # get a heads-up if the request is no longer blocked.
125122 pass
126123
127- def test_post_read_bounded_stream (self , server_base_url ):
124+ def test_post_read_bounded_stream (self , server_base_url , requests ):
128125 body = testing .rand_string (_SIZE_1_KB // 2 , _SIZE_1_KB )
129126 resp = requests .post (
130127 server_base_url + 'bucket' , data = body , timeout = _REQUEST_TIMEOUT
131128 )
132129 assert resp .status_code == 200
133130 assert resp .text == body
134131
135- def test_post_read_bounded_stream_large (self , server_base_url ):
132+ def test_post_read_bounded_stream_large (self , server_base_url , requests ):
136133 """Test that we can correctly read large bodies chunked server-side.
137134
138135 ASGI servers typically employ some type of flow control to stream
@@ -152,11 +149,11 @@ def test_post_read_bounded_stream_large(self, server_base_url):
152149 assert resp .json ().get ('drops' ) > size_mb
153150 assert resp .json ().get ('sha1' ) == hashlib .sha1 (body ).hexdigest ()
154151
155- def test_post_read_bounded_stream_no_body (self , server_base_url ):
152+ def test_post_read_bounded_stream_no_body (self , server_base_url , requests ):
156153 resp = requests .post (server_base_url + 'bucket' , timeout = _REQUEST_TIMEOUT )
157154 assert not resp .text
158155
159- def test_sse (self , server_base_url ):
156+ def test_sse (self , server_base_url , requests ):
160157 resp = requests .get (server_base_url + 'events' , timeout = _REQUEST_TIMEOUT )
161158 assert resp .status_code == 200
162159
@@ -167,7 +164,7 @@ def test_sse(self, server_base_url):
167164
168165 assert not events [- 1 ]
169166
170- def test_sse_client_disconnects_early (self , server_base_url ):
167+ def test_sse_client_disconnects_early (self , server_base_url , requests ):
171168 """Test that when the client connection is lost, the server task does not hang.
172169
173170 In the case of SSE, Falcon should detect when the client connection is
@@ -182,8 +179,7 @@ def test_sse_client_disconnects_early(self, server_base_url):
182179 timeout = (_asgi_test_app .SSE_TEST_MAX_DELAY_SEC / 2 ),
183180 )
184181
185- @pytest .mark .skipif (httpx is None , reason = 'httpx is required for this test' )
186- async def test_stream_chunked_request (self , server_base_url ):
182+ async def test_stream_chunked_request (self , server_base_url , httpx ):
187183 """Regression test for https://github.com/falconry/falcon/issues/2024"""
188184
189185 async def emitter ():
@@ -200,9 +196,6 @@ async def emitter():
200196 assert resp .json ().get ('drops' ) >= 1
201197
202198
203- @pytest .mark .skipif (
204- requests is None , reason = 'requests module is required for this test'
205- )
206199@pytest .mark .skipif (
207200 websockets is None , reason = 'websockets is required for this test class'
208201)
@@ -217,6 +210,7 @@ async def test_hello(
217210 max_receive_queue ,
218211 server_base_url ,
219212 server_url_events_ws ,
213+ requests ,
220214 ):
221215 resp = requests .patch (
222216 server_base_url + 'wsoptions' , json = {'max_receive_queue' : max_receive_queue }
@@ -617,7 +611,7 @@ def _can_run(factory):
617611
618612
619613@pytest .fixture (params = [_uvicorn_factory , _daphne_factory , _hypercorn_factory ])
620- def server_base_url (request ):
614+ def server_base_url (request , requests ):
621615 process_factory = request .param
622616 _can_run (process_factory )
623617
0 commit comments