@@ -75,6 +75,19 @@ def get_all_output(self):
75
75
break
76
76
return outputs
77
77
78
+ def get_all_response_headers (self ):
79
+ outputs = self .get_all_output ()
80
+ response_start = next (o for o in outputs if o ["type" ] == "http.response.start" )
81
+ return response_start ["headers" ]
82
+
83
+ def get_response_header_value (self , header_name ):
84
+ response_headers = self .get_all_response_headers ()
85
+ return next (
86
+ value .decode ("utf-8" )
87
+ for name , value in response_headers
88
+ if name .decode ("utf-8" ) == header_name
89
+ )
90
+
78
91
def increment_metrics (self , metric_name , help_text , increments ):
79
92
c = Counter (metric_name , help_text , registry = self .registry )
80
93
for _ in range (increments ):
@@ -158,3 +171,22 @@ def test_gzip_disabled(self):
158
171
# Assert outputs are not compressed.
159
172
outputs = self .get_all_output ()
160
173
self .assert_outputs (outputs , metric_name , help_text , increments , compressed = False )
174
+
175
+ def test_openmetrics_encoding (self ):
176
+ """Response content type is application/openmetrics-text when appropriate Accept header is in request"""
177
+ app = make_asgi_app (self .registry )
178
+ self .seed_app (app )
179
+ self .scope ["headers" ] = [(b"Accept" , b"application/openmetrics-text" )]
180
+ self .send_input ({"type" : "http.request" , "body" : b"" })
181
+
182
+ content_type = self .get_response_header_value ('Content-Type' ).split (";" )[0 ]
183
+ assert content_type == "application/openmetrics-text"
184
+
185
+ def test_plaintext_encoding (self ):
186
+ """Response content type is text/plain when Accept header is missing in request"""
187
+ app = make_asgi_app (self .registry )
188
+ self .seed_app (app )
189
+ self .send_input ({"type" : "http.request" , "body" : b"" })
190
+
191
+ content_type = self .get_response_header_value ('Content-Type' ).split (";" )[0 ]
192
+ assert content_type == "text/plain"
0 commit comments