Skip to content

Commit 2392c2a

Browse files
authored
webknossos_context: Guard against trailing slashes in URL (#733)
* webknossos_context: Guard against trailing slashes in URL * Update test_context.py * Update Changelog.md * formatting & minor fix
1 parent 7167af4 commit 2392c2a

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

webknossos/Changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ For upgrade instructions, please check the respective *Breaking Changes* section
2020
- added Python 3.9 support to wk-libs [#716](https://github.com/scalableminds/webknossos-libs/pull/716)
2121

2222
### Fixed
23-
23+
- URLs for the webknossos-context (e.g. in the `WK_URL` env var or via `webknossos_context(url=…)`) may now contain `/` in the end and are sanitized. Before, requests would fail if the URL contained a final `/`. [#733](https://github.com/scalableminds/webknossos-libs/pull/733)
2424

2525
## [0.10.1](https://github.com/scalableminds/webknossos-libs/releases/tag/v0.10.1) - 2022-05-10
2626
[Commits](https://github.com/scalableminds/webknossos-libs/compare/v0.10.0...v0.10.1)

webknossos/tests/client/test_context.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import pytest
22

3-
from webknossos.client.context import _get_context, _WebknossosContext
3+
from webknossos.client.context import (
4+
_get_context,
5+
_WebknossosContext,
6+
webknossos_context,
7+
)
48

59
pytestmark = [pytest.mark.with_vcr]
610

@@ -15,3 +19,8 @@ def env_context() -> _WebknossosContext:
1519

1620
def test_user_organization(env_context: _WebknossosContext) -> None:
1721
assert env_context.organization_id == "Organization_X"
22+
23+
24+
def test_trailing_slash_in_url(env_context: _WebknossosContext) -> None:
25+
with webknossos_context(url=env_context.url + "/"):
26+
assert env_context.url == _get_context().url

webknossos/webknossos/client/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def _clear_all_context_caches() -> None:
123123

124124
@attr.frozen
125125
class _WebknossosContext:
126-
url: str = os.environ.get("WK_URL", default=DEFAULT_WEBKNOSSOS_URL)
126+
url: str = os.environ.get("WK_URL", default=DEFAULT_WEBKNOSSOS_URL).rstrip("/")
127127
token: Optional[str] = os.environ.get("WK_TOKEN", default=None)
128128
timeout: int = int(os.environ.get("WK_TIMEOUT", default=DEFAULT_HTTP_TIMEOUT))
129129

@@ -204,7 +204,7 @@ def my_func():
204204
`url` and `timeout` are taken from the previous context (e.g. environment variables) if not specified.
205205
`token` must be set explicitly, it is not available when not specified.
206206
"""
207-
self._url = _get_context().url if url is None else url
207+
self._url = _get_context().url if url is None else url.rstrip("/")
208208
self._token = token
209209
self._timeout = _get_context().timeout if timeout is None else timeout
210210
self._context_var_token_stack: List[Token[_WebknossosContext]] = []

0 commit comments

Comments
 (0)