@@ -61,8 +61,28 @@ async def _send(message: ASGISendEvent) -> None:
61
61
]
62
62
63
63
64
+ async def _run_app (app : WSGIWrapper , scope : HTTPScope , body : bytes = b"" ) -> List [ASGISendEvent ]:
65
+ queue : asyncio .Queue = asyncio .Queue ()
66
+ await queue .put ({"type" : "http.request" , "body" : body })
67
+
68
+ messages = []
69
+
70
+ async def _send (message : ASGISendEvent ) -> None :
71
+ nonlocal messages
72
+ messages .append (message )
73
+
74
+ event_loop = asyncio .get_running_loop ()
75
+
76
+ def _call_soon (func : Callable , * args : Any ) -> Any :
77
+ future = asyncio .run_coroutine_threadsafe (func (* args ), event_loop )
78
+ return future .result ()
79
+
80
+ await app (scope , queue .get , _send , partial (event_loop .run_in_executor , None ), _call_soon )
81
+ return messages
82
+
83
+
64
84
@pytest .mark .asyncio
65
- async def test_wsgi_asyncio (event_loop : asyncio . AbstractEventLoop ) -> None :
85
+ async def test_wsgi_asyncio () -> None :
66
86
app = WSGIWrapper (echo_body , 2 ** 16 )
67
87
scope : HTTPScope = {
68
88
"http_version" : "1.1" ,
@@ -79,20 +99,7 @@ async def test_wsgi_asyncio(event_loop: asyncio.AbstractEventLoop) -> None:
79
99
"server" : None ,
80
100
"extensions" : {},
81
101
}
82
- queue : asyncio .Queue = asyncio .Queue ()
83
- await queue .put ({"type" : "http.request" })
84
-
85
- messages = []
86
-
87
- async def _send (message : ASGISendEvent ) -> None :
88
- nonlocal messages
89
- messages .append (message )
90
-
91
- def _call_soon (func : Callable , * args : Any ) -> Any :
92
- future = asyncio .run_coroutine_threadsafe (func (* args ), event_loop )
93
- return future .result ()
94
-
95
- await app (scope , queue .get , _send , partial (event_loop .run_in_executor , None ), _call_soon )
102
+ messages = await _run_app (app , scope )
96
103
assert messages == [
97
104
{
98
105
"headers" : [(b"content-type" , b"text/plain; charset=utf-8" ), (b"content-length" , b"0" )],
@@ -105,7 +112,7 @@ def _call_soon(func: Callable, *args: Any) -> Any:
105
112
106
113
107
114
@pytest .mark .asyncio
108
- async def test_max_body_size (event_loop : asyncio . AbstractEventLoop ) -> None :
115
+ async def test_max_body_size () -> None :
109
116
app = WSGIWrapper (echo_body , 4 )
110
117
scope : HTTPScope = {
111
118
"http_version" : "1.1" ,
@@ -122,25 +129,39 @@ async def test_max_body_size(event_loop: asyncio.AbstractEventLoop) -> None:
122
129
"server" : None ,
123
130
"extensions" : {},
124
131
}
125
- queue : asyncio .Queue = asyncio .Queue ()
126
- await queue .put ({"type" : "http.request" , "body" : b"abcde" })
127
- messages = []
128
-
129
- async def _send (message : ASGISendEvent ) -> None :
130
- nonlocal messages
131
- messages .append (message )
132
-
133
- def _call_soon (func : Callable , * args : Any ) -> Any :
134
- future = asyncio .run_coroutine_threadsafe (func (* args ), event_loop )
135
- return future .result ()
136
-
137
- await app (scope , queue .get , _send , partial (event_loop .run_in_executor , None ), _call_soon )
132
+ messages = await _run_app (app , scope , b"abcde" )
138
133
assert messages == [
139
134
{"headers" : [], "status" : 400 , "type" : "http.response.start" },
140
135
{"body" : bytearray (b"" ), "type" : "http.response.body" , "more_body" : False },
141
136
]
142
137
143
138
139
+ def no_start_response (environ : dict , start_response : Callable ) -> List [bytes ]:
140
+ return [b"result" ]
141
+
142
+
143
+ @pytest .mark .asyncio
144
+ async def test_no_start_response () -> None :
145
+ app = WSGIWrapper (no_start_response , 2 ** 16 )
146
+ scope : HTTPScope = {
147
+ "http_version" : "1.1" ,
148
+ "asgi" : {},
149
+ "method" : "GET" ,
150
+ "headers" : [],
151
+ "path" : "/" ,
152
+ "root_path" : "/" ,
153
+ "query_string" : b"a=b" ,
154
+ "raw_path" : b"/" ,
155
+ "scheme" : "http" ,
156
+ "type" : "http" ,
157
+ "client" : ("localhost" , 80 ),
158
+ "server" : None ,
159
+ "extensions" : {},
160
+ }
161
+ with pytest .raises (RuntimeError ):
162
+ await _run_app (app , scope )
163
+
164
+
144
165
def test_build_environ_encoding () -> None :
145
166
scope : HTTPScope = {
146
167
"http_version" : "1.0" ,
0 commit comments