diff --git a/mx_exporter/__init__.py b/mx_exporter/__init__.py index e68574b..11eb080 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-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..ba84c0b --- /dev/null +++ b/tests/test_http_handler.py @@ -0,0 +1,34 @@ +import io +import sys +import types + + +stub = types.ModuleType("mx_exporter.mx_exporter") +stub.MxCollector = object +sys.modules.setdefault("mx_exporter.mx_exporter", stub) + +from mx_exporter import MxExporterHandler + + +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