-
Notifications
You must be signed in to change notification settings - Fork 0
MPT-17614 validate, clean base url #209
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
Merged
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
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,47 @@ | ||
| import re | ||
| from urllib.parse import SplitResult, urlsplit, urlunparse | ||
|
|
||
| INVALID_ENV_URL_MESSAGE = ( | ||
| "Base URL is required. " | ||
| "Set it up as env variable MPT_URL or pass it as `base_url` " | ||
| "argument to MPTClient. Expected format scheme://host[:port]" | ||
| ) | ||
| PATHS_TO_REMOVE_RE = re.compile(r"^/$|^/public/?$|^/public/v1/?$") | ||
|
|
||
|
|
||
| def _format_host(hostname: str | None) -> str: | ||
| if not hostname or not isinstance(hostname, str): | ||
| raise ValueError(INVALID_ENV_URL_MESSAGE) | ||
|
|
||
| return f"[{hostname}]" if ":" in hostname else hostname | ||
|
|
||
|
|
||
| def _format_port(split_result: SplitResult) -> str: | ||
| try: | ||
| parsed_port = split_result.port | ||
| except ValueError as exc: | ||
| raise ValueError(INVALID_ENV_URL_MESSAGE) from exc | ||
| return f":{parsed_port}" if parsed_port else "" | ||
|
|
||
|
|
||
| def _sanitize_path(path: str) -> str: | ||
| return PATHS_TO_REMOVE_RE.sub("", path) | ||
|
|
||
|
|
||
| def _build_sanitized_base_url(split_result: SplitResult) -> str: | ||
| host = _format_host(split_result.hostname) | ||
| port = _format_port(split_result) | ||
| path = _sanitize_path(split_result.path) | ||
| return str(urlunparse((split_result.scheme, f"{host}{port}", path, "", "", ""))) | ||
|
|
||
|
|
||
| def validate_base_url(base_url: str | None) -> str: | ||
| """Validate base url.""" | ||
| if not base_url or not isinstance(base_url, str): | ||
| raise ValueError(INVALID_ENV_URL_MESSAGE) | ||
|
|
||
| split_result = urlsplit(base_url, scheme="https") | ||
| if not split_result.scheme or not split_result.hostname: | ||
| raise ValueError(INVALID_ENV_URL_MESSAGE) | ||
|
|
||
| return _build_sanitized_base_url(split_result) | ||
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,42 @@ | ||
| import pytest | ||
|
|
||
| from mpt_api_client.http.client_utils import validate_base_url | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("input_url", "expected"), | ||
| [ | ||
| ("//[2001:db8:85a3::8a2e:370:7334]:80/a", "https://[2001:db8:85a3::8a2e:370:7334]:80/a"), | ||
| ("//example.com", "https://example.com"), | ||
| ("http://example.com", "http://example.com"), | ||
| ("http://example.com:88/something/else", "http://example.com:88/something/else"), | ||
| ("http://user@example.com:88/", "http://example.com:88"), | ||
| ("http://user:pass@example.com:88/", "http://example.com:88"), | ||
| ("http://example.com/public", "http://example.com"), | ||
| ("http://example.com/public/", "http://example.com"), | ||
| ("http://example.com/public/else", "http://example.com/public/else"), | ||
| ("http://example.com/public/v1", "http://example.com"), | ||
| ("http://example.com/public/v1/", "http://example.com"), | ||
| ("http://example.com/else/public", "http://example.com/else/public"), | ||
| ("http://example.com/elsepublic", "http://example.com/elsepublic"), | ||
| ], | ||
| ) | ||
| def test_protocol_and_host(input_url, expected): | ||
| result = validate_base_url(input_url) | ||
|
|
||
| assert result == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_url", | ||
| [ | ||
| "", | ||
| "http//example.com", | ||
| "://example.com", | ||
| "http:example.com", | ||
| "http:/example.com", | ||
| ], | ||
| ) | ||
| def test_protocol_and_host_error(input_url): | ||
| with pytest.raises(ValueError): | ||
| validate_base_url(input_url) |
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.
Uh oh!
There was an error while loading. Please reload this page.