-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
88 lines (63 loc) · 2.48 KB
/
tests.py
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
from asgi_tools.tests import ASGITestClient
from asgi_tools.app import App
async def test_base():
from asgi_prometheus import PrometheusMiddleware
async def app(scope, receive, send):
await send({"type": "http.response.start", "status": 200})
await send({"type": "http.response.body", "body": b"OK", "more_body": False})
app = PrometheusMiddleware(app, metrics_url='/metrics')
client = ASGITestClient(app)
res = await client.get('/')
assert res.status_code == 200
assert await res.text() == 'OK'
res = await client.get('/metrics')
assert res.status_code == 200
text = await res.text()
assert text
assert 'requests_count_total' in text
assert 'requests_time' in text
async def test_group_path():
from asgi_prometheus import PrometheusMiddleware
async def app(scope, receive, send):
await send({"type": "http.response.start", "status": 200})
await send({"type": "http.response.body", "body": b"OK", "more_body": False})
app = PrometheusMiddleware(app, group_paths={'/api', '/api/v1/users'})
client = ASGITestClient(app)
res = await client.get('/')
assert res.status_code == 200
assert await res.text() == 'OK'
res = await client.get('/api/v1/users')
assert res.status_code == 200
res = await client.get('/api/v1/messages')
assert res.status_code == 200
res = await client.get('/unknown')
assert res.status_code == 200
res = await client.get('/prometheus')
assert res.status_code == 200
text = await res.text()
assert 'requests_count_total{method="GET",path="/api*"}' in text
assert 'requests_count_total{method="GET",path="/api/v1/users*"}' in text
async def test_asgi_tools_internal():
from asgi_prometheus import PrometheusMiddleware
app = App()
app.middleware(PrometheusMiddleware)
client = ASGITestClient(app)
res = await client.get('/')
assert res.status_code == 404
res = await client.get('/prometheus')
assert res.status_code == 200
text = await res.text()
assert text
assert 'requests_count_total' in text
async def test_asgi_tools_external():
from asgi_prometheus import PrometheusMiddleware
app = App()
app = PrometheusMiddleware(app)
client = ASGITestClient(app)
res = await client.get('/')
assert res.status_code == 404
res = await client.get('/prometheus')
assert res.status_code == 200
text = await res.text()
assert text
assert 'requests_count_total' in text