harden dashboard API contracts and proxy errors

This commit is contained in:
Marco0300
2026-09-03 23:20:47 +02:00
parent 594da00240
commit 00bd49a894
4 changed files with 138 additions and 10 deletions
+20
View File
@@ -1,10 +1,14 @@
import http.client
import json
import os
import threading
import unittest
from unittest.mock import patch
from server import ProxyStaticHandler
from http.server import ThreadingHTTPServer
RealHTTPConnection = http.client.HTTPConnection
class FakeUpstream:
@@ -30,6 +34,22 @@ class ProxyServerTests(unittest.TestCase):
with patch('server.http.client.HTTPConnection', FakeUpstream):
self.assertTrue(server.proxy_api)
def test_upstream_failure_is_structured_json(self):
httpd = ThreadingHTTPServer(("127.0.0.1", 0), ProxyStaticHandler)
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
thread.start()
try:
with patch('server.http.client.HTTPConnection', side_effect=OSError('connection refused')):
conn = RealHTTPConnection('127.0.0.1', httpd.server_port, timeout=3)
conn.request('GET', '/api/v1/dashboard/summary')
response = conn.getresponse()
body = response.read()
self.assertEqual(response.status, 502)
self.assertEqual(response.getheader('Content-Type'), 'application/json; charset=utf-8')
self.assertEqual(json.loads(body), {'error': 'api_upstream_unavailable'})
finally:
httpd.shutdown(); httpd.server_close(); thread.join(timeout=2)
if __name__ == '__main__':
unittest.main()