Skip to content

Commit

Permalink
❗ raise an exception for relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
chriddyp committed Jan 9, 2020
1 parent 86b7665 commit a0b5f1f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
10 changes: 7 additions & 3 deletions dash/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from io import open # pylint: disable=redefined-builtin
from functools import wraps
import future.utils as utils
from . import exceptions

logger = logging.getLogger()

Expand Down Expand Up @@ -57,10 +58,13 @@ def get_asset_path(requests_pathname, asset_path, asset_url_path):
def get_relative_path(requests_pathname, path):
if requests_pathname == '/' and path == '':
return '/'
if requests_pathname == '/' and not path.startswith('/'):
return path
elif requests_pathname != '/' and path == '':
return requests_pathname
elif not path.startswith('/'):
return requests_pathname + path
raise exceptions.UnsupportedRelativePath(
"Paths that aren't prefixed with a leading / are not supported.\n" +
"You supplied: {}".format(path)
)
return "/".join(
[
requests_pathname.rstrip("/"),
Expand Down
4 changes: 4 additions & 0 deletions dash/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ class SameInputOutputException(CallbackException):

class MissingCallbackContextException(CallbackException):
pass


class UnsupportedRelativePath(CallbackException):
pass
15 changes: 11 additions & 4 deletions tests/unit/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,20 @@ def test_app_name_server(empty_environ, name, server, expected):
("/my-dash-app/", "/page-1/", "/my-dash-app/page-1/"),
("/", "/page-1/sub-page-1", "/page-1/sub-page-1"),
("/", "relative-page-1", "relative-page-1"),
("/my-dash-app", "relative-page-1", "/my-dash-apprelative-page-1"),
("/my-dash-app/", "/page-1/sub-page-1", "/my-dash-app/page-1/sub-page-1"),
]
)

def test_pathname_prefix_relative_url(prefix, partial_path, expected):
path = get_relative_path(prefix, partial_path)
assert path == expected

@pytest.mark.parametrize(
"prefix, partial_path",
[
("/", "relative-page-1"),
("/my-dash-app/", "relative-page-1"),
]
)
def test_invalid_get_relative_path(prefix, partial_path):
with pytest.raises(_exc.UnsupportedRelativePath):
get_relative_path(prefix, partial_path)

0 comments on commit a0b5f1f

Please sign in to comment.