Skip to content
Open
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
2 changes: 1 addition & 1 deletion api/libs/url_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def github_blob_to_raw_url(url: str) -> str:
parts = parsed.path.split("/", 4)
if (
parsed.scheme != "https"
or parsed.netloc != "github.com"
or parsed.netloc.lower() != "github.com"
or len(parts) != 5
or not parts[1]
or not parts[2]
Expand Down
34 changes: 34 additions & 0 deletions api/tests/unit_tests/libs/test_github_blob_url_host_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from libs.url_utils import github_blob_to_raw_url


def test_github_blob_to_raw_url_normalizes_capitalized_host() -> None:
# URL host names are case-insensitive (RFC 3986); a capitalized GitHub host
# must resolve to the same raw file URL as the lowercase spelling.
expected = "https://raw.githubusercontent.com/acme/app/main/dsl.yml"
assert github_blob_to_raw_url("https://GitHub.com/acme/app/blob/main/dsl.yml") == expected
assert github_blob_to_raw_url("https://GITHUB.COM/acme/app/blob/main/dsl.yml") == expected
assert github_blob_to_raw_url("https://github.COM/acme/app/blob/main/dsl.yml") == expected


def test_github_blob_to_raw_url_lowercase_host_is_unchanged() -> None:
assert github_blob_to_raw_url("https://github.com/acme/app/blob/main/dsl.yml") == (
"https://raw.githubusercontent.com/acme/app/main/dsl.yml"
)


def test_github_blob_to_raw_url_preserves_query_and_fragment() -> None:
# Regression: the conversion keeps the query string and fragment intact.
assert github_blob_to_raw_url("https://github.com/acme/app/blob/main/dsl.yml?ref=main#L12") == (
"https://raw.githubusercontent.com/acme/app/main/dsl.yml?ref=main#L12"
)


def test_github_blob_to_raw_url_leaves_other_urls_untouched() -> None:
non_github = "https://gitlab.com/acme/app/blob/main/dsl.yml"
assert github_blob_to_raw_url(non_github) == non_github
subdomain = "https://GIST.GITHUB.COM/acme/app/blob/main/dsl.yml"
assert github_blob_to_raw_url(subdomain) == subdomain
tree_path = "https://github.com/acme/app/tree/main/dsl.yml"
assert github_blob_to_raw_url(tree_path) == tree_path
http_scheme = "http://github.com/acme/app/blob/main/dsl.yml"
assert github_blob_to_raw_url(http_scheme) == http_scheme