From 1fb8bf73e7779d038c35ccd294083b10301a2a90 Mon Sep 17 00:00:00 2001 From: ftsell Date: Mon, 12 Feb 2024 21:12:36 +0100 Subject: [PATCH] add generated tests for metrics endpoint --- .../metrics/tests/__init__.py | 0 .../metrics/tests/test_metric_exposition.py | 7 +++++++ .../src/{{ cookiecutter.project_slug }}/metrics/views.py | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 {{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/tests/__init__.py create mode 100644 {{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/tests/test_metric_exposition.py diff --git a/{{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/tests/__init__.py b/{{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/tests/test_metric_exposition.py b/{{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/tests/test_metric_exposition.py new file mode 100644 index 0000000..f18f8d7 --- /dev/null +++ b/{{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/tests/test_metric_exposition.py @@ -0,0 +1,7 @@ +from django.shortcuts import resolve_url +from django.test import Client + + +def test_metric_endpoint_returns_200(client: Client): + response = client.get(resolve_url("prometheus_metrics")) + assert response.status_code == 200 diff --git a/{{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/views.py b/{{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/views.py index a618fbf..b5d67b9 100644 --- a/{{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/views.py +++ b/{{ cookiecutter.project_slug }}/src/{{ cookiecutter.project_slug }}/metrics/views.py @@ -27,6 +27,6 @@ def get(self, request: HttpRequest) -> HttpResponse: if not self.is_allowed(request): return HttpResponse(status=HTTPStatus.FORBIDDEN) - encode, content_type = prometheus_client.exposition.choose_encoder(request.META["HTTP_ACCEPT"]) + encode, content_type = prometheus_client.exposition.choose_encoder(request.META.get("HTTP_ACCEPT")) content = encode(REGISTRY) return HttpResponse(status=HTTPStatus.OK, content=content, content_type=content_type)