From a11a292cd9764260b65ed4a7cd7c763ef843c3f0 Mon Sep 17 00:00:00 2001 From: Min RK Date: Mon, 22 Mar 2021 09:30:17 +0100 Subject: [PATCH] test custom hub routespecs --- jupyterhub/app.py | 3 +-- jupyterhub/tests/test_app.py | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/jupyterhub/app.py b/jupyterhub/app.py index 1b9c20e8..bed2c0b3 100644 --- a/jupyterhub/app.py +++ b/jupyterhub/app.py @@ -905,8 +905,7 @@ class JupyterHub(Application): routespec = routespec + "/" if not self.subdomain_host and not routespec.startswith("/"): routespec = "/" + routespec - if routespec != proposal.value: - return routespec + return routespec @observe("hub_routespec") def _hub_routespec_changed(self, change): diff --git a/jupyterhub/tests/test_app.py b/jupyterhub/tests/test_app.py index a918e9b7..47fdb1d7 100644 --- a/jupyterhub/tests/test_app.py +++ b/jupyterhub/tests/test_app.py @@ -1,5 +1,6 @@ """Test the JupyterHub entry point""" import binascii +import logging import os import re import sys @@ -329,3 +330,41 @@ def test_url_config(hub_config, expected): # validate additional properties for key, value in expected.items(): assert getattr(app, key) == value + + +@pytest.mark.parametrize( + "base_url, hub_routespec, expected_routespec, should_warn, bad_prefix", + [ + (None, None, "/", False, False), + ("/", "/", "/", False, False), + ("/base", "/base", "/base/", False, False), + ("/", "/hub", "/hub/", True, False), + (None, "hub/api", "/hub/api/", True, False), + ("/base", "/hub/", "/hub/", True, True), + (None, "/hub/api/health", "/hub/api/health/", True, True), + ], +) +def test_hub_routespec( + base_url, hub_routespec, expected_routespec, should_warn, bad_prefix, caplog +): + cfg = Config() + if base_url: + cfg.JupyterHub.base_url = base_url + if hub_routespec: + cfg.JupyterHub.hub_routespec = hub_routespec + with caplog.at_level(logging.WARNING): + app = JupyterHub(config=cfg, log=logging.getLogger()) + app.init_hub() + hub = app.hub + assert hub.routespec == expected_routespec + + if should_warn: + assert "custom route for Hub" in caplog.text + assert hub_routespec in caplog.text + else: + assert "custom route for Hub" not in caplog.text + + if bad_prefix: + assert "may not receive" in caplog.text + else: + assert "may not receive" not in caplog.text