From b23307555bc2120c8a975edab63cb5a1ce375ab6 Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Mon, 9 Dec 2024 19:55:49 +0100 Subject: [PATCH] Don't include port if port is default port of the protocol (#70) --- src/linkplay/endpoint.py | 7 ++++++- tests/linkplay/test_bridge.py | 3 +-- tests/linkplay/test_endpoint.py | 21 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/linkplay/endpoint.py b/src/linkplay/endpoint.py index 1c06d49..8deb2c2 100644 --- a/src/linkplay/endpoint.py +++ b/src/linkplay/endpoint.py @@ -37,7 +37,12 @@ def __init__( "http", "https", ], "Protocol must be either 'http' or 'https'" - self._endpoint: str = f"{protocol}://{endpoint}:{port}" + include_port = (protocol == "http" and port != 80) or ( + protocol == "https" and port != 443 + ) + port_suffix = f":{port}" if include_port else "" + self._endpoint: str = f"{protocol}://{endpoint}{port_suffix}" + self._session: ClientSession = session def to_dict(self): diff --git a/tests/linkplay/test_bridge.py b/tests/linkplay/test_bridge.py index df78fbd..c33ab5b 100644 --- a/tests/linkplay/test_bridge.py +++ b/tests/linkplay/test_bridge.py @@ -4,7 +4,6 @@ from unittest.mock import AsyncMock, patch import pytest - from linkplay.bridge import ( LinkPlayBridge, LinkPlayDevice, @@ -31,7 +30,7 @@ def test_device_name(): protocol="http", port=80, endpoint="1.2.3.4", session=None ) bridge: LinkPlayBridge = LinkPlayBridge(endpoint=endpoint) - assert f"{bridge}" == "http://1.2.3.4:80" + assert f"{bridge}" == "http://1.2.3.4" bridge.device.properties[DeviceAttribute.DEVICE_NAME] = "TestDevice" assert f"{bridge}" == "TestDevice" diff --git a/tests/linkplay/test_endpoint.py b/tests/linkplay/test_endpoint.py index 02ec823..c1dde3b 100644 --- a/tests/linkplay/test_endpoint.py +++ b/tests/linkplay/test_endpoint.py @@ -1,10 +1,29 @@ """Test endpoint functionality.""" import pytest - from linkplay.endpoint import LinkPlayApiEndpoint +@pytest.mark.parametrize( + "protocol, endpoint, port, expected", + [ + ("http", "1.2.3.4", 80, "http://1.2.3.4"), + ("http", "1.2.3.4", 8080, "http://1.2.3.4:8080"), + ("https", "1.2.3.4", 443, "https://1.2.3.4"), + ("https", "1.2.3.4", 8443, "https://1.2.3.4:8443"), + ], +) +def test_api_endpoint_protocol_port_combination( + protocol, endpoint, port, expected +) -> None: + """Tests the endpoint creation""" + + endpoint: LinkPlayApiEndpoint = LinkPlayApiEndpoint( + protocol=protocol, port=port, endpoint=endpoint, session=None + ) + assert f"{endpoint}" == expected + + def test_api_endpoint_protocol_raises_assertion_error() -> None: """Tests whether or not instantiating the LinkPlayApiEndpoint with an invalid protocol raises an AssertionError."""