Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/swgoh_comlink/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import hashlib
import hmac
import os
import re
import time
from collections.abc import Callable
from json import dumps
from typing import Any
from urllib.parse import urlparse, urlunparse

from .exceptions import SwgohComlinkValueError
from .helpers import Constants
Expand All @@ -23,8 +23,6 @@
# Keys whose values must be masked in logs, repr, and debug output.
_SENSITIVE_KEYS = frozenset({"secret_key", "access_key"})

url_port_re = re.compile(r"^https://\S+:(\d+)$", re.VERBOSE | re.IGNORECASE)

DEFAULT_TIMEOUT: float = 120.0
GAME_DATA_TIMEOUT: float = 300.0

Expand All @@ -51,8 +49,11 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
def sanitize_url(url: str) -> str:
"""Make sure provided URL is in the expected format and return sanitized."""
url = url.strip("/")
if url.startswith("https") and not re.fullmatch(url_port_re, url):
url = f"{url}:443"
if url.startswith("https"):
parsed = urlparse(url)
if parsed.port is None:
netloc = f"{parsed.hostname}:443"
url = urlunparse(parsed._replace(netloc=netloc))
return url


Expand Down
2 changes: 1 addition & 1 deletion src/swgoh_comlink/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# coding=utf-8
__version__ = "2.0.6"
__version__ = "2.0.7"
9 changes: 9 additions & 0 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ def test_https_without_port_gets_443(self):
def test_https_with_port_unchanged(self):
assert sanitize_url("https://example.com:8443") == "https://example.com:8443"

def test_https_with_path_without_port(self):
assert sanitize_url("https://gasapi.dev/comlink") == "https://gasapi.dev:443/comlink"

def test_https_with_path_and_port_unchanged(self):
assert sanitize_url("https://gasapi.dev:8443/comlink") == "https://gasapi.dev:8443/comlink"

def test_http_with_path_unchanged(self):
assert sanitize_url("http://localhost:3000/comlink") == "http://localhost:3000/comlink"

def test_strips_multiple_trailing_slashes(self):
assert sanitize_url("http://localhost:3000///") == "http://localhost:3000"

Expand Down
Loading