Skip to content

Commit ff55c48

Browse files
committed
add tests for extension URLs with base_url
1 parent 18455ec commit ff55c48

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

jupyter_server/extension/application.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import sys
22
import re
33
import logging
4+
from urllib.parse import urljoin
45

56
from jinja2 import Environment, FileSystemLoader
67

78
from traitlets import (
89
Unicode,
910
List,
1011
Dict,
11-
Bool,
12-
default,
13-
validate
12+
default
1413
)
1514
from traitlets.config import Config
1615
from tornado.log import LogFormatter
@@ -191,8 +190,10 @@ def _default_log_format(self):
191190

192191
@property
193192
def static_url_prefix(self):
194-
return "/static/{name}/".format(
195-
name=self.name)
193+
static_url = "static/{name}".format(
194+
name=self.name
195+
)
196+
return urljoin(self.serverapp.base_url, static_url)
196197

197198
static_paths = List(Unicode(),
198199
help="""paths to search for serving static files.
@@ -288,6 +289,7 @@ def _prepare_handlers(self):
288289
# Add static endpoint for this extension, if static paths are given.
289290
if len(self.static_paths) > 0:
290291
# Append the extension's static directory to server handlers.
292+
print(self.static_url_prefix)
291293
static_url = url_path_join(self.static_url_prefix, "(.*)")
292294

293295
# Construct handler.

jupyter_server/extension/handler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from urllib.parse import urljoin
12
from jupyter_server.base.handlers import FileFindHandler
23

34

@@ -55,10 +56,10 @@ def base_url(self):
5556

5657
@property
5758
def static_url_prefix(self):
58-
return "{base_url}/static/{name}/".format(
59-
name=self.name,
60-
base_url=self.base_url
59+
static_url = "static/{name}".format(
60+
name=self.name
6161
)
62+
return urljoin(self.serverapp.base_url, static_url)
6263

6364
@property
6465
def static_path(self):

tests/extension/mockextensions/app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from traitlets import Unicode, List
23

34
from jupyter_server.base.handlers import JupyterHandler
@@ -10,6 +11,8 @@
1011
ExtensionHandlerJinjaMixin
1112
)
1213

14+
STATIC_PATH = os.path.join(os.path.dirname(__file__), "static")
15+
1316

1417
class MockExtensionHandler(ExtensionHandlerMixin, JupyterHandler):
1518

@@ -31,6 +34,7 @@ class MockExtensionApp(ExtensionAppJinjaMixin, ExtensionApp):
3134

3235
name = 'mockextension'
3336
template_paths = List().tag(config=True)
37+
static_paths = [STATIC_PATH]
3438
mock_trait = Unicode('mock trait', config=True)
3539
loaded = False
3640

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock static content

tests/extension/test_handler.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,42 @@ async def test_handler_argv(fetch):
7272
)
7373
assert r.code == 200
7474
assert r.body.decode() == 'test mock trait'
75+
76+
77+
@pytest.mark.parametrize(
78+
'server_config',
79+
[
80+
{
81+
"ServerApp": {
82+
"jpserver_extensions": {
83+
"tests.extension.mockextensions": True
84+
},
85+
# Move extension handlers behind a url prefix
86+
"base_url": "test_prefix"
87+
},
88+
"MockExtensionApp": {
89+
# Change a trait in the MockExtensionApp using
90+
# the following config value.
91+
"mock_trait": "test mock trait"
92+
}
93+
}
94+
]
95+
)
96+
async def test_base_url(fetch):
97+
# Test that the extension's handlers were properly prefixed
98+
r = await fetch(
99+
'test_prefix', 'mock',
100+
method='GET'
101+
)
102+
assert r.code == 200
103+
assert r.body.decode() == 'test mock trait'
104+
105+
# Test that the static namespace was prefixed by base_url
106+
r = await fetch(
107+
'test_prefix',
108+
'static', 'mockextension', 'mock.txt',
109+
method='GET'
110+
)
111+
assert r.code == 200
112+
body = r.body.decode()
113+
assert "mock static content" in body

0 commit comments

Comments
 (0)