Skip to content

Commit 1ff6051

Browse files
committed
Update standalone tests
1 parent 7ed8974 commit 1ff6051

File tree

1 file changed

+50
-51
lines changed

1 file changed

+50
-51
lines changed

tests/test_standalone.py

+50-51
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import logging
12
import sys
23
from pathlib import Path
34

45
import pytest
56
from tornado import testing
67

7-
from jupyter_server_proxy.standalone import _default_address_and_port, make_proxy_app
8+
from jupyter_server_proxy.standalone import StandaloneProxyServer
89

910
"""
1011
Test if address and port are identified correctly
@@ -13,68 +14,70 @@
1314

1415
def test_address_and_port_with_http_address(monkeypatch):
1516
monkeypatch.setenv("JUPYTERHUB_SERVICE_URL", "http://localhost/")
16-
address, port = _default_address_and_port()
17+
proxy_server = StandaloneProxyServer()
1718

18-
assert address == "localhost"
19-
assert port == 80
19+
assert proxy_server.address == "localhost"
20+
assert proxy_server.port == 80
2021

2122

2223
def test_address_and_port_with_https_address(monkeypatch):
2324
monkeypatch.setenv("JUPYTERHUB_SERVICE_URL", "https://localhost/")
24-
address, port = _default_address_and_port()
25+
proxy_server = StandaloneProxyServer()
2526

26-
assert address == "localhost"
27-
assert port == 443
27+
assert proxy_server.address == "localhost"
28+
assert proxy_server.port == 443
2829

2930

3031
def test_address_and_port_with_address_and_port(monkeypatch):
3132
monkeypatch.setenv("JUPYTERHUB_SERVICE_URL", "http://localhost:7777/")
32-
address, port = _default_address_and_port()
33-
34-
assert address == "localhost"
35-
assert port == 7777
36-
37-
38-
def make_app(unix_socket: bool, skip_authentication: bool):
39-
command = [
40-
sys.executable,
41-
str(Path(__file__).parent / "resources" / "httpinfo.py"),
42-
"--port={port}",
43-
"--unix-socket={unix_socket}",
44-
]
45-
46-
return make_proxy_app(
47-
command=command,
48-
prefix="/some/prefix",
49-
port=0,
50-
unix_socket=unix_socket,
51-
environment={},
52-
mappath={},
53-
timeout=60,
54-
skip_authentication=skip_authentication,
55-
debug=True,
56-
websocket_max_message_size=0,
57-
)
58-
59-
60-
class TestStandaloneProxyRedirect(testing.AsyncHTTPTestCase):
33+
proxy_server = StandaloneProxyServer()
34+
35+
assert proxy_server.address == "localhost"
36+
assert proxy_server.port == 7777
37+
38+
39+
class _TestStandaloneBase(testing.AsyncHTTPTestCase):
40+
runTest = None # Required for Tornado 6.1
41+
42+
unix_socket: bool
43+
skip_authentication: bool
44+
45+
def get_app(self):
46+
command = [
47+
sys.executable,
48+
str(Path(__file__).parent / "resources" / "httpinfo.py"),
49+
"--port={port}",
50+
"--unix-socket={unix_socket}",
51+
]
52+
53+
proxy_server = StandaloneProxyServer(
54+
command=command,
55+
base_url="/some/prefix",
56+
unix_socket=self.unix_socket,
57+
timeout=60,
58+
skip_authentication=self.skip_authentication,
59+
log_level=logging.DEBUG,
60+
)
61+
62+
return proxy_server.create_app()
63+
64+
65+
class TestStandaloneProxyRedirect(_TestStandaloneBase):
6166
"""
6267
Ensure requests are proxied to the application. We need to disable authentication here,
6368
as we do not want to be redirected to the JupyterHub Login.
6469
"""
6570

66-
runTest = None # Required for Tornado 6.1
67-
68-
def get_app(self):
69-
return make_app(False, True)
71+
unix_socket = False
72+
skip_authentication = True
7073

7174
def test_add_slash(self):
7275
response = self.fetch("/some/prefix", follow_redirects=False)
7376

7477
assert response.code == 301
7578
assert response.headers.get("Location") == "/some/prefix/"
7679

77-
def test_without_prefix(self):
80+
def test_wrong_prefix(self):
7881
response = self.fetch("/some/other/prefix")
7982

8083
assert response.code == 404
@@ -92,11 +95,9 @@ def test_on_prefix(self):
9295
@pytest.mark.skipif(
9396
sys.platform == "win32", reason="Unix socket not supported on Windows"
9497
)
95-
class TestStandaloneProxyWithUnixSocket(testing.AsyncHTTPTestCase):
96-
runTest = None # Required for Tornado 6.1
97-
98-
def get_app(self):
99-
return make_app(True, True)
98+
class TestStandaloneProxyWithUnixSocket(_TestStandaloneBase):
99+
unix_socket = True
100+
skip_authentication = True
100101

101102
def test_with_unix_socket(self):
102103
response = self.fetch("/some/prefix/")
@@ -108,15 +109,13 @@ def test_with_unix_socket(self):
108109
assert "X-Proxycontextpath: /some/prefix/" in body
109110

110111

111-
class TestStandaloneProxyLogin(testing.AsyncHTTPTestCase):
112+
class TestStandaloneProxyLogin(_TestStandaloneBase):
112113
"""
113114
Ensure we redirect to JupyterHub login when authentication is enabled
114115
"""
115116

116-
runTest = None # Required for Tornado 6.1
117-
118-
def get_app(self):
119-
return make_app(False, False)
117+
unix_socket = False
118+
skip_authentication = False
120119

121120
def test_redirect_to_login_url(self):
122121
response = self.fetch("/some/prefix/", follow_redirects=False)

0 commit comments

Comments
 (0)