From 7fb87b74a2221c335a80fb175f2e1ad87d999d8a Mon Sep 17 00:00:00 2001 From: Baitinger Jens Date: Mon, 9 Feb 2026 15:56:05 +0100 Subject: [PATCH] feat(keycloak): support for relative path and management relative path When these parameters were set, the functions get_url() and get_management_url() did return the wrong URLs (without the prefix) Now the implementation checks if the parameter is checked and adds it to the returned URL --- .../keycloak/testcontainers/keycloak/__init__.py | 14 ++++++++++++-- modules/keycloak/tests/test_keycloak.py | 9 ++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/modules/keycloak/testcontainers/keycloak/__init__.py b/modules/keycloak/testcontainers/keycloak/__init__.py index 21ffc4231..044ba0b2b 100644 --- a/modules/keycloak/testcontainers/keycloak/__init__.py +++ b/modules/keycloak/testcontainers/keycloak/__init__.py @@ -78,12 +78,22 @@ def _configure(self) -> None: def get_url(self) -> str: host = self.get_container_host_ip() port = self.get_exposed_port(self.port) - return f"http://{host}:{port}" + + if "KC_HTTP_RELATIVE_PATH" in self.env: + path = self.env.get("KC_HTTP_RELATIVE_PATH", "").strip("/") + return f"http://{host}:{port}/{path}/" + else: + return f"http://{host}:{port}" def get_management_url(self) -> str: host = self.get_container_host_ip() port = self.get_exposed_port(self.management_port) - return f"http://{host}:{port}" + + if "KC_HTTP_MANAGEMENT_RELATIVE_PATH" in self.env: + path = self.env.get("KC_HTTP_MANAGEMENT_RELATIVE_PATH", "").strip("/") + return f"http://{host}:{port}/{path}/" + else: + return f"http://{host}:{port}" @wait_container_is_ready(requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout) def _readiness_probe(self) -> None: diff --git a/modules/keycloak/tests/test_keycloak.py b/modules/keycloak/tests/test_keycloak.py index 24f533d11..4df0ca9c9 100644 --- a/modules/keycloak/tests/test_keycloak.py +++ b/modules/keycloak/tests/test_keycloak.py @@ -2,7 +2,14 @@ from testcontainers.keycloak import KeycloakContainer -@pytest.mark.parametrize("image_version", ["26.0.0", "25.0", "24.0.1", "18.0"]) +@pytest.mark.parametrize("image_version", ["26.4.0", "26.0.0", "25.0", "24.0.1", "18.0"]) def test_docker_run_keycloak(image_version: str): with KeycloakContainer(f"quay.io/keycloak/keycloak:{image_version}") as keycloak_admin: assert keycloak_admin.get_client().users_count() == 1 + + +def test_docker_run_keycloak_with_management_relative_path(): + with KeycloakContainer("quay.io/keycloak/keycloak:26.4.0").with_env( + "KC_HTTP_MANAGEMENT_RELATIVE_PATH", "/some/deeply/nested/path" + ) as keycloak_admin: + assert keycloak_admin.get_client().users_count() == 1