Skip to content

Commit 2639b53

Browse files
committed
Add tests for Callable traits
1 parent b0c7feb commit 2639b53

File tree

2 files changed

+83
-29
lines changed

2 files changed

+83
-29
lines changed

tests/resources/jupyter_server_config.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,27 @@ def _get_path(*args):
1313
c = get_config() # noqa
1414

1515

16+
def get_command(port):
17+
return [sys.executable, _get_path("httpinfo.py"), f"--port={port}"]
18+
19+
def get_command_unix_socket(unix_socket):
20+
return [sys.executable, _get_path("httpinfo.py"), f"--unix-socket={unix_socket}"]
21+
22+
def get_environment(base_url):
23+
return {
24+
"JUPYTERLAB_BASE_URL": base_url,
25+
"MYVAR": "String with escaped {{var}}"
26+
}
27+
1628
def mappathf(path):
1729
p = path + "mapped"
1830
return p
1931

32+
def request_headers_overwrite():
33+
return {
34+
"X-Custom-Header": "pytest-23456",
35+
}
36+
2037

2138
def translate_ciao(path, host, response, orig_response, port):
2239
# Assume that the body has not been modified by any previous rewrite
@@ -47,10 +64,6 @@ def cats_only(response, path):
4764
response.body = b"dogs not allowed"
4865

4966

50-
def my_env():
51-
return {"MYVAR": "String with escaped {{var}}"}
52-
53-
5467
c.ServerProxy.servers = {
5568
"python-http": {
5669
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
@@ -63,19 +76,39 @@ def my_env():
6376
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
6477
"port": 54321,
6578
},
79+
"python-http-callable-command": {
80+
"command": get_command,
81+
},
6682
"python-http-mappath": {
6783
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
6884
"mappath": {
6985
"/": "/index.html",
7086
},
7187
},
72-
"python-http-mappathf": {
88+
"python-http-callable-mappath": {
7389
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
7490
"mappath": mappathf,
7591
},
76-
"python-http-callable-env": {
92+
"python-http-environment": {
93+
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
94+
"environment": {
95+
"JUPYTERLAB_BASE_URL": "{base_url}",
96+
"MYVAR": "String with escaped {{var}}"
97+
}
98+
},
99+
"python-http-callable-environment": {
77100
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
78-
"environment": my_env,
101+
"environment": get_environment,
102+
},
103+
"python-http-request-headers": {
104+
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
105+
"request_headers_override": {
106+
"X-Custom-Header": "pytest-23456",
107+
},
108+
},
109+
"python-http-callable-request-headers": {
110+
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
111+
"request_headers_override": request_headers_overwrite,
79112
},
80113
"python-websocket": {
81114
"command": [sys.executable, _get_path("websocket.py"), "--port={port}"],
@@ -94,6 +127,10 @@ def my_env():
94127
],
95128
"unix_socket": True,
96129
},
130+
"python-unix-socket-callable": {
131+
"command": get_command_unix_socket,
132+
"unix_socket": True,
133+
},
97134
"python-unix-socket-file": {
98135
"command": [
99136
sys.executable,
@@ -107,12 +144,6 @@ def my_env():
107144
# python-unix-socket-file
108145
"unix_socket": "/tmp/jupyter-server-proxy-test-socket",
109146
},
110-
"python-request-headers": {
111-
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
112-
"request_headers_override": {
113-
"X-Custom-Header": "pytest-23456",
114-
},
115-
},
116147
"python-gzipserver": {
117148
"command": [sys.executable, _get_path("gzipserver.py"), "{port}"],
118149
},

tests/test_proxies.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def request_get(port, path, token, host=LOCALHOST):
3333
sys.platform == "win32", reason="Unix socket not supported on Windows"
3434
),
3535
),
36+
pytest.param(
37+
"/python-unix-socket-callable/",
38+
marks=pytest.mark.skipif(
39+
sys.platform == "win32", reason="Unix socket not supported on Windows"
40+
),
41+
),
3642
pytest.param(
3743
"/python-unix-socket-file/",
3844
marks=pytest.mark.skipif(
@@ -278,6 +284,17 @@ def test_server_proxy_port_non_service_rewrite_response(
278284
s = r.read().decode("ascii")
279285
assert s.startswith("GET /foo?token=")
280286

287+
def test_server_proxy_command_callable(
288+
a_server_port_and_token: Tuple[int, str]
289+
) -> None:
290+
PORT, TOKEN = a_server_port_and_token
291+
r = request_get(PORT, "/python-http-callable-command/abc", TOKEN)
292+
assert r.code == 200
293+
s = r.read().decode("ascii")
294+
assert s.startswith("GET /abc?token=")
295+
assert "X-Forwarded-Context: /python-http-callable-command\n" in s
296+
assert "X-Proxycontextpath: /python-http-callable-command\n" in s
297+
281298

282299
@pytest.mark.parametrize(
283300
"requestpath,expected",
@@ -311,26 +328,40 @@ def test_server_proxy_mappath_callable(
311328
requestpath, expected, a_server_port_and_token: Tuple[int, str]
312329
) -> None:
313330
PORT, TOKEN = a_server_port_and_token
314-
r = request_get(PORT, "/python-http-mappathf" + requestpath, TOKEN)
331+
r = request_get(PORT, "/python-http-callable-mappath" + requestpath, TOKEN)
315332
assert r.code == 200
316333
s = r.read().decode("ascii")
317334
assert s.startswith("GET " + expected)
318-
assert "X-Forwarded-Context: /python-http-mappathf\n" in s
319-
assert "X-Proxycontextpath: /python-http-mappathf\n" in s
335+
assert "X-Forwarded-Context: /python-http-callable-mappath\n" in s
336+
assert "X-Proxycontextpath: /python-http-callable-mappath\n" in s
320337

321338

322-
def test_server_proxy_remote(a_server_port_and_token: Tuple[int, str]) -> None:
339+
@pytest.mark.parametrize("name", ["python-http-environment", "python-http-callable-environment"])
340+
def test_server_proxy_environment(
341+
name: str, a_server_port_and_token: Tuple[int, str]
342+
) -> None:
323343
PORT, TOKEN = a_server_port_and_token
324-
r = request_get(PORT, "/newproxy", TOKEN, host="127.0.0.1")
344+
r = request_get(PORT, f"/{name}/test", TOKEN)
325345
assert r.code == 200
346+
s = r.read().decode("ascii")
347+
assert s.startswith("GET /test?token=")
348+
assert f"X-Forwarded-Context: /{name}\n" in s
349+
assert f"X-Proxycontextpath: /{name}\n" in s
326350

327351

328-
def test_server_request_headers(a_server_port_and_token: Tuple[int, str]) -> None:
352+
@pytest.mark.parametrize("name", ["python-http-request-headers", "python-http-callable-request-headers"])
353+
def test_server_proxy_request_headers(name, a_server_port_and_token: Tuple[int, str]) -> None:
329354
PORT, TOKEN = a_server_port_and_token
330-
r = request_get(PORT, "/python-request-headers/", TOKEN, host="127.0.0.1")
355+
r = request_get(PORT, f"/{name}/", TOKEN, host="127.0.0.1")
331356
assert r.code == 200
332357
s = r.read().decode("ascii")
333-
assert "X-Custom-Header: pytest-23456\n" in s
358+
assert f"X-Custom-Header: pytest-23456\n" in s
359+
360+
361+
def test_server_proxy_remote(a_server_port_and_token: Tuple[int, str]) -> None:
362+
PORT, TOKEN = a_server_port_and_token
363+
r = request_get(PORT, "/newproxy", TOKEN, host="127.0.0.1")
364+
assert r.code == 200
334365

335366

336367
def test_server_content_encoding_header(
@@ -498,14 +529,6 @@ def test_bad_server_proxy_url(
498529
assert "X-ProxyContextPath" not in r.headers
499530

500531

501-
def test_callable_environment_formatting(
502-
a_server_port_and_token: Tuple[int, str]
503-
) -> None:
504-
PORT, TOKEN = a_server_port_and_token
505-
r = request_get(PORT, "/python-http-callable-env/test", TOKEN)
506-
assert r.code == 200
507-
508-
509532
@pytest.mark.parametrize(
510533
"rawsocket_type",
511534
[

0 commit comments

Comments
 (0)