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
):
"""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()
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
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()
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()
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()
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()
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