Skip to content

Commit 685cd69

Browse files
committed
🔧 handle prefixes in the middle of the supplied paths
and paths that look like request_pathname_prefix but don’t have the trailing slash
1 parent 20dd95f commit 685cd69

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

dash/_utils.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,27 @@ def get_relative_path(requests_pathname, path):
7575
def strip_relative_path(requests_pathname, path):
7676
if path is None:
7777
return None
78-
elif not path.startswith('/'):
78+
elif ((requests_pathname != '/' and
79+
not path.startswith(requests_pathname.rstrip('/')))
80+
or (requests_pathname == '/' and not path.startswith('/'))):
7981
raise exceptions.UnsupportedRelativePath(
80-
"Paths that aren't prefixed with a leading / are not supported.\n" +
81-
"You supplied: {}".format(path)
82+
"Paths that aren't prefixed with a leading " +
83+
"requests_pathname_prefix are not supported.\n" +
84+
"You supplied: {} and requests_pathname_prefix was {}".format(
85+
path,
86+
requests_pathname
87+
)
88+
)
89+
if (requests_pathname != '/' and
90+
path.startswith(requests_pathname.rstrip('/'))):
91+
path = path.replace(
92+
# handle the case where the path might be `/my-dash-app`
93+
# but the requests_pathname_prefix is `/my-dash-app/`
94+
requests_pathname.rstrip('/'),
95+
'',
96+
1
8297
)
83-
if requests_pathname != '/':
84-
path = path.replace(requests_pathname, '')
85-
return path.lstrip('/').rstrip('/')
98+
return path.strip('/')
8699

87100

88101
# pylint: disable=no-member

tests/unit/test_configs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def test_invalid_get_relative_path(prefix, partial_path):
203203
("/my-dash-app/", None, None),
204204
205205
("/", "/", ""),
206-
("/my-dash-app/", "/", ""),
206+
("/my-dash-app/", "/my-dash-app", ""),
207+
("/my-dash-app/", "/my-dash-app/", ""),
207208
208209
("/", "/page-1", "page-1"),
209210
("/my-dash-app/", "/my-dash-app/page-1", "page-1"),
@@ -216,6 +217,9 @@ def test_invalid_get_relative_path(prefix, partial_path):
216217
217218
("/", "/page-1/sub-page-1/", "page-1/sub-page-1"),
218219
("/my-dash-app/", "/my-dash-app/page-1/sub-page-1/", "page-1/sub-page-1"),
220+
221+
("/my-dash-app/", "/my-dash-app/my-dash-app/", "my-dash-app"),
222+
("/my-dash-app/", "/my-dash-app/something-else/my-dash-app/", "something-else/my-dash-app"),
219223
]
220224
)
221225
def test_strip_relative_path(prefix, partial_path, expected):
@@ -228,6 +232,7 @@ def test_strip_relative_path(prefix, partial_path, expected):
228232
[
229233
("/", "relative-page-1"),
230234
("/my-dash-app", "relative-page-1"),
235+
("/my-dash-app", "/some-other-path")
231236
]
232237
)
233238
def test_invalid_strip_relative_path(prefix, partial_path):

0 commit comments

Comments
 (0)