diff options
-rw-r--r-- | pecan/core.py | 2 | ||||
-rw-r--r-- | pecan/decorators.py | 5 | ||||
-rw-r--r-- | tests/test_base.py | 15 |
3 files changed, 21 insertions, 1 deletions
diff --git a/pecan/core.py b/pecan/core.py index b44077e..f023608 100644 --- a/pecan/core.py +++ b/pecan/core.py @@ -136,7 +136,7 @@ class Pecan(object): node, remainder = lookup_controller(node, path) return node, remainder except NonCanonicalPath, e: - if self.force_canonical: + if self.force_canonical and not _cfg(e.controller).get('accept_noncanonical', False): raise exc.HTTPFound(add_slash=True) return e.controller, e.remainder diff --git a/pecan/decorators.py b/pecan/decorators.py index bcd9b79..212e497 100644 --- a/pecan/decorators.py +++ b/pecan/decorators.py @@ -19,6 +19,7 @@ def expose(template = None, error_handler = None, htmlfill = None, generic = False): + if template == 'json': content_type = 'application/json' def decorate(f): @@ -73,3 +74,7 @@ def transactional(ignore_redirects=True): wrap.__transactional_ignore_redirects__ = ignore_redirects return wrap return deco + +def accept_noncanonical(func): + _cfg(func)['accept_noncanonical'] = True + return func diff --git a/tests/test_base.py b/tests/test_base.py index b2215be..8c39ef3 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -5,6 +5,7 @@ from webtest import TestApp from pecan import Pecan, expose, request, response, redirect, abort, make_app, override_template from pecan.templating import _builtin_renderers as builtin_renderers, error_formatters +from pecan.decorators import accept_noncanonical import os @@ -653,6 +654,11 @@ class TestEngines(object): @expose() def index(self, arg): return arg + class AcceptController(object): + @accept_noncanonical + @expose() + def index(self): + return 'accept' class SubController(object): @expose() def index(self): @@ -664,6 +670,7 @@ class TestEngines(object): sub = SubController() arg = ArgSubController() + accept = AcceptController() app = TestApp(Pecan(RootController())) @@ -686,6 +693,14 @@ class TestEngines(object): assert r.status_int == 200 assert r.body == 'foo' + r = app.get('/accept/') + assert r.status_int == 200 + assert 'accept' == r.body + + r = app.get('/accept') + assert r.status_int == 200 + assert 'accept' == r.body + app = TestApp(Pecan(RootController(), force_canonical=False)) r = app.get('/') assert r.status_int == 200 |