Files
IronMurphy 5b752c5571 Normalize a protocol-relative URL instead of producing a hostless one (#823)
normalize_nav_url() prefixed any value without a "://" with "https://",
so a protocol-relative URL such as //baidu.com (the form pages expose when
you copy a link from source) became https:////baidu.com with an empty host.
Bare hosts keep the existing behaviour; a leading "//" now maps to
https://host/path.
2026-09-19 20:45:20 +08:00

33 lines
1.1 KiB
Python

"""tests/unit/utils/test_url.py"""
from __future__ import annotations
from octop.infra.utils.url import normalize_nav_url
def test_empty_and_blank() -> None:
assert normalize_nav_url("") == ""
assert normalize_nav_url(" ") == ""
def test_bare_host_is_prefixed() -> None:
assert normalize_nav_url("baidu.com") == "https://baidu.com"
assert normalize_nav_url("example.com/a?q=1") == "https://example.com/a?q=1"
def test_absolute_http_urls_are_preserved() -> None:
assert normalize_nav_url("http://x.com/") == "http://x.com/"
assert normalize_nav_url("https://x.com/") == "https://x.com/"
assert normalize_nav_url("HTTPS://x.com/") == "HTTPS://x.com/"
def test_protocol_relative_url_gets_https() -> None:
"""`//host` is a relative scheme URL (common when copied from a page source);
it must not become the hostless https:////host."""
assert normalize_nav_url("//baidu.com") == "https://baidu.com"
assert normalize_nav_url("//example.com/path?q=1") == "https://example.com/path?q=1"
def test_non_http_scheme_with_slashes_is_rejected() -> None:
assert normalize_nav_url("ftp://x.com") == ""