Skip to content

Commit b025e42

Browse files
committed
created util method to normalise http protocol in http path
1 parent ce55e7b commit b025e42

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
URL utility functions for the Databricks SQL connector.
3+
"""
4+
5+
6+
def normalize_host_with_protocol(host: str) -> str:
7+
"""
8+
Normalize a connection hostname by ensuring it has a protocol and removing trailing slashes.
9+
10+
This is useful for handling cases where users may provide hostnames with or without protocols
11+
(common with dbt-databricks users copying URLs from their browser).
12+
13+
Args:
14+
host: Connection hostname which may or may not include a protocol prefix (https:// or http://)
15+
and may or may not have a trailing slash
16+
17+
Returns:
18+
Normalized hostname with protocol prefix and no trailing slash
19+
20+
Examples:
21+
normalize_host_with_protocol("myserver.com") -> "https://myserver.com"
22+
normalize_host_with_protocol("https://myserver.com") -> "https://myserver.com"
23+
"""
24+
# Remove trailing slash
25+
host = host.rstrip("/")
26+
27+
# Add protocol if not present
28+
if not host.startswith("https://") and not host.startswith("http://"):
29+
host = f"https://{host}"
30+
31+
return host
32+

tests/unit/test_url_utils.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Tests for URL utility functions."""
2+
import pytest
3+
from databricks.sql.common.url_utils import normalize_host_with_protocol
4+
5+
6+
class TestNormalizeHostWithProtocol:
7+
"""Tests for normalize_host_with_protocol function."""
8+
9+
@pytest.mark.parametrize("input_host,expected_output", [
10+
# Hostname without protocol - should add https://
11+
("myserver.com", "https://myserver.com"),
12+
("workspace.databricks.com", "https://workspace.databricks.com"),
13+
14+
# Hostname with https:// - should not duplicate
15+
("https://myserver.com", "https://myserver.com"),
16+
("https://workspace.databricks.com", "https://workspace.databricks.com"),
17+
18+
# Hostname with http:// - should preserve
19+
("http://localhost", "http://localhost"),
20+
("http://myserver.com:8080", "http://myserver.com:8080"),
21+
22+
# Hostname with port numbers
23+
("myserver.com:443", "https://myserver.com:443"),
24+
("https://myserver.com:443", "https://myserver.com:443"),
25+
("http://localhost:8080", "http://localhost:8080"),
26+
27+
# Trailing slash - should be removed
28+
("myserver.com/", "https://myserver.com"),
29+
("https://myserver.com/", "https://myserver.com"),
30+
("http://localhost/", "http://localhost"),
31+
32+
# Real Databricks patterns
33+
("adb-123456.azuredatabricks.net", "https://adb-123456.azuredatabricks.net"),
34+
("https://adb-123456.azuredatabricks.net", "https://adb-123456.azuredatabricks.net"),
35+
("https://adb-123456.azuredatabricks.net/", "https://adb-123456.azuredatabricks.net"),
36+
("workspace.databricks.com/", "https://workspace.databricks.com"),
37+
])
38+
def test_normalize_host_with_protocol(self, input_host, expected_output):
39+
"""Test host normalization with various input formats."""
40+
assert normalize_host_with_protocol(input_host) == expected_output
41+

0 commit comments

Comments
 (0)