From 573846e01209327030db146909de03e97cd8a858 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Thu, 13 Feb 2014 17:00:52 -0500 Subject: Fix a trailing slash bug for `RestController`s that have a `_lookup` method. Change-Id: Ibd025fc52d37f58644de23cf283afd8d4d55e2d1 Closes-Bug: #1280003 --- pecan/rest.py | 4 ++++ pecan/tests/test_rest.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/pecan/rest.py b/pecan/rest.py index 9fc18ab..4058bd6 100644 --- a/pecan/rest.py +++ b/pecan/rest.py @@ -1,6 +1,7 @@ from inspect import getargspec, ismethod from webob import exc +import six from .core import abort, request from .decorators import expose @@ -63,6 +64,9 @@ class RestController(object): try: result = handler(method, args) + # filter empty strings from the arg list + args = list(six.moves.filter(bool, args)) + # # If the signature of the handler does not match the number # of remaining positional arguments, attempt to handle diff --git a/pecan/tests/test_rest.py b/pecan/tests/test_rest.py index 6975fc2..2f5aefa 100644 --- a/pecan/tests/test_rest.py +++ b/pecan/tests/test_rest.py @@ -289,6 +289,45 @@ class TestRestController(PecanTestCase): assert r.status_int == 200 assert r.body == b_(dumps(dict(items=ThingsController.data))) + def test_getall_with_lookup(self): + + class LookupController(RestController): + + def __init__(self, _id): + self._id = _id + + @expose() + def get_all(self): + return 'ID: %s' % self._id + + class ThingsController(RestController): + + data = ['zero', 'one', 'two', 'three'] + + @expose() + def _lookup(self, _id, *remainder): + return LookupController(_id), remainder + + @expose('json') + def get_all(self): + return dict(items=self.data) + + class RootController(object): + things = ThingsController() + + # create the app + app = TestApp(make_app(RootController())) + + # test get_all + for path in ('/things', '/things/'): + r = app.get(path) + assert r.status_int == 200 + assert r.body == b_(dumps(dict(items=ThingsController.data))) + + r = app.get('/things/foo') + assert r.status_int == 200 + assert r.body == b_('ID: foo') + def test_simple_nested_rest(self): class BarController(RestController): -- cgit v1.2.1