Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mx_exporter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions tests/test_http_handler.py
Original file line number Diff line number Diff line change
@@ -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