From 6f61f854f6c92e0cce148c6e7655854d6562f1f4 Mon Sep 17 00:00:00 2001 From: Diogo Castro Date: Thu, 7 Dec 2023 12:28:18 +0100 Subject: [PATCH 1/2] Test callable environments formatting If the environment has an escaped variable (so, a string), the server should start without failing to format it. --- tests/resources/jupyter_server_config.py | 8 ++++++++ tests/test_proxies.py | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/tests/resources/jupyter_server_config.py b/tests/resources/jupyter_server_config.py index 2fd2dd99..97136d2f 100644 --- a/tests/resources/jupyter_server_config.py +++ b/tests/resources/jupyter_server_config.py @@ -42,6 +42,10 @@ def cats_only(response, path): response.code = 403 response.body = b"dogs not allowed" +def my_env(): + return { + "MYVAR": "String with escaped {{var}}" + } c.ServerProxy.servers = { "python-http": { @@ -65,6 +69,10 @@ def cats_only(response, path): "command": [sys.executable, "./tests/resources/httpinfo.py", "--port={port}"], "mappath": mappathf, }, + "python-http-callable-env": { + "command": [sys.executable, "./tests/resources/httpinfo.py", "--port={port}"], + "environment": my_env, + }, "python-websocket": { "command": [sys.executable, "./tests/resources/websocket.py", "--port={port}"], "request_headers_override": { diff --git a/tests/test_proxies.py b/tests/test_proxies.py index 8ffabf49..5605b4d1 100644 --- a/tests/test_proxies.py +++ b/tests/test_proxies.py @@ -408,3 +408,9 @@ def test_bad_server_proxy_url( if status >= 400: # request should not have been proxied assert "X-ProxyContextPath" not in r.headers + + +def test_callable_environment_formatting(a_server_port_and_token: Tuple[int, str]) -> None: + PORT, TOKEN = a_server_port_and_token + r = request_get(PORT, "/python-http-callable-env/test", TOKEN) + assert r.code == 200 From 40a37b3698ab1dfa6b5688312721262008196bb5 Mon Sep 17 00:00:00 2001 From: Diogo Castro Date: Thu, 7 Dec 2023 12:30:49 +0100 Subject: [PATCH 2/2] Only format a proxy environment once It was doing double formatting, which broke when passing environment variables with escaped strings (i.e {{var}}). --- jupyter_server_proxy/handlers.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/jupyter_server_proxy/handlers.py b/jupyter_server_proxy/handlers.py index db57222b..2890dc3a 100644 --- a/jupyter_server_proxy/handlers.py +++ b/jupyter_server_proxy/handlers.py @@ -654,9 +654,7 @@ def _render_template(self, value): def _realize_rendered_template(self, attribute): """Call any callables, then render any templated values.""" if callable(attribute): - attribute = self._render_template( - call_with_asked_args(attribute, self.process_args) - ) + attribute = call_with_asked_args(attribute, self.process_args) return self._render_template(attribute) @web.authenticated