Skip to content
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

webknossos_context: Guard against trailing slashes in URL #733

Merged
merged 4 commits into from May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion webknossos/Changelog.md
Expand Up @@ -20,7 +20,7 @@ For upgrade instructions, please check the respective *Breaking Changes* section
- added Python 3.9 support to wk-libs [#716](https://github.com/scalableminds/webknossos-libs/pull/716)

### Fixed

- 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)

## [0.10.1](https://github.com/scalableminds/webknossos-libs/releases/tag/v0.10.1) - 2022-05-10
[Commits](https://github.com/scalableminds/webknossos-libs/compare/v0.10.0...v0.10.1)
Expand Down
11 changes: 10 additions & 1 deletion webknossos/tests/client/test_context.py
@@ -1,6 +1,10 @@
import pytest

from webknossos.client.context import _get_context, _WebknossosContext
from webknossos.client.context import (
_get_context,
_WebknossosContext,
webknossos_context,
)

pytestmark = [pytest.mark.with_vcr]

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

def test_user_organization(env_context: _WebknossosContext) -> None:
assert env_context.organization_id == "Organization_X"


def test_trailing_slash_in_url(env_context: _WebknossosContext) -> None:
with webknossos_context(url=env_context.url + "/"):
assert env_context.url == _get_context().url
4 changes: 2 additions & 2 deletions webknossos/webknossos/client/context.py
Expand Up @@ -123,7 +123,7 @@ def _clear_all_context_caches() -> None:

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

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