Skip to content

Commit 173ef31

Browse files
committed
Add tests for Callable traits
1 parent b0c7feb commit 173ef31

File tree

2 files changed

+90
-28
lines changed

2 files changed

+90
-28
lines changed

tests/resources/jupyter_server_config.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,29 @@ 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+
20+
def get_command_unix_socket(unix_socket):
21+
return [sys.executable, _get_path("httpinfo.py"), f"--unix-socket={unix_socket}"]
22+
23+
24+
def get_environment(base_url):
25+
return {"JUPYTERLAB_BASE_URL": base_url, "MYVAR": "String with escaped {{var}}"}
26+
27+
1628
def mappathf(path):
1729
p = path + "mapped"
1830
return p
1931

2032

33+
def request_headers_overwrite():
34+
return {
35+
"X-Custom-Header": "pytest-23456",
36+
}
37+
38+
2139
def translate_ciao(path, host, response, orig_response, port):
2240
# Assume that the body has not been modified by any previous rewrite
2341
assert response.body == orig_response.body
@@ -47,10 +65,6 @@ def cats_only(response, path):
4765
response.body = b"dogs not allowed"
4866

4967

50-
def my_env():
51-
return {"MYVAR": "String with escaped {{var}}"}
52-
53-
5468
c.ServerProxy.servers = {
5569
"python-http": {
5670
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
@@ -63,19 +77,39 @@ def my_env():
6377
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
6478
"port": 54321,
6579
},
80+
"python-http-callable-command": {
81+
"command": get_command,
82+
},
6683
"python-http-mappath": {
6784
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
6885
"mappath": {
6986
"/": "/index.html",
7087
},
7188
},
72-
"python-http-mappathf": {
89+
"python-http-callable-mappath": {
7390
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
7491
"mappath": mappathf,
7592
},
76-
"python-http-callable-env": {
93+
"python-http-environment": {
94+
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
95+
"environment": {
96+
"JUPYTERLAB_BASE_URL": "{base_url}",
97+
"MYVAR": "String with escaped {{var}}",
98+
},
99+
},
100+
"python-http-callable-environment": {
77101
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
78-
"environment": my_env,
102+
"environment": get_environment,
103+
},
104+
"python-http-request-headers": {
105+
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
106+
"request_headers_override": {
107+
"X-Custom-Header": "pytest-23456",
108+
},
109+
},
110+
"python-http-callable-request-headers": {
111+
"command": [sys.executable, _get_path("httpinfo.py"), "--port={port}"],
112+
"request_headers_override": request_headers_overwrite,
79113
},
80114
"python-websocket": {
81115
"command": [sys.executable, _get_path("websocket.py"), "--port={port}"],
@@ -94,6 +128,10 @@ def my_env():
94128
],
95129
"unix_socket": True,
96130
},
131+
"python-unix-socket-callable": {
132+
"command": get_command_unix_socket,
133+
"unix_socket": True,
134+
},
97135
"python-unix-socket-file": {
98136
"command": [
99137
sys.executable,
@@ -107,12 +145,6 @@ def my_env():
107145
# python-unix-socket-file
108146
"unix_socket": "/tmp/jupyter-server-proxy-test-socket",
109147
},
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-
},
116148
"python-gzipserver": {
117149
"command": [sys.executable, _get_path("gzipserver.py"), "{port}"],
118150
},

tests/test_proxies.py

Lines changed: 45 additions & 15 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(
@@ -279,6 +285,18 @@ def test_server_proxy_port_non_service_rewrite_response(
279285
assert s.startswith("GET /foo?token=")
280286

281287

288+
def test_server_proxy_command_callable(
289+
a_server_port_and_token: Tuple[int, str]
290+
) -> None:
291+
PORT, TOKEN = a_server_port_and_token
292+
r = request_get(PORT, "/python-http-callable-command/abc", TOKEN)
293+
assert r.code == 200
294+
s = r.read().decode("ascii")
295+
assert s.startswith("GET /abc?token=")
296+
assert "X-Forwarded-Context: /python-http-callable-command\n" in s
297+
assert "X-Proxycontextpath: /python-http-callable-command\n" in s
298+
299+
282300
@pytest.mark.parametrize(
283301
"requestpath,expected",
284302
[
@@ -311,28 +329,48 @@ def test_server_proxy_mappath_callable(
311329
requestpath, expected, a_server_port_and_token: Tuple[int, str]
312330
) -> None:
313331
PORT, TOKEN = a_server_port_and_token
314-
r = request_get(PORT, "/python-http-mappathf" + requestpath, TOKEN)
332+
r = request_get(PORT, "/python-http-callable-mappath" + requestpath, TOKEN)
315333
assert r.code == 200
316334
s = r.read().decode("ascii")
317335
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
336+
assert "X-Forwarded-Context: /python-http-callable-mappath\n" in s
337+
assert "X-Proxycontextpath: /python-http-callable-mappath\n" in s
320338

321339

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

327354

328-
def test_server_request_headers(a_server_port_and_token: Tuple[int, str]) -> None:
355+
@pytest.mark.parametrize(
356+
"name", ["python-http-request-headers", "python-http-callable-request-headers"]
357+
)
358+
def test_server_proxy_request_headers(
359+
name, a_server_port_and_token: Tuple[int, str]
360+
) -> None:
329361
PORT, TOKEN = a_server_port_and_token
330-
r = request_get(PORT, "/python-request-headers/", TOKEN, host="127.0.0.1")
362+
r = request_get(PORT, f"/{name}/", TOKEN, host="127.0.0.1")
331363
assert r.code == 200
332364
s = r.read().decode("ascii")
333365
assert "X-Custom-Header: pytest-23456\n" in s
334366

335367

368+
def test_server_proxy_remote(a_server_port_and_token: Tuple[int, str]) -> None:
369+
PORT, TOKEN = a_server_port_and_token
370+
r = request_get(PORT, "/newproxy", TOKEN, host="127.0.0.1")
371+
assert r.code == 200
372+
373+
336374
def test_server_content_encoding_header(
337375
a_server_port_and_token: Tuple[int, str]
338376
) -> None:
@@ -498,14 +536,6 @@ def test_bad_server_proxy_url(
498536
assert "X-ProxyContextPath" not in r.headers
499537

500538

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-
509539
@pytest.mark.parametrize(
510540
"rawsocket_type",
511541
[

0 commit comments

Comments
 (0)