Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check trailing slash in host url #681

Merged
merged 8 commits into from
Jul 3, 2024
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
19 changes: 13 additions & 6 deletions databricks/sdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,20 @@ def attributes(cls) -> Iterable[ConfigAttribute]:
def _fix_host_if_needed(self):
if not self.host:
return
# fix url to remove trailing slash

# Add a default scheme if it's missing
if '://' not in self.host:
self.host = 'https://' + self.host

o = urllib.parse.urlparse(self.host)
if not o.hostname:
# only hostname is specified
self.host = f"https://{self.host}"
else:
self.host = f"{o.scheme}://{o.netloc}"
# remove trailing slash
path = o.path.rstrip('/')
# remove port if 443
netloc = o.netloc
if o.port == 443:
netloc = netloc.split(':')[0]

self.host = urllib.parse.urlunparse((o.scheme, netloc, path, o.params, o.query, o.fragment))

def _set_inner_config(self, keyword_args: Dict[str, any]):
for attr in self.attributes():
Expand Down
13 changes: 13 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import platform

import pytest

from databricks.sdk.config import Config, with_product, with_user_agent_extra
from databricks.sdk.version import __version__

Expand All @@ -20,6 +22,17 @@ def test_config_supports_legacy_credentials_provider():
assert c2._product_version == '1.2.3'


@pytest.mark.parametrize('host,expected', [("https://abc.def.ghi", "https://abc.def.ghi"),
("https://abc.def.ghi/", "https://abc.def.ghi"),
("abc.def.ghi", "https://abc.def.ghi"),
("abc.def.ghi/", "https://abc.def.ghi"),
("https://abc.def.ghi:443", "https://abc.def.ghi"),
("abc.def.ghi:443", "https://abc.def.ghi")])
def test_config_host_url_format_check(mocker, host, expected):
mocker.patch('databricks.sdk.config.Config.init_auth')
assert Config(host=host).host == expected


def test_extra_and_upstream_user_agent(monkeypatch):

class MockUname:
Expand Down
Loading