From b9748bbe216430fdf547dc28ded64c869ae828a5 Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:47:47 +0000 Subject: [PATCH] Don't include port if port is default of the protocol --- src/linkplay/endpoint.py | 6 +++++- tests/linkplay/test_bridge.py | 3 +-- tests/linkplay/test_endpoint.py | 21 ++++++++++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/linkplay/endpoint.py b/src/linkplay/endpoint.py index 1c06d49..e5d36bb 100644 --- a/src/linkplay/endpoint.py +++ b/src/linkplay/endpoint.py @@ -37,7 +37,11 @@ def __init__( "http", "https", ], "Protocol must be either 'http' or 'https'" - self._endpoint: str = f"{protocol}://{endpoint}:{port}" + if (protocol == "http" and port != 80) or (protocol == "https" and port != 443): + self._endpoint: str = f"{protocol}://{endpoint}:{port}" + else: + self._endpoint: str = f"{protocol}://{endpoint}" + 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."""