Skip to content

Commit

Permalink
Don't include port if port is default port of the protocol (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
silamon authored Dec 9, 2024
1 parent 5ce4275 commit b233075
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/linkplay/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions tests/linkplay/test_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from unittest.mock import AsyncMock, patch

import pytest

from linkplay.bridge import (
LinkPlayBridge,
LinkPlayDevice,
Expand All @@ -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"
Expand Down
21 changes: 20 additions & 1 deletion tests/linkplay/test_endpoint.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand Down

0 comments on commit b233075

Please sign in to comment.