1+ import asyncio
12import gzip
2- from unittest import skipUnless , TestCase
3+ from unittest import TestCase
34
4- from prometheus_client import CollectorRegistry , Counter
5- from prometheus_client .exposition import CONTENT_TYPE_PLAIN_0_0_4
6-
7- try :
8- # Python >3.5 only
9- import asyncio
5+ from asgiref .testing import ApplicationCommunicator
106
11- from asgiref .testing import ApplicationCommunicator
12-
13- from prometheus_client import make_asgi_app
14- HAVE_ASYNCIO_AND_ASGI = True
15- except ImportError :
16- HAVE_ASYNCIO_AND_ASGI = False
7+ from prometheus_client import CollectorRegistry , Counter , make_asgi_app
8+ from prometheus_client .exposition import CONTENT_TYPE_PLAIN_0_0_4
179
1810
1911def setup_testing_defaults (scope ):
@@ -33,7 +25,6 @@ def setup_testing_defaults(scope):
3325
3426
3527class ASGITest (TestCase ):
36- @skipUnless (HAVE_ASYNCIO_AND_ASGI , "Don't have asyncio/asgi installed." )
3728 def setUp (self ):
3829 self .registry = CollectorRegistry ()
3930 self .captured_status = None
@@ -45,10 +36,8 @@ def setUp(self):
4536
4637 def tearDown (self ):
4738 if self .communicator :
48- asyncio .new_event_loop ().run_until_complete (
49- self .communicator .wait ()
50- )
51-
39+ asyncio .new_event_loop ().run_until_complete (self .communicator .wait ())
40+
5241 def seed_app (self , app ):
5342 self .communicator = ApplicationCommunicator (app , self .scope )
5443
@@ -106,22 +95,25 @@ def assert_not_metrics(self, output, metric_name, help_text, increments):
10695 def assert_outputs (self , outputs , metric_name , help_text , increments , compressed ):
10796 self .assertEqual (len (outputs ), 2 )
10897 response_start = outputs [0 ]
109- self .assertEqual (response_start [' type' ], ' http.response.start' )
98+ self .assertEqual (response_start [" type" ], " http.response.start" )
11099 response_body = outputs [1 ]
111- self .assertEqual (response_body [' type' ], ' http.response.body' )
100+ self .assertEqual (response_body [" type" ], " http.response.body" )
112101 # Status code
113- self .assertEqual (response_start [' status' ], 200 )
102+ self .assertEqual (response_start [" status" ], 200 )
114103 # Headers
115104 num_of_headers = 2 if compressed else 1
116- self .assertEqual (len (response_start ['headers' ]), num_of_headers )
117- self .assertIn ((b"Content-Type" , CONTENT_TYPE_PLAIN_0_0_4 .encode ('utf8' )), response_start ['headers' ])
105+ self .assertEqual (len (response_start ["headers" ]), num_of_headers )
106+ self .assertIn (
107+ (b"Content-Type" , CONTENT_TYPE_PLAIN_0_0_4 .encode ("utf8" )),
108+ response_start ["headers" ],
109+ )
118110 if compressed :
119- self .assertIn ((b"Content-Encoding" , b"gzip" ), response_start [' headers' ])
111+ self .assertIn ((b"Content-Encoding" , b"gzip" ), response_start [" headers" ])
120112 # Body
121113 if compressed :
122- output = gzip .decompress (response_body [' body' ]).decode (' utf8' )
114+ output = gzip .decompress (response_body [" body" ]).decode (" utf8" )
123115 else :
124- output = response_body [' body' ].decode (' utf8' )
116+ output = response_body [" body" ].decode (" utf8" )
125117
126118 self .assert_metrics (output , metric_name , help_text , increments )
127119
@@ -136,7 +128,9 @@ def validate_metrics(self, metric_name, help_text, increments):
136128 self .send_default_request ()
137129 # Assert outputs
138130 outputs = self .get_all_output ()
139- self .assert_outputs (outputs , metric_name , help_text , increments , compressed = False )
131+ self .assert_outputs (
132+ outputs , metric_name , help_text , increments , compressed = False
133+ )
140134
141135 def test_report_metrics_1 (self ):
142136 self .validate_metrics ("counter" , "A counter" , 2 )
@@ -163,7 +157,9 @@ def test_gzip(self):
163157 self .send_input ({"type" : "http.request" , "body" : b"" })
164158 # Assert outputs are compressed.
165159 outputs = self .get_all_output ()
166- self .assert_outputs (outputs , metric_name , help_text , increments , compressed = True )
160+ self .assert_outputs (
161+ outputs , metric_name , help_text , increments , compressed = True
162+ )
167163
168164 def test_gzip_disabled (self ):
169165 # Increment a metric.
@@ -179,16 +175,20 @@ def test_gzip_disabled(self):
179175 self .send_input ({"type" : "http.request" , "body" : b"" })
180176 # Assert outputs are not compressed.
181177 outputs = self .get_all_output ()
182- self .assert_outputs (outputs , metric_name , help_text , increments , compressed = False )
178+ self .assert_outputs (
179+ outputs , metric_name , help_text , increments , compressed = False
180+ )
183181
184182 def test_openmetrics_encoding (self ):
185183 """Response content type is application/openmetrics-text when appropriate Accept header is in request"""
186184 app = make_asgi_app (self .registry )
187185 self .seed_app (app )
188- self .scope ["headers" ] = [(b"Accept" , b"application/openmetrics-text; version=1.0.0" )]
186+ self .scope ["headers" ] = [
187+ (b"Accept" , b"application/openmetrics-text; version=1.0.0" )
188+ ]
189189 self .send_input ({"type" : "http.request" , "body" : b"" })
190190
191- content_type = self .get_response_header_value (' Content-Type' ).split (";" )[0 ]
191+ content_type = self .get_response_header_value (" Content-Type" ).split (";" )[0 ]
192192 assert content_type == "application/openmetrics-text"
193193
194194 def test_plaintext_encoding (self ):
@@ -197,29 +197,28 @@ def test_plaintext_encoding(self):
197197 self .seed_app (app )
198198 self .send_input ({"type" : "http.request" , "body" : b"" })
199199
200- content_type = self .get_response_header_value (' Content-Type' ).split (";" )[0 ]
200+ content_type = self .get_response_header_value (" Content-Type" ).split (";" )[0 ]
201201 assert content_type == "text/plain"
202202
203203 def test_qs_parsing (self ):
204204 """Only metrics that match the 'name[]' query string param appear"""
205205
206206 app = make_asgi_app (self .registry )
207- metrics = [
208- ("asdf" , "first test metric" , 1 ),
209- ("bsdf" , "second test metric" , 2 )
210- ]
207+ metrics = [("asdf" , "first test metric" , 1 ), ("bsdf" , "second test metric" , 2 )]
211208
212209 for m in metrics :
213210 self .increment_metrics (* m )
214211
215212 for i_1 in range (len (metrics )):
216213 self .seed_app (app )
217- self .scope ['query_string' ] = f"name[]={ metrics [i_1 ][0 ]} _total" .encode ("utf-8" )
214+ self .scope ["query_string" ] = f"name[]={ metrics [i_1 ][0 ]} _total" .encode (
215+ "utf-8"
216+ )
218217 self .send_default_request ()
219218
220219 outputs = self .get_all_output ()
221220 response_body = outputs [1 ]
222- output = response_body [' body' ].decode (' utf8' )
221+ output = response_body [" body" ].decode (" utf8" )
223222
224223 self .assert_metrics (output , * metrics [i_1 ])
225224
@@ -229,6 +228,4 @@ def test_qs_parsing(self):
229228
230229 self .assert_not_metrics (output , * metrics [i_2 ])
231230
232- asyncio .new_event_loop ().run_until_complete (
233- self .communicator .wait ()
234- )
231+ asyncio .new_event_loop ().run_until_complete (self .communicator .wait ())
0 commit comments