Skip to content

Improve Tests #520

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions tests/resources/jupyter_server_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"],
Expand All @@ -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}"],
Expand All @@ -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,
Expand All @@ -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}"],
},
Expand Down
60 changes: 45 additions & 15 deletions tests/test_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
[
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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",
[
Expand Down