-
Notifications
You must be signed in to change notification settings - Fork 130
created util method to normalise http protocol in http path #724
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
Open
nikhilsuri-db
wants to merge
8
commits into
main
Choose a base branch
from
PECOBLR-1446
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−21
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
14915d2
created util method to normalise http protocol in http path
nikhilsuri-db 81ec814
Added impacted files using util method
nikhilsuri-db ef4f92c
Fixed linting issues
nikhilsuri-db e982615
fixed broken test with mock host string
nikhilsuri-db 5ae79ce
mocked http client
nikhilsuri-db cca26eb
made case sensitive check in url utils
nikhilsuri-db a5c698c
linting issue resolved
nikhilsuri-db 5f9954f
removed unnecessary md files
nikhilsuri-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| """ | ||
| URL utility functions for the Databricks SQL connector. | ||
| """ | ||
|
|
||
|
|
||
| def normalize_host_with_protocol(host: str) -> str: | ||
| """ | ||
| Normalize a connection hostname by ensuring it has a protocol and removing trailing slashes. | ||
|
|
||
| This is useful for handling cases where users may provide hostnames with or without protocols | ||
| (common with dbt-databricks users copying URLs from their browser). | ||
|
|
||
| Args: | ||
| host: Connection hostname which may or may not include a protocol prefix (https:// or http://) | ||
nikhilsuri-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| and may or may not have a trailing slash | ||
|
|
||
| Returns: | ||
| Normalized hostname with protocol prefix and no trailing slash | ||
|
|
||
| Examples: | ||
| normalize_host_with_protocol("myserver.com") -> "https://myserver.com" | ||
| normalize_host_with_protocol("https://myserver.com") -> "https://myserver.com" | ||
| normalize_host_with_protocol("HTTPS://myserver.com") -> "https://myserver.com" | ||
|
|
||
| Raises: | ||
| ValueError: If host is None or empty string | ||
| """ | ||
| # Handle None or empty host | ||
| if not host or not host.strip(): | ||
| raise ValueError("Host cannot be None or empty") | ||
|
|
||
| # Remove trailing slash | ||
| host = host.rstrip("/") | ||
nikhilsuri-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Add protocol if not present (case-insensitive check) | ||
| host_lower = host.lower() | ||
| if not host_lower.startswith("https://") and not host_lower.startswith("http://"): | ||
| host = f"https://{host}" | ||
| elif host_lower.startswith("https://") or host_lower.startswith("http://"): | ||
| # Normalize protocol to lowercase | ||
| protocol_end = host.index("://") + 3 | ||
| host = host[:protocol_end].lower() + host[protocol_end:] | ||
|
|
||
| return host | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """Tests for URL utility functions.""" | ||
| import pytest | ||
| from databricks.sql.common.url_utils import normalize_host_with_protocol | ||
|
|
||
|
|
||
| class TestNormalizeHostWithProtocol: | ||
| """Tests for normalize_host_with_protocol function.""" | ||
|
|
||
| @pytest.mark.parametrize("input_host,expected_output", [ | ||
| # Hostname without protocol - should add https:// | ||
| ("myserver.com", "https://myserver.com"), | ||
| ("workspace.databricks.com", "https://workspace.databricks.com"), | ||
|
|
||
| # Hostname with https:// - should not duplicate | ||
| ("https://myserver.com", "https://myserver.com"), | ||
| ("https://workspace.databricks.com", "https://workspace.databricks.com"), | ||
|
|
||
| # Hostname with http:// - should preserve | ||
| ("http://localhost", "http://localhost"), | ||
| ("http://myserver.com:8080", "http://myserver.com:8080"), | ||
|
|
||
| # Hostname with port numbers | ||
| ("myserver.com:443", "https://myserver.com:443"), | ||
| ("https://myserver.com:443", "https://myserver.com:443"), | ||
| ("http://localhost:8080", "http://localhost:8080"), | ||
|
|
||
| # Trailing slash - should be removed | ||
| ("myserver.com/", "https://myserver.com"), | ||
| ("https://myserver.com/", "https://myserver.com"), | ||
| ("http://localhost/", "http://localhost"), | ||
|
|
||
| # Case-insensitive protocol handling - should normalize to lowercase | ||
| ("HTTPS://myserver.com", "https://myserver.com"), | ||
| ("HTTP://myserver.com", "http://myserver.com"), | ||
| ("HttPs://workspace.databricks.com", "https://workspace.databricks.com"), | ||
| ("HtTp://localhost:8080", "http://localhost:8080"), | ||
| ("HTTPS://MYSERVER.COM", "https://MYSERVER.COM"), # Only protocol lowercased | ||
|
|
||
| # Case-insensitive with trailing slashes | ||
| ("HTTPS://myserver.com/", "https://myserver.com"), | ||
| ("HTTP://localhost:8080/", "http://localhost:8080"), | ||
| ("HttPs://workspace.databricks.com//", "https://workspace.databricks.com"), | ||
|
|
||
| # Mixed case protocols with ports | ||
| ("HTTPS://myserver.com:443", "https://myserver.com:443"), | ||
| ("HtTp://myserver.com:8080", "http://myserver.com:8080"), | ||
|
|
||
| # Case preservation - only protocol lowercased, hostname case preserved | ||
| ("HTTPS://MyServer.DataBricks.COM", "https://MyServer.DataBricks.COM"), | ||
| ("HttPs://CamelCase.Server.com", "https://CamelCase.Server.com"), | ||
| ("HTTP://UPPERCASE.COM:8080", "http://UPPERCASE.COM:8080"), | ||
| ]) | ||
| def test_normalize_host_with_protocol(self, input_host, expected_output): | ||
| """Test host normalization with various input formats.""" | ||
| result = normalize_host_with_protocol(input_host) | ||
| assert result == expected_output | ||
|
|
||
| # Additional assertion: verify protocol is always lowercase | ||
| assert result.startswith("https://") or result.startswith("http://") | ||
|
|
||
| @pytest.mark.parametrize("invalid_host", [ | ||
| None, | ||
| "", | ||
| " ", # Whitespace only | ||
| ]) | ||
| def test_normalize_host_with_protocol_raises_on_invalid_input(self, invalid_host): | ||
| """Test that function raises ValueError for None or empty host.""" | ||
| with pytest.raises(ValueError, match="Host cannot be None or empty"): | ||
| normalize_host_with_protocol(invalid_host) | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was rechecking this piece of code : auth_utils.py and thrift_backend - Has proper check on this already. should we use this util instead?
Also, the sea flow looks incorrect at the moment : backend/sea/utils/http_client.py