From f854479d2418ae6717297036784daa9bff146808 Mon Sep 17 00:00:00 2001 From: mengz <2567587994@qq.com> Date: Tue, 9 Jun 2026 11:00:57 +0800 Subject: [PATCH 1/2] Return JSON body from health endpoint --- mx_exporter/__init__.py | 3 +++ tests/test_http_handler.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/test_http_handler.py diff --git a/mx_exporter/__init__.py b/mx_exporter/__init__.py index e68574b..4d08897 100644 --- a/mx_exporter/__init__.py +++ b/mx_exporter/__init__.py @@ -71,9 +71,12 @@ def do_GET(self): self.wfile.write(html_content.encode("utf-8")) elif self.path == '/health': + body = json.dumps({"status": "ok"}).encode('utf-8') self.send_response(200) self.send_header('Content-type', 'application/json') + self.send_header('Content-Length', str(len(body))) self.end_headers() + self.wfile.write(body) elif self.path == '/json': self._handle_json() diff --git a/tests/test_http_handler.py b/tests/test_http_handler.py new file mode 100644 index 0000000..30c0e3f --- /dev/null +++ b/tests/test_http_handler.py @@ -0,0 +1,32 @@ +import io + +from mx_exporter import MxExporterHandler + + +class DummyRequest: + def makefile(self, *args, **kwargs): + return io.BytesIO() + + +def build_handler(path): + handler = MxExporterHandler.__new__(MxExporterHandler) + handler.path = path + handler.request_version = "HTTP/1.1" + handler.command = "GET" + handler.requestline = "GET %s HTTP/1.1" % path + handler.wfile = io.BytesIO() + handler.rfile = io.BytesIO() + handler.client_address = ("127.0.0.1", 0) + handler.server = object() + handler.close_connection = False + return handler + + +def test_health_endpoint_returns_json_body(): + handler = build_handler("/health") + + handler.do_GET() + + response = handler.wfile.getvalue() + assert b"200 OK" in response + assert b'{"status": "ok"}' in response From 97acb8fbfa0e5b0ac985766f55eb3ddaabd6e1bf Mon Sep 17 00:00:00 2001 From: papertager <2567587994@qq.com> Date: Thu, 11 Jun 2026 00:12:48 +0800 Subject: [PATCH 2/2] Align health handler response headers --- mx_exporter/__init__.py | 2 +- tests/test_http_handler.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/mx_exporter/__init__.py b/mx_exporter/__init__.py index 4d08897..11eb080 100644 --- a/mx_exporter/__init__.py +++ b/mx_exporter/__init__.py @@ -73,7 +73,7 @@ def do_GET(self): elif self.path == '/health': body = json.dumps({"status": "ok"}).encode('utf-8') self.send_response(200) - self.send_header('Content-type', 'application/json') + self.send_header('Content-Type', 'application/json') self.send_header('Content-Length', str(len(body))) self.end_headers() self.wfile.write(body) diff --git a/tests/test_http_handler.py b/tests/test_http_handler.py index 30c0e3f..ba84c0b 100644 --- a/tests/test_http_handler.py +++ b/tests/test_http_handler.py @@ -1,11 +1,13 @@ import io +import sys +import types -from mx_exporter import MxExporterHandler +stub = types.ModuleType("mx_exporter.mx_exporter") +stub.MxCollector = object +sys.modules.setdefault("mx_exporter.mx_exporter", stub) -class DummyRequest: - def makefile(self, *args, **kwargs): - return io.BytesIO() +from mx_exporter import MxExporterHandler def build_handler(path):