]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
fix(ocpp-server): update to websockets 16.x API (request.headers, request.path)
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 17 Mar 2026 11:45:49 +0000 (12:45 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 17 Mar 2026 11:45:49 +0000 (12:45 +0100)
tests/ocpp-server/server.py
tests/ocpp-server/test_server.py

index b8601630bd6eaca26659d49ad4d474ede3ed496a..63fba3e5d9c795d34587283b54e449c1e41a500d 100644 (file)
@@ -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()
index 8a1829c333b4210541cc292884c031b783e737b1..d53f8b65c6069855f8fde6c03fd734df234fec33 100644 (file)
@@ -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