From: Jérôme Benoit Date: Tue, 17 Mar 2026 11:45:49 +0000 (+0100) Subject: fix(ocpp-server): update to websockets 16.x API (request.headers, request.path) X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=9ea92a4d4e0910b0f1d5431ccbc88807d6cf8674;p=e-mobility-charging-stations-simulator.git fix(ocpp-server): update to websockets 16.x API (request.headers, request.path) --- diff --git a/tests/ocpp-server/server.py b/tests/ocpp-server/server.py index b8601630..63fba3e5 100644 --- a/tests/ocpp-server/server.py +++ b/tests/ocpp-server/server.py @@ -122,9 +122,11 @@ class ChargePoint(ocpp.v201.ChargePoint): charge_points: set["ChargePoint"] | None = None, ): # Extract CP ID from last URL segment (OCPP 2.0.1 Part 4) - cp_id = connection.path.strip("/").split("/")[-1] + cp_id = connection.request.path.strip("/").split("/")[-1] if cp_id == "": - logger.warning("Empty CP ID extracted from path: %s", connection.path) + logger.warning( + "Empty CP ID extracted from path: %s", connection.request.path + ) super().__init__(cp_id, connection) self._charge_points = charge_points if charge_points is not None else set() self._command_timer = None @@ -620,7 +622,7 @@ async def on_connect( ): """Handle new WebSocket connections from charge points.""" try: - requested_protocols = websocket.request_headers["Sec-WebSocket-Protocol"] + requested_protocols = websocket.request.headers["Sec-WebSocket-Protocol"] except KeyError: logger.info("Client hasn't requested any Subprotocol. Closing Connection") return await websocket.close() diff --git a/tests/ocpp-server/test_server.py b/tests/ocpp-server/test_server.py index 8a1829c3..d53f8b65 100644 --- a/tests/ocpp-server/test_server.py +++ b/tests/ocpp-server/test_server.py @@ -72,7 +72,8 @@ TEST_CUSTOM_COST = 42.50 def mock_connection(): """Create a mock WebSocket connection for ChargePoint instantiation.""" conn = MagicMock() - conn.path = TEST_CHARGE_POINT_PATH + conn.request = MagicMock() + conn.request.path = TEST_CHARGE_POINT_PATH return conn @@ -989,7 +990,8 @@ class TestOnConnect: async def test_missing_subprotocol_header_closes_connection(self): mock_ws = MagicMock() - mock_ws.request_headers = {} + mock_ws.request = MagicMock() + mock_ws.request.headers = {} mock_ws.close = AsyncMock() config = self._make_config() @@ -998,7 +1000,8 @@ class TestOnConnect: async def test_protocol_mismatch_closes_connection(self): mock_ws = MagicMock() - mock_ws.request_headers = {"Sec-WebSocket-Protocol": "ocpp1.6"} + mock_ws.request = MagicMock() + mock_ws.request.headers = {"Sec-WebSocket-Protocol": "ocpp1.6"} mock_ws.subprotocol = None mock_ws.close = AsyncMock() config = self._make_config() @@ -1008,9 +1011,10 @@ class TestOnConnect: async def test_successful_connection_creates_charge_point(self): mock_ws = MagicMock() - mock_ws.request_headers = {"Sec-WebSocket-Protocol": "ocpp2.0.1"} + mock_ws.request = MagicMock() + mock_ws.request.headers = {"Sec-WebSocket-Protocol": "ocpp2.0.1"} mock_ws.subprotocol = "ocpp2.0.1" - mock_ws.path = "/TestCP" + mock_ws.request.path = "/TestCP" mock_ws.close = AsyncMock() config = self._make_config() @@ -1025,9 +1029,10 @@ class TestOnConnect: from websockets.frames import Close mock_ws = MagicMock() - mock_ws.request_headers = {"Sec-WebSocket-Protocol": "ocpp2.0.1"} + mock_ws.request = MagicMock() + mock_ws.request.headers = {"Sec-WebSocket-Protocol": "ocpp2.0.1"} mock_ws.subprotocol = "ocpp2.0.1" - mock_ws.path = "/TestCP" + mock_ws.request.path = "/TestCP" mock_ws.close = AsyncMock() config = self._make_config() @@ -1043,9 +1048,10 @@ class TestOnConnect: async def test_command_sent_on_connect_when_specified(self): mock_ws = MagicMock() - mock_ws.request_headers = {"Sec-WebSocket-Protocol": "ocpp2.0.1"} + mock_ws.request = MagicMock() + mock_ws.request.headers = {"Sec-WebSocket-Protocol": "ocpp2.0.1"} mock_ws.subprotocol = "ocpp2.0.1" - mock_ws.path = "/TestCP" + mock_ws.request.path = "/TestCP" mock_ws.close = AsyncMock() config = self._make_config( command_name=Action.clear_cache, delay=1.0, period=None