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