Skip to content

Commit

Permalink
Merge pull request #2468 from pgjones/2.2.x
Browse files Browse the repository at this point in the history
Ensure that rules without strict slashes match paths with slashes
  • Loading branch information
davidism authored Jul 25, 2022
2 parents 7ae6bc1 + 364d9c2 commit 76ccf1f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Version 2.2.1

Unreleased

- Fix router so that ``/path/`` will match a rule ``/path`` if strict
slashes mode is disabled for the rule. :issue:`2467`
- Restore ``ValidationError`` to be importable from
``werkzeug.routing``. :issue:`2465`


Version 2.2.0
-------------
Expand Down
1 change: 1 addition & 0 deletions src/werkzeug/routing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
from .converters import PathConverter
from .converters import UnicodeConverter
from .converters import UUIDConverter
from .converters import ValidationError
from .exceptions import BuildError
from .exceptions import NoMatch
from .exceptions import RequestAliasRedirect
Expand Down
16 changes: 16 additions & 0 deletions src/werkzeug/routing/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ def _match(
rv = _match(new_state, remaining, values + list(match.groups()))
if rv is not None:
return rv

# If there is no match and the only part left is a
# trailing slash ("") consider rules that aren't
# strict-slashes as these should match if there is a final
# slash part.
if parts == [""]:
for rule in state.rules:
if rule.strict_slashes:
continue
if rule.methods is not None and method not in rule.methods:
have_match_for.update(rule.methods)
elif rule.websocket != websocket:
websocket_mismatch = True
else:
return rule, values

return None

try:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def test_strict_slashes_leaves_dont_consume():
r.Rule("/path3/", endpoint="branch", strict_slashes=False),
r.Rule("/path4", endpoint="leaf", strict_slashes=False),
r.Rule("/path4/", endpoint="branch", strict_slashes=False),
r.Rule("/path5", endpoint="leaf"),
],
strict_slashes=False,
)
Expand All @@ -233,6 +234,7 @@ def test_strict_slashes_leaves_dont_consume():
assert adapter.match("/path3/", method="GET") == ("branch", {})
assert adapter.match("/path4", method="GET") == ("leaf", {})
assert adapter.match("/path4/", method="GET") == ("branch", {})
assert adapter.match("/path5/", method="GET") == ("leaf", {})


def test_environ_defaults():
Expand Down

0 comments on commit 76ccf1f

Please sign in to comment.