Skip to content

Commit 6b9b592

Browse files
committed
add tests for aiohttp
1 parent 1202325 commit 6b9b592

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
from aiohttp import web
3+
from aiohttp.test_utils import TestClient, TestServer
4+
from prometheus_client.openmetrics.exposition import (
5+
CONTENT_TYPE_LATEST,
6+
)
7+
from servicelib.aiohttp.monitoring import setup_monitoring
8+
9+
10+
@pytest.fixture
11+
def aiohttp_app_with_monitoring():
12+
app = web.Application()
13+
setup_monitoring(app, app_name="test_app")
14+
return app
15+
16+
17+
@pytest.fixture
18+
async def client(aiohttp_app_with_monitoring):
19+
async with TestServer(aiohttp_app_with_monitoring) as server:
20+
async with TestClient(server) as client:
21+
yield client
22+
23+
24+
async def test_metrics_endpoint(client):
25+
response = await client.get("/metrics")
26+
assert response.status == 200
27+
assert response.headers["Content-Type"] == CONTENT_TYPE_LATEST
28+
body = await response.text()
29+
assert "# HELP" in body # Check for Prometheus metrics format

packages/service-library/tests/fastapi/test_prometheus_middleware.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from asgi_lifespan import LifespanManager
55
from fastapi import FastAPI
66
from httpx import AsyncClient
7+
from prometheus_client.openmetrics.exposition import CONTENT_TYPE_LATEST
78
from servicelib.fastapi.monitoring import setup_prometheus_instrumentation
89

910

@@ -23,5 +24,6 @@ async def test_metrics_endpoint(client: AsyncClient, app: FastAPI):
2324
"""
2425
response = await client.get("/metrics")
2526
assert response.status_code == 200
27+
assert response.headers["Content-Type"] == CONTENT_TYPE_LATEST
2628
assert "# HELP" in response.text
2729
assert "# TYPE" in response.text

0 commit comments

Comments
 (0)