From a31b9cf144506ff8c5999fd663362fc857b55a5d Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Thu, 18 Jun 2026 17:04:58 +0200 Subject: [PATCH] fix: proxy --- psa_car_controller/web/app.py | 3 ++- tests/test_ha_ingress_integration.py | 33 +++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/psa_car_controller/web/app.py b/psa_car_controller/web/app.py index 28520eb..13daafa 100644 --- a/psa_car_controller/web/app.py +++ b/psa_car_controller/web/app.py @@ -27,7 +27,7 @@ logger = logging.getLogger(__name__) class MyProxyFix(ProxyFix): - def __init__(self, dashapp, static_prefix): + def __init__(self, dashapp: Dash, static_prefix): self.flask_app = dashapp.server self.dash_app = dashapp self.static_prefix = static_prefix @@ -41,6 +41,7 @@ class MyProxyFix(ProxyFix): self.dash_app.config.requests_pathname_prefix = "" self.dash_app.config.url_base_pathname = None else: + environ["HTTP_X_FORWARDED_PREFIX"] = prefix if not prefix.endswith("/"): prefix += "/" self.dash_app.config.requests_pathname_prefix = prefix diff --git a/tests/test_ha_ingress_integration.py b/tests/test_ha_ingress_integration.py index f65ac38..7640db6 100644 --- a/tests/test_ha_ingress_integration.py +++ b/tests/test_ha_ingress_integration.py @@ -8,7 +8,6 @@ the HTML response contain the prefix. Run with: python -m unittest tests.test_ha_ingress_integration -v or: python tests/test_ha_ingress_integration.py """ -import psa_car_controller import os import sys import time @@ -16,8 +15,7 @@ import re import socket import requests import threading -from unittest import TestCase, skipIf -from unittest.mock import patch, MagicMock +from unittest import TestCase # Add parent directory to path sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) @@ -186,6 +184,35 @@ class TestHomeAssistantIngressIntegration(TestCase): print(f"API request failed (may need auth): {e}") # This is OK - we're just checking the server is running + def test_style_json_with_ingress_path(self): + """ + Test that /style.json returns JSON with sprite path containing the prefix + when X-Ingress-Path header is present. + """ + prefix = '/api/ingress/a93a74ea_psacc/' + headers = { + 'X-Ingress-Path': prefix, + 'Host': f'127.0.0.1:{self.server_port}' + } + + url = f"{self.server_url}/style.json" + expected = f"{self.server_url}{prefix}assets/sprites/osm-liberty" + try: + # todo lower timeout + response = requests.get(url, headers=headers, timeout=3600) + + self.assertEqual(response.status_code, 200, + f"Expected 200, got {response.status_code}") + + data = response.json() + self.assertIn('sprite', data, "Response should contain 'sprite' key") + + sprite_path = data['sprite'] + self.assertEqual(sprite_path, expected) + + except requests.exceptions.RequestException as e: + self.fail(f"Request failed: {e}") + if __name__ == '__main__': import unittest