Repository navigation
Expand file tree
/
Copy pathtest_asgi.py
More file actions
309 lines (256 loc) · 11.4 KB
/
Copy pathtest_asgi.py
File metadata and controls
309 lines (256 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import asyncio
import gzip
import inspect
from unittest import skipUnless, TestCase
from asgiref.testing import ApplicationCommunicator
from prometheus_client import CollectorRegistry, Counter, make_asgi_app
from prometheus_client.exposition import CONTENT_TYPE_PLAIN_0_0_4
try:
from starlette.applications import Starlette # type: ignore[import]
HAVE_STARLETTE = True
except ImportError:
HAVE_STARLETTE = False
def setup_testing_defaults(scope):
scope.update(
{
"client": ("127.0.0.1", 32767),
"headers": [],
"http_version": "1.0",
"method": "GET",
"path": "/",
"query_string": b"",
"scheme": "http",
"server": ("127.0.0.1", 80),
"type": "http",
}
)
class ASGITest(TestCase):
def setUp(self):
self.registry = CollectorRegistry()
self.captured_status = None
self.captured_headers = None
# Setup ASGI scope
self.scope = {}
setup_testing_defaults(self.scope)
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.communicator = None
def tearDown(self):
if self.communicator:
self.loop.run_until_complete(
self.communicator.wait()
)
self.loop.close()
def seed_app(self, app):
async def _init():
self.communicator = ApplicationCommunicator(app, self.scope)
self.loop.run_until_complete(_init())
def send_input(self, payload):
self.loop.run_until_complete(
self.communicator.send_input(payload)
)
def send_default_request(self):
self.send_input({"type": "http.request", "body": b""})
def get_output(self):
output = self.loop.run_until_complete(
self.communicator.receive_output(0)
)
return output
def get_all_output(self):
outputs = []
while True:
try:
outputs.append(self.get_output())
except asyncio.TimeoutError:
break
return outputs
def get_all_response_headers(self):
outputs = self.get_all_output()
response_start = next(o for o in outputs if o["type"] == "http.response.start")
return response_start["headers"]
def get_response_header_value(self, header_name):
response_headers = self.get_all_response_headers()
return next(
value.decode("utf-8")
for name, value in response_headers
if name.decode("utf-8") == header_name
)
def increment_metrics(self, metric_name, help_text, increments):
c = Counter(metric_name, help_text, registry=self.registry)
for _ in range(increments):
c.inc()
def assert_metrics(self, output, metric_name, help_text, increments):
self.assertIn("# HELP " + metric_name + "_total " + help_text + "\n", output)
self.assertIn("# TYPE " + metric_name + "_total counter\n", output)
self.assertIn(metric_name + "_total " + str(increments) + ".0\n", output)
def assert_not_metrics(self, output, metric_name, help_text, increments):
self.assertNotIn("# HELP " + metric_name + "_total " + help_text + "\n", output)
self.assertNotIn("# TYPE " + metric_name + "_total counter\n", output)
self.assertNotIn(metric_name + "_total " + str(increments) + ".0\n", output)
def assert_outputs(self, outputs, metric_name, help_text, increments, compressed):
self.assertEqual(len(outputs), 2)
response_start = outputs[0]
self.assertEqual(response_start['type'], 'http.response.start')
response_body = outputs[1]
self.assertEqual(response_body['type'], 'http.response.body')
# Status code
self.assertEqual(response_start['status'], 200)
# Headers
num_of_headers = 2 if compressed else 1
self.assertEqual(len(response_start['headers']), num_of_headers)
self.assertIn((b"Content-Type", CONTENT_TYPE_PLAIN_0_0_4.encode('utf8')), response_start['headers'])
if compressed:
self.assertIn((b"Content-Encoding", b"gzip"), response_start['headers'])
# Body
if compressed:
output = gzip.decompress(response_body['body']).decode('utf8')
else:
output = response_body['body'].decode('utf8')
self.assert_metrics(output, metric_name, help_text, increments)
def validate_metrics(self, metric_name, help_text, increments):
"""
ASGI app serves the metrics from the provided registry.
"""
self.increment_metrics(metric_name, help_text, increments)
# Create and run ASGI app
app = make_asgi_app(self.registry)
self.seed_app(app)
self.send_default_request()
# Assert outputs
outputs = self.get_all_output()
self.assert_outputs(outputs, metric_name, help_text, increments, compressed=False)
def test_report_metrics_1(self):
self.validate_metrics("counter", "A counter", 2)
def test_report_metrics_2(self):
self.validate_metrics("counter", "Another counter", 3)
def test_report_metrics_3(self):
self.validate_metrics("requests", "Number of requests", 5)
def test_report_metrics_4(self):
self.validate_metrics("failed_requests", "Number of failed requests", 7)
def test_gzip(self):
# Increment a metric.
metric_name = "counter"
help_text = "A counter"
increments = 2
self.increment_metrics(metric_name, help_text, increments)
app = make_asgi_app(self.registry)
# Send input with gzip header.
self.scope["headers"] = [(b"accept-encoding", b"gzip")]
self.seed_app(app)
self.send_input({"type": "http.request", "body": b""})
# Assert outputs are compressed.
outputs = self.get_all_output()
self.assert_outputs(outputs, metric_name, help_text, increments, compressed=True)
def test_gzip_disabled(self):
# Increment a metric.
metric_name = "counter"
help_text = "A counter"
increments = 2
self.increment_metrics(metric_name, help_text, increments)
# Disable compression explicitly.
app = make_asgi_app(self.registry, disable_compression=True)
# Send input with gzip header.
self.scope["headers"] = [(b"accept-encoding", b"gzip")]
self.seed_app(app)
self.send_input({"type": "http.request", "body": b""})
# Assert outputs are not compressed.
outputs = self.get_all_output()
self.assert_outputs(outputs, metric_name, help_text, increments, compressed=False)
def test_openmetrics_encoding(self):
"""Response content type is application/openmetrics-text when appropriate Accept header is in request"""
app = make_asgi_app(self.registry)
self.scope["headers"] = [(b"Accept", b"application/openmetrics-text; version=1.0.0")]
self.seed_app(app)
self.send_input({"type": "http.request", "body": b""})
content_type = self.get_response_header_value('Content-Type').split(";")[0]
assert content_type == "application/openmetrics-text"
def test_plaintext_encoding(self):
"""Response content type is text/plain when Accept header is missing in request"""
app = make_asgi_app(self.registry)
self.seed_app(app)
self.send_input({"type": "http.request", "body": b""})
content_type = self.get_response_header_value('Content-Type').split(";")[0]
assert content_type == "text/plain"
def test_qs_parsing(self):
"""Only metrics that match the 'name[]' query string param appear"""
app = make_asgi_app(self.registry)
metrics = [
("asdf", "first test metric", 1),
("bsdf", "second test metric", 2)
]
for m in metrics:
self.increment_metrics(*m)
for i_1 in range(len(metrics)):
self.scope['query_string'] = f"name[]={metrics[i_1][0]}_total".encode("utf-8")
self.seed_app(app)
self.send_default_request()
outputs = self.get_all_output()
response_body = outputs[1]
output = response_body['body'].decode('utf8')
self.assert_metrics(output, *metrics[i_1])
for i_2 in range(len(metrics)):
if i_1 == i_2:
continue
self.assert_not_metrics(output, *metrics[i_2])
self.loop.run_until_complete(
self.communicator.wait()
)
def test_qs_parsing_multi(self):
"""Only metrics that match the 'name[]' query string param appear"""
app = make_asgi_app(self.registry)
metrics = [
("asdf", "first test metric", 1),
("bsdf", "second test metric", 2),
("csdf", "third test metric", 3)
]
for m in metrics:
self.increment_metrics(*m)
self.scope['query_string'] = "&".join([f"name[]={m[0]}_total" for m in metrics[0:2]]).encode("utf-8")
self.seed_app(app)
self.send_default_request()
outputs = self.get_all_output()
response_body = outputs[1]
output = response_body['body'].decode('utf8')
self.assert_metrics(output, *metrics[0])
self.assert_metrics(output, *metrics[1])
self.assert_not_metrics(output, *metrics[2])
self.loop.run_until_complete(
self.communicator.wait()
)
def test_app_is_asgi_application_object(self):
"""make_asgi_app returns a callable object rather than a plain function.
Frameworks such as Starlette treat plain functions passed to add_route
as request/response endpoints, and any other callable as an ASGI app.
"""
app = make_asgi_app(self.registry)
self.assertFalse(inspect.isfunction(app))
self.assertFalse(inspect.ismethod(app))
self.assertTrue(inspect.iscoroutinefunction(app.__call__))
# It is still a valid ASGI application when called directly.
self.increment_metrics("counter", "A counter", 2)
self.seed_app(app)
self.send_default_request()
outputs = self.get_all_output()
self.assert_outputs(outputs, "counter", "A counter", 2, compressed=False)
def _run_starlette(self, starlette_app, path):
self.scope["path"] = path
self.scope["raw_path"] = path.encode("utf-8")
self.seed_app(starlette_app)
self.send_default_request()
return self.get_all_output()
@skipUnless(HAVE_STARLETTE, "Don't have starlette installed.")
def test_starlette_add_route(self):
"""add_route serves metrics at the exact path, without a redirect."""
self.increment_metrics("counter", "A counter", 2)
starlette_app = Starlette()
starlette_app.add_route("/metrics", make_asgi_app(self.registry))
outputs = self._run_starlette(starlette_app, "/metrics")
self.assert_outputs(outputs, "counter", "A counter", 2, compressed=False)
@skipUnless(HAVE_STARLETTE, "Don't have starlette installed.")
def test_starlette_mount(self):
"""Mounting the app as a path prefix keeps working."""
self.increment_metrics("counter", "A counter", 2)
starlette_app = Starlette()
starlette_app.mount("/metrics", make_asgi_app(self.registry))
outputs = self._run_starlette(starlette_app, "/metrics/")
self.assert_outputs(outputs, "counter", "A counter", 2, compressed=False)