Skip to content

Commit f635bd6

Browse files
committed
try_parse_url_path -> fix multiple slashes issue
1 parent acf1166 commit f635bd6

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

aikido_zen/helpers/build_route_from_url_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,22 @@ def test_replaces_ulid_strings():
144144

145145
# 25 characters
146146
assert build_route_from_url("/posts/01arz3ndektsv4rrffq69g5fa") != "/posts/:ulid"
147+
148+
149+
def test_multiple_slashes():
150+
assert build_route_from_url("//test_ratelimiting_1//") == "/test_ratelimiting_1"
151+
152+
assert (
153+
build_route_from_url("////posts/66ec29159d00113616fc7184//")
154+
== "/posts/:objectId"
155+
)
156+
157+
assert (
158+
build_route_from_url("/posts///66ec29159d00113616fc71845")
159+
== "/posts/66ec29159d00113616fc71845"
160+
)
161+
162+
assert (
163+
build_route_from_url("//posts//66ec29159d00113616fc718//")
164+
== "/posts/66ec29159d00113616fc718"
165+
)

aikido_zen/helpers/try_parse_url_path.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from urllib.parse import urlparse
8+
import regex as re
89

910

1011
def try_parse_url(url):
@@ -26,4 +27,9 @@ def try_parse_url_path(url):
2627
return None
2728
if parsed.path == "":
2829
return "/"
29-
return parsed.path
30+
31+
# Multiple slashes are ignored in python, so we want to also remove them here
32+
# This allows the route building & endpoint matching to work properly.
33+
normalized_path = re.sub(r"/+", "/", parsed.path)
34+
35+
return normalized_path

aikido_zen/helpers/try_parse_url_path_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ def test_try_parse_url_path_for_absolute_url():
2222
assert try_parse_url_path("http://localhost/posts/3") == "/posts/3"
2323

2424

25+
def test_try_parse_url_path_for_absolute_url_with_multiple_slashes():
26+
assert try_parse_url_path("http://localhost/posts//////3////") == "/posts/3/"
27+
assert try_parse_url_path("http://localhost/posts//3//") == "/posts/3/"
28+
assert try_parse_url_path("http://localhost////") == "/"
29+
assert try_parse_url_path("http://localhost//") == "/"
30+
31+
2532
def test_try_parse_url_path_for_absolute_url_with_query():
2633
assert try_parse_url_path("http://localhost/posts/3?abc=def") == "/posts/3"
2734

0 commit comments

Comments
 (0)