Skip to content

Commit

Permalink
Address PrefixedUrlString bug in #1075
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 31, 2020
1 parent bf18b9b commit a4ca26a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ async def _asset_urls(self, key, template, context, request, view_name):
if url in seen_urls:
continue
seen_urls.add(url)
if url.startswith("/"):
# Take base_url into account:
url = self.urls.path(url)
if sri:
output.append({"url": url, "sri": sri})
else:
Expand Down
7 changes: 5 additions & 2 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asyncio
from contextlib import contextmanager
import click
from collections import OrderedDict, namedtuple
import base64
import click
import hashlib
import inspect
import itertools
Expand Down Expand Up @@ -1016,8 +1016,11 @@ class PrefixedUrlString(str):
def __add__(self, other):
return type(self)(super().__add__(other))

def __str__(self):
return super().__str__()

def __getattribute__(self, name):
if name in dir(str):
if not name.startswith("__") and name in dir(str):

def method(self, *args, **kwargs):
value = getattr(super(), name)(*args, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ def generate_sortable_rows(num):
"source_url": "https://github.com/simonw/datasette/blob/master/tests/fixtures.py",
"about": "About Datasette",
"about_url": "https://github.com/simonw/datasette",
"extra_css_urls": ["/static/extra-css-urls.css"],
"plugins": {
"name-of-plugin": {"depth": "root"},
"env-plugin": {"foo": {"$env": "FOO_ENV"}},
Expand Down
1 change: 1 addition & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,7 @@ def test_paginate_using_link_header(app_client, qs):
num_pages = 0
while path:
response = app_client.get(path)
assert response.status == 200
num_pages += 1
link = response.headers.get("link")
if link:
Expand Down
1 change: 1 addition & 0 deletions tests/test_cli_serve_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_serve_with_get_exit_code_for_error(tmp_path_factory):
"--get",
"/this-is-404",
],
catch_exceptions=False,
)
assert result.exit_code == 1
assert "404" in result.output
5 changes: 5 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,11 @@ def test_base_url_config(app_client_base_url_prefix, path):
}


def test_base_url_affects_metadata_extra_css_urls(app_client_base_url_prefix):
html = app_client_base_url_prefix.get("/").text
assert '<link rel="stylesheet" href="/prefix/static/extra-css-urls.css">' in html


@pytest.mark.parametrize(
"path,expected",
[
Expand Down

0 comments on commit a4ca26a

Please sign in to comment.