Skip to content

Commit 271b028

Browse files
committed
isolate the asgi qs encoding bug into a test case
1 parent 6bb8f79 commit 271b028

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

tests/test_asgi.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ def increment_metrics(self, metric_name, help_text, increments):
9393
for _ in range(increments):
9494
c.inc()
9595

96+
def assert_metrics(self, output, metric_name, help_text, increments):
97+
self.assertIn("# HELP " + metric_name + "_total " + help_text + "\n", output)
98+
self.assertIn("# TYPE " + metric_name + "_total counter\n", output)
99+
self.assertIn(metric_name + "_total " + str(increments) + ".0\n", output)
100+
101+
def assert_not_metrics(self, output, metric_name, help_text, increments):
102+
self.assertNotIn("# HELP " + metric_name + "_total " + help_text + "\n", output)
103+
self.assertNotIn("# TYPE " + metric_name + "_total counter\n", output)
104+
self.assertNotIn(metric_name + "_total " + str(increments) + ".0\n", output)
105+
96106
def assert_outputs(self, outputs, metric_name, help_text, increments, compressed):
97107
self.assertEqual(len(outputs), 2)
98108
response_start = outputs[0]
@@ -112,9 +122,8 @@ def assert_outputs(self, outputs, metric_name, help_text, increments, compressed
112122
output = gzip.decompress(response_body['body']).decode('utf8')
113123
else:
114124
output = response_body['body'].decode('utf8')
115-
self.assertIn("# HELP " + metric_name + "_total " + help_text + "\n", output)
116-
self.assertIn("# TYPE " + metric_name + "_total counter\n", output)
117-
self.assertIn(metric_name + "_total " + str(increments) + ".0\n", output)
125+
126+
self.assert_metrics(output, metric_name, help_text, increments)
118127

119128
def validate_metrics(self, metric_name, help_text, increments):
120129
"""
@@ -190,3 +199,36 @@ def test_plaintext_encoding(self):
190199

191200
content_type = self.get_response_header_value('Content-Type').split(";")[0]
192201
assert content_type == "text/plain"
202+
203+
def test_qs_parsing(self):
204+
"""Only metrics that match the 'name[]' query string param appear"""
205+
206+
app = make_asgi_app(self.registry)
207+
metrics = [
208+
("asdf", "first test metric", 1),
209+
("bsdf", "second test metric", 2)
210+
]
211+
212+
for m in metrics:
213+
self.increment_metrics(*m)
214+
215+
for i_1 in range(len(metrics)):
216+
self.seed_app(app)
217+
self.scope['query_string'] = f"name[]={metrics[i_1][0]}_total".encode("utf-8")
218+
self.send_default_request()
219+
220+
outputs = self.get_all_output()
221+
response_body = outputs[1]
222+
output = response_body['body'].decode('utf8')
223+
224+
self.assert_metrics(output, *metrics[i_1])
225+
226+
for i_2 in range(len(metrics)):
227+
if i_1 == i_2:
228+
continue
229+
230+
self.assert_not_metrics(output, *metrics[i_2])
231+
232+
asyncio.get_event_loop().run_until_complete(
233+
self.communicator.wait()
234+
)

0 commit comments

Comments
 (0)