diff --git a/src/testcontainers/core/wait_strategies.py b/src/testcontainers/core/wait_strategies.py index 4e9f5a77..17ea81e1 100644 --- a/src/testcontainers/core/wait_strategies.py +++ b/src/testcontainers/core/wait_strategies.py @@ -206,11 +206,13 @@ def from_url(cls, url: str) -> Self: Example: strategy = HttpWaitStrategy.from_url("https://localhost:8080/api/health") """ - from urllib.parse import urlparse + from urllib.parse import urlsplit - parsed = urlparse(url) + parsed = urlsplit(url) port = parsed.port or (443 if parsed.scheme == "https" else 80) path = parsed.path or "/" + if parsed.query: + path += f"?{parsed.query}" strategy = cls(port, path) diff --git a/tests/core/test_wait_strategies.py b/tests/core/test_wait_strategies.py index 60c1be57..f2d7bb2a 100644 --- a/tests/core/test_wait_strategies.py +++ b/tests/core/test_wait_strategies.py @@ -364,6 +364,20 @@ def test_from_url(self, url, expected_port, expected_path, expected_tls): assert strategy._path == expected_path assert strategy._tls is expected_tls + @pytest.mark.parametrize( + "url,expected_path", + [ + ("http://localhost:8080/health?ready=true", "/health?ready=true"), + ("http://localhost:8080?ready=true", "/?ready=true"), + ("https://localhost/health?a=1&a=2&empty=", "/health?a=1&a=2&empty="), + ("http://localhost/health?value=a%23b%26c#ignored", "/health?value=a%23b%26c"), + ("http://localhost/health;version=2?ready=true", "/health;version=2?ready=true"), + ], + ) + def test_from_url_preserves_request_target(self, url, expected_path): + strategy = HttpWaitStrategy.from_url(url) + assert strategy._path == expected_path + @pytest.mark.parametrize( "status_codes,expected_result", [