From 5a83d90ceec4c7264f1b11a103168bdf3d9f30a6 Mon Sep 17 00:00:00 2001 From: Maxim Zhiltsov Date: Fri, 7 Oct 2022 14:18:42 +0600 Subject: [PATCH 1/2] Strip trailing slashes in SDK apis --- cvat-sdk/cvat_sdk/core/client.py | 6 ++++-- tests/python/sdk/test_client.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cvat-sdk/cvat_sdk/core/client.py b/cvat-sdk/cvat_sdk/core/client.py index f4e5d84eca0..4f772d7b9b3 100644 --- a/cvat-sdk/cvat_sdk/core/client.py +++ b/cvat-sdk/cvat_sdk/core/client.py @@ -98,6 +98,8 @@ def _validate_and_prepare_url(cls, url: str) -> str: schema = "" base_url = url + base_url = base_url.rstrip("/") + if schema and schema not in cls.ALLOWED_SCHEMAS: raise InvalidHostException( f"Invalid url schema '{schema}', expected " @@ -279,7 +281,7 @@ class CVAT_API_V2: """Build parameterized API URLs""" def __init__(self, host: str): - self.host = host + self.host = host.rstrip("/") self.base = self.host + "/api/" self.git = self.host + "/git/repository/" @@ -308,7 +310,7 @@ def make_endpoint_url( def make_client( host: str, *, port: Optional[int] = None, credentials: Optional[Tuple[int, int]] = None ) -> Client: - url = host + url = host.rstrip("/") if port: url = f"{url}:{port}" diff --git a/tests/python/sdk/test_client.py b/tests/python/sdk/test_client.py index 7fdf70eee65..6c6af23a863 100644 --- a/tests/python/sdk/test_client.py +++ b/tests/python/sdk/test_client.py @@ -58,6 +58,19 @@ def test_can_get_server_version(self): assert (version.major, version.minor) >= (2, 0) +def test_can_strip_trailing_slash_in_hostname_in_make_client(admin_user: str): + host, port = BASE_URL.split("://", maxsplit=1)[1].rsplit(":", maxsplit=1) + + with make_client(host=host + "/", port=port, credentials=(admin_user, USER_PASS)) as client: + assert client.api_map.host == BASE_URL + + +def test_can_strip_trailing_slash_in_hostname_in_client_ctor(admin_user: str): + with Client(url=BASE_URL + "/") as client: + client.login((admin_user, USER_PASS)) + assert client.api_map.host == BASE_URL + + def test_can_detect_server_schema_if_not_provided(): host, port = BASE_URL.split("://", maxsplit=1)[1].rsplit(":", maxsplit=1) client = make_client(host=host, port=int(port)) From 987f65b3d3c5125a1b491ed799e66679eae8d501 Mon Sep 17 00:00:00 2001 From: Maxim Zhiltsov Date: Fri, 7 Oct 2022 14:38:54 +0600 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9151e7a079..4ea69b26085 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ non-ascii paths while adding files from "Connected file share" (issue #4428) - IOG and f-BRS serverless function () - Invisible label item in label constructor when label color background is white, or close to it () +- A trailing slash in hostname does't allow SDK to send some requests + () ### Security - TDB