-
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
5
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.
Open
Changes from all commits
Commits
Show all changes
5 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 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,31 @@ | ||
| """ | ||
| 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://) | ||
| 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" | ||
| """ | ||
| # Remove trailing slash | ||
| host = host.rstrip("/") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit : check for NPE |
||
|
|
||
| # Add protocol if not present | ||
| if not host.startswith("https://") and not host.startswith("http://"): | ||
| host = f"https://{host}" | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| """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"), | ||
|
|
||
| ]) | ||
| def test_normalize_host_with_protocol(self, input_host, expected_output): | ||
| """Test host normalization with various input formats.""" | ||
| assert normalize_host_with_protocol(input_host) == expected_output | ||
|
|
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.
nit : case insensitivity should be considered