Skip to content

Commit

Permalink
parts: Split out extend(), fix a bug, and test.
Browse files Browse the repository at this point in the history
So that it won't happen again.
  • Loading branch information
MostAwesomeDude committed Oct 9, 2012
1 parent 1fec54e commit 41947bc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
27 changes: 23 additions & 4 deletions flask_holster/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ def holsterize(app, view):
return p


def extend(route):
"""
Add a file extension to a route.
This function adds a file extension, called "ext", to a route.
If the route ends with a trailing slash, then the extension precedes the
slash. This preserves the standard directory-redirect mechanism of
Werkzeug.
If the route is the root, then the extension is tacked onto the end. This
looks awkward but there's no better solution.
"""

if route == "/":
return "/.<ext>"
elif route.endswith("/"):
return "%s.<ext>/" % route[:-1]
else:
return "%s.<ext>" % route


def bare_holster(app, route):
"""
Decorator which replaces ``route()``.
Expand All @@ -75,10 +97,7 @@ def index():
for holstering views, only for attaching them to the app.
"""

if route.endswith("/"):
extended = "%s.<ext>/" % route[:-1]
else:
extended = "%s.<ext>" % route
extended = extend(route)

def inner(view):
name = view.__name__
Expand Down
25 changes: 25 additions & 0 deletions flask_holster/test/test_parts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from unittest import TestCase

from flask_holster.parts import extend

class TestExtend(TestCase):

def test_extend_standard(self):
i = "/standard"
o = "/standard.<ext>"
self.assertEqual(extend(i), o)

def test_extend_params(self):
i = "/<params>"
o = "/<params>.<ext>"
self.assertEqual(extend(i), o)

def test_extend_root(self):
i = "/"
o = "/.<ext>"
self.assertEqual(extend(i), o)

def test_extend_directory(self):
i = "/dir/"
o = "/dir.<ext>/"
self.assertEqual(extend(i), o)

0 comments on commit 41947bc

Please sign in to comment.