summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug.hellmann@dreamhost.com>2013-01-17 18:45:20 -0500
committerDoug Hellmann <doug.hellmann@dreamhost.com>2013-01-17 18:45:20 -0500
commit2e79f50c0a1d1a33a82124ff4f6b3f0d244d1a04 (patch)
tree6bb8dcb6c8acd60e6cb6288c865d11a37f2add6b
parent0ebd1cf90d06770301e2fca528c1adbcdf44b3c2 (diff)
downloadpecan-2e79f50c0a1d1a33a82124ff4f6b3f0d244d1a04.tar.gz
Option to disable content-type guessing by URL
Add a new option to disable content-type guessing based on the extension of the URL. Change-Id: I5bea41fcc1011cfb19255cc0333924034007a645 Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
-rw-r--r--pecan/core.py11
-rw-r--r--pecan/scaffolds/base/+package+/app.py_tmpl6
-rw-r--r--pecan/tests/test_base.py15
3 files changed, 29 insertions, 3 deletions
diff --git a/pecan/core.py b/pecan/core.py
index fb741a9..8aa6dab 100644
--- a/pecan/core.py
+++ b/pecan/core.py
@@ -177,11 +177,15 @@ class Pecan(object):
namespace automatically.
:param force_canonical: A boolean indicating if this project should
require canonical URLs.
+ :param guess_content_type_from_ext: A boolean indicating if this project
+ should use the extension in the URL for guessing
+ the content type to return.
'''
def __init__(self, root, default_renderer='mako',
template_path='templates', hooks=[], custom_renderers={},
- extra_template_vars={}, force_canonical=True):
+ extra_template_vars={}, force_canonical=True,
+ guess_content_type_from_ext=True):
if isinstance(root, basestring):
root = self.__translate_root__(root)
@@ -192,6 +196,7 @@ class Pecan(object):
self.hooks = hooks
self.template_path = template_path
self.force_canonical = force_canonical
+ self.guess_content_type_from_ext = guess_content_type_from_ext
def __translate_root__(self, item):
'''
@@ -370,7 +375,9 @@ class Pecan(object):
request.pecan['extension'] = None
# attempt to guess the content type based on the file extension
- if not request.pecan['content_type'] and '.' in path.split('/')[-1]:
+ if self.guess_content_type_from_ext \
+ and not request.pecan['content_type'] \
+ and '.' in path.split('/')[-1]:
new_path, extension = splitext(path)
# preface with a letter to ensure compat for 2.5
diff --git a/pecan/scaffolds/base/+package+/app.py_tmpl b/pecan/scaffolds/base/+package+/app.py_tmpl
index 24d0dfe..191fc11 100644
--- a/pecan/scaffolds/base/+package+/app.py_tmpl
+++ b/pecan/scaffolds/base/+package+/app.py_tmpl
@@ -12,5 +12,9 @@ def setup_app(config):
template_path=config.app.template_path,
logging=getattr(config, 'logging', {}),
debug=getattr(config.app, 'debug', False),
- force_canonical=getattr(config.app, 'force_canonical', True)
+ force_canonical=getattr(config.app, 'force_canonical', True),
+ guess_content_type_from_ext=getattr(
+ config.app,
+ 'guess_content_type_from_ext',
+ True),
)
diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py
index 043df38..e31e577 100644
--- a/pecan/tests/test_base.py
+++ b/pecan/tests/test_base.py
@@ -925,6 +925,21 @@ class TestFileTypeExtensions(unittest.TestCase):
assert r.status_int == 200
assert r.body == 'SOME VALUE'
+ def test_guessing_disabled(self):
+ class RootController(object):
+ @expose(content_type=None)
+ def _default(self, *args):
+ assert 'index.html' in args
+ assert request.pecan['extension'] is None
+ return 'SOME VALUE'
+
+ app = TestApp(Pecan(RootController(),
+ guess_content_type_from_ext=False))
+
+ r = app.get('/index.html')
+ assert r.status_int == 200
+ assert r.body == 'SOME VALUE'
+
class TestContentTypeByAcceptHeaders(unittest.TestCase):