base_url configuration setting, closes #394

* base_url configuration setting
* base_url works for static assets as well
This commit is contained in:
Simon Willison 2020-03-24 17:18:43 -07:00 committed by GitHub
commit 7656fd64d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 104 additions and 28 deletions

View file

@ -351,7 +351,7 @@ def prepare_connection(conn, database, datasette):
@hookimpl
def extra_css_urls(template, database, table, datasette):
return ['https://example.com/{}/extra-css-urls-demo.css'.format(
return ['https://plugin-example.com/{}/extra-css-urls-demo.css'.format(
base64.b64encode(json.dumps({
"template": template,
"database": database,
@ -363,9 +363,9 @@ def extra_css_urls(template, database, table, datasette):
@hookimpl
def extra_js_urls():
return [{
'url': 'https://example.com/jquery.js',
'url': 'https://plugin-example.com/jquery.js',
'sri': 'SRIHASH',
}, 'https://example.com/plugin1.js']
}, 'https://plugin-example.com/plugin1.js']
@hookimpl
@ -421,9 +421,9 @@ import json
@hookimpl
def extra_js_urls():
return [{
'url': 'https://example.com/jquery.js',
'url': 'https://plugin-example.com/jquery.js',
'sri': 'SRIHASH',
}, 'https://example.com/plugin2.js']
}, 'https://plugin-example.com/plugin2.js']
@hookimpl

View file

@ -1307,6 +1307,7 @@ def test_config_json(app_client):
"force_https_urls": False,
"hash_urls": False,
"template_debug": False,
"base_url": "/",
} == response.json

View file

@ -1157,3 +1157,46 @@ def test_metadata_sort_desc(app_client):
table = Soup(response.body, "html.parser").find("table")
rows = [[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")]
assert list(reversed(expected)) == rows
@pytest.mark.parametrize("base_url", ["/prefix/", "https://example.com/"])
@pytest.mark.parametrize(
"path",
[
"/",
"/fixtures",
"/fixtures/compound_three_primary_keys",
"/fixtures/compound_three_primary_keys/a,a,a",
"/fixtures/paginated_view",
],
)
def test_base_url_config(base_url, path):
for client in make_app_client(config={"base_url": base_url}):
response = client.get(base_url + path.lstrip("/"))
soup = Soup(response.body, "html.parser")
for el in soup.findAll(["a", "link", "script"]):
if "href" in el.attrs:
href = el["href"]
elif "src" in el.attrs:
href = el["src"]
else:
continue # Could be a <script>...</script>
if (
not href.startswith("#")
and href
not in {
"https://github.com/simonw/datasette",
"https://github.com/simonw/datasette/blob/master/LICENSE",
"https://github.com/simonw/datasette/blob/master/tests/fixtures.py",
}
and not href.startswith("https://plugin-example.com/")
):
# If this has been made absolute it may start http://localhost/
if href.startswith("http://localhost/"):
href = href[len("http://localost/") :]
assert href.startswith(base_url), {
"base_url": base_url,
"path": path,
"href_or_src": href,
"element_parent": str(el.parent),
}

View file

@ -64,7 +64,7 @@ def test_plugin_extra_js_urls(app_client):
== {
"integrity": "SRIHASH",
"crossorigin": "anonymous",
"src": "https://example.com/jquery.js",
"src": "https://plugin-example.com/jquery.js",
}
]
@ -74,7 +74,7 @@ def test_plugins_with_duplicate_js_urls(app_client):
response = app_client.get("/fixtures")
# This test is a little tricky, as if the user has any other plugins in
# their current virtual environment those may affect what comes back too.
# What matters is that https://example.com/jquery.js is only there once
# What matters is that https://plugin-example.com/jquery.js is only there once
# and it comes before plugin1.js and plugin2.js which could be in either
# order
scripts = Soup(response.body, "html.parser").findAll("script")
@ -82,16 +82,16 @@ def test_plugins_with_duplicate_js_urls(app_client):
# No duplicates allowed:
assert len(srcs) == len(set(srcs))
# jquery.js loaded once:
assert 1 == srcs.count("https://example.com/jquery.js")
assert 1 == srcs.count("https://plugin-example.com/jquery.js")
# plugin1.js and plugin2.js are both there:
assert 1 == srcs.count("https://example.com/plugin1.js")
assert 1 == srcs.count("https://example.com/plugin2.js")
assert 1 == srcs.count("https://plugin-example.com/plugin1.js")
assert 1 == srcs.count("https://plugin-example.com/plugin2.js")
# jquery comes before them both
assert srcs.index("https://example.com/jquery.js") < srcs.index(
"https://example.com/plugin1.js"
assert srcs.index("https://plugin-example.com/jquery.js") < srcs.index(
"https://plugin-example.com/plugin1.js"
)
assert srcs.index("https://example.com/jquery.js") < srcs.index(
"https://example.com/plugin2.js"
assert srcs.index("https://plugin-example.com/jquery.js") < srcs.index(
"https://plugin-example.com/plugin2.js"
)