1
+ import logging
1
2
import sys
2
3
from pathlib import Path
3
4
4
5
import pytest
5
6
from tornado import testing
6
7
7
- from jupyter_server_proxy .standalone import _default_address_and_port , make_proxy_app
8
+ from jupyter_server_proxy .standalone import StandaloneProxyServer
8
9
9
10
"""
10
11
Test if address and port are identified correctly
13
14
14
15
def test_address_and_port_with_http_address (monkeypatch ):
15
16
monkeypatch .setenv ("JUPYTERHUB_SERVICE_URL" , "http://localhost/" )
16
- address , port = _default_address_and_port ()
17
+ proxy_server = StandaloneProxyServer ()
17
18
18
- assert address == "localhost"
19
- assert port == 80
19
+ assert proxy_server . address == "localhost"
20
+ assert proxy_server . port == 80
20
21
21
22
22
23
def test_address_and_port_with_https_address (monkeypatch ):
23
24
monkeypatch .setenv ("JUPYTERHUB_SERVICE_URL" , "https://localhost/" )
24
- address , port = _default_address_and_port ()
25
+ proxy_server = StandaloneProxyServer ()
25
26
26
- assert address == "localhost"
27
- assert port == 443
27
+ assert proxy_server . address == "localhost"
28
+ assert proxy_server . port == 443
28
29
29
30
30
31
def test_address_and_port_with_address_and_port (monkeypatch ):
31
32
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 ):
61
66
"""
62
67
Ensure requests are proxied to the application. We need to disable authentication here,
63
68
as we do not want to be redirected to the JupyterHub Login.
64
69
"""
65
70
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
70
73
71
74
def test_add_slash (self ):
72
75
response = self .fetch ("/some/prefix" , follow_redirects = False )
73
76
74
77
assert response .code == 301
75
78
assert response .headers .get ("Location" ) == "/some/prefix/"
76
79
77
- def test_without_prefix (self ):
80
+ def test_wrong_prefix (self ):
78
81
response = self .fetch ("/some/other/prefix" )
79
82
80
83
assert response .code == 404
@@ -92,11 +95,9 @@ def test_on_prefix(self):
92
95
@pytest .mark .skipif (
93
96
sys .platform == "win32" , reason = "Unix socket not supported on Windows"
94
97
)
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
100
101
101
102
def test_with_unix_socket (self ):
102
103
response = self .fetch ("/some/prefix/" )
@@ -108,15 +109,13 @@ def test_with_unix_socket(self):
108
109
assert "X-Proxycontextpath: /some/prefix/" in body
109
110
110
111
111
- class TestStandaloneProxyLogin (testing . AsyncHTTPTestCase ):
112
+ class TestStandaloneProxyLogin (_TestStandaloneBase ):
112
113
"""
113
114
Ensure we redirect to JupyterHub login when authentication is enabled
114
115
"""
115
116
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
120
119
121
120
def test_redirect_to_login_url (self ):
122
121
response = self .fetch ("/some/prefix/" , follow_redirects = False )
0 commit comments