diff --git a/tests/resources/jupyter_server_config.py b/tests/resources/jupyter_server_config.py index ac1e0dfe..1b715a37 100644 --- a/tests/resources/jupyter_server_config.py +++ b/tests/resources/jupyter_server_config.py @@ -13,11 +13,29 @@ def _get_path(*args): c = get_config() # noqa +def get_command(port): + return [sys.executable, _get_path("httpinfo.py"), f"--port={port}"] + + +def get_command_unix_socket(unix_socket): + return [sys.executable, _get_path("httpinfo.py"), f"--unix-socket={unix_socket}"] + + +def get_environment(base_url): + return {"JUPYTERLAB_BASE_URL": base_url, "MYVAR": "String with escaped {{var}}"} + + def mappathf(path): p = path + "mapped" return p +def request_headers_overwrite(): + return { + "X-Custom-Header": "pytest-23456", + } + + def translate_ciao(path, host, response, orig_response, port): # Assume that the body has not been modified by any previous rewrite assert response.body == orig_response.body @@ -47,10 +65,6 @@ def cats_only(response, path): response.body = b"dogs not allowed" -def my_env(): - return {"MYVAR": "String with escaped {{var}}"} - - c.ServerProxy.servers = { "python-http": { "command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"], @@ -63,19 +77,39 @@ def my_env(): "command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"], "port": 54321, }, + "python-http-callable-command": { + "command": get_command, + }, "python-http-mappath": { "command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"], "mappath": { "/": "/index.html", }, }, - "python-http-mappathf": { + "python-http-callable-mappath": { "command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"], "mappath": mappathf, }, - "python-http-callable-env": { + "python-http-environment": { + "command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"], + "environment": { + "JUPYTERLAB_BASE_URL": "{base_url}", + "MYVAR": "String with escaped {{var}}", + }, + }, + "python-http-callable-environment": { "command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"], - "environment": my_env, + "environment": get_environment, + }, + "python-http-request-headers": { + "command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"], + "request_headers_override": { + "X-Custom-Header": "pytest-23456", + }, + }, + "python-http-callable-request-headers": { + "command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"], + "request_headers_override": request_headers_overwrite, }, "python-websocket": { "command": [sys.executable, _get_path("websocket.py"), "--port={port}"], @@ -94,6 +128,10 @@ def my_env(): ], "unix_socket": True, }, + "python-unix-socket-callable": { + "command": get_command_unix_socket, + "unix_socket": True, + }, "python-unix-socket-file": { "command": [ sys.executable, @@ -107,12 +145,6 @@ def my_env(): # python-unix-socket-file "unix_socket": "/tmp/jupyter-server-proxy-test-socket", }, - "python-request-headers": { - "command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"], - "request_headers_override": { - "X-Custom-Header": "pytest-23456", - }, - }, "python-gzipserver": { "command": [sys.executable, _get_path("gzipserver.py"), "{port}"], }, diff --git a/tests/test_proxies.py b/tests/test_proxies.py index 4573517c..1016481f 100644 --- a/tests/test_proxies.py +++ b/tests/test_proxies.py @@ -33,6 +33,12 @@ def request_get(port, path, token, host=LOCALHOST): sys.platform == "win32", reason="Unix socket not supported on Windows" ), ), + pytest.param( + "/python-unix-socket-callable/", + marks=pytest.mark.skipif( + sys.platform == "win32", reason="Unix socket not supported on Windows" + ), + ), pytest.param( "/python-unix-socket-file/", marks=pytest.mark.skipif( @@ -279,6 +285,18 @@ def test_server_proxy_port_non_service_rewrite_response( assert s.startswith("GET /foo?token=") +def test_server_proxy_command_callable( + a_server_port_and_token: Tuple[int, str] +) -> None: + PORT, TOKEN = a_server_port_and_token + r = request_get(PORT, "/python-http-callable-command/abc", TOKEN) + assert r.code == 200 + s = r.read().decode("ascii") + assert s.startswith("GET /abc?token=") + assert "X-Forwarded-Context: /python-http-callable-command\n" in s + assert "X-Proxycontextpath: /python-http-callable-command\n" in s + + @pytest.mark.parametrize( "requestpath,expected", [ @@ -311,28 +329,48 @@ def test_server_proxy_mappath_callable( requestpath, expected, a_server_port_and_token: Tuple[int, str] ) -> None: PORT, TOKEN = a_server_port_and_token - r = request_get(PORT, "/python-http-mappathf" + requestpath, TOKEN) + r = request_get(PORT, "/python-http-callable-mappath" + requestpath, TOKEN) assert r.code == 200 s = r.read().decode("ascii") assert s.startswith("GET " + expected) - assert "X-Forwarded-Context: /python-http-mappathf\n" in s - assert "X-Proxycontextpath: /python-http-mappathf\n" in s + assert "X-Forwarded-Context: /python-http-callable-mappath\n" in s + assert "X-Proxycontextpath: /python-http-callable-mappath\n" in s -def test_server_proxy_remote(a_server_port_and_token: Tuple[int, str]) -> None: +@pytest.mark.parametrize( + "name", ["python-http-environment", "python-http-callable-environment"] +) +def test_server_proxy_environment( + name: str, a_server_port_and_token: Tuple[int, str] +) -> None: PORT, TOKEN = a_server_port_and_token - r = request_get(PORT, "/newproxy", TOKEN, host="127.0.0.1") + r = request_get(PORT, f"/{name}/test", TOKEN) assert r.code == 200 + s = r.read().decode("ascii") + assert s.startswith("GET /test?token=") + assert f"X-Forwarded-Context: /{name}\n" in s + assert f"X-Proxycontextpath: /{name}\n" in s -def test_server_request_headers(a_server_port_and_token: Tuple[int, str]) -> None: +@pytest.mark.parametrize( + "name", ["python-http-request-headers", "python-http-callable-request-headers"] +) +def test_server_proxy_request_headers( + name, a_server_port_and_token: Tuple[int, str] +) -> None: PORT, TOKEN = a_server_port_and_token - r = request_get(PORT, "/python-request-headers/", TOKEN, host="127.0.0.1") + r = request_get(PORT, f"/{name}/", TOKEN, host="127.0.0.1") assert r.code == 200 s = r.read().decode("ascii") assert "X-Custom-Header: pytest-23456\n" in s +def test_server_proxy_remote(a_server_port_and_token: Tuple[int, str]) -> None: + PORT, TOKEN = a_server_port_and_token + r = request_get(PORT, "/newproxy", TOKEN, host="127.0.0.1") + assert r.code == 200 + + def test_server_content_encoding_header( a_server_port_and_token: Tuple[int, str] ) -> None: @@ -498,14 +536,6 @@ def test_bad_server_proxy_url( 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 - - @pytest.mark.parametrize( "rawsocket_type", [