diff options
author | Ryan Petrello <lists@ryanpetrello.com> | 2012-11-04 21:18:17 -0500 |
---|---|---|
committer | Ryan Petrello <lists@ryanpetrello.com> | 2012-11-04 21:18:17 -0500 |
commit | a8af1111380dca03facee63086106f22509589ff (patch) | |
tree | 4f27c9ea746fa56c7be84e9b803e2e84a0433953 | |
parent | 337ddc94afa1e4488bdeb64c8ed1a706acb8e29f (diff) | |
download | pecan-a8af1111380dca03facee63086106f22509589ff.tar.gz |
More refinements for HTTP Accept content type guessing.
-rw-r--r-- | pecan/core.py | 40 | ||||
-rw-r--r-- | pecan/tests/test_base.py | 6 |
2 files changed, 22 insertions, 24 deletions
diff --git a/pecan/core.py b/pecan/core.py index 20576e1..8155180 100644 --- a/pecan/core.py +++ b/pecan/core.py @@ -1,27 +1,29 @@ -from templating import RendererFactory -from routing import lookup_controller, NonCanonicalPath -from util import _cfg, encode_if_needed -from middleware.recursive import ForwardRequestException - -from webob import Request, Response, exc, acceptparse +import urllib +import sys +try: + from simplejson import loads +except ImportError: # pragma: no cover + from json import loads # noqa from threading import local from itertools import chain from mimetypes import guess_type, add_type from urlparse import urlsplit, urlunsplit from os.path import splitext +import logging -try: - from simplejson import loads -except ImportError: # pragma: no cover - from json import loads # noqa +from webob import Request, Response, exc, acceptparse + +from templating import RendererFactory +from routing import lookup_controller, NonCanonicalPath +from util import _cfg, encode_if_needed +from middleware.recursive import ForwardRequestException -import urllib -import sys # make sure that json is defined in mimetypes add_type('application/json', '.json', True) state = local() +logger = logging.getLogger(__name__) def proxy(key): @@ -403,18 +405,16 @@ class Pecan(object): ) if best_default is None: - import warnings msg = "Controller '%s' defined does not support " + \ "content_type '%s'. Supported type(s): %s" - warnings.warn( + logger.error( msg % ( controller.__name__, request.pecan['content_type'], cfg.get('content_types', {}).keys() - ), - RuntimeWarning + ) ) - raise exc.HTTPNotFound + raise exc.HTTPNotAcceptable() request.pecan['content_type'] = best_default else: @@ -426,16 +426,14 @@ class Pecan(object): request.pecan['content_type'] not in \ cfg.get('content_types', {}): - import warnings msg = "Controller '%s' defined does not support content_type " + \ "'%s'. Supported type(s): %s" - warnings.warn( + logger.error( msg % ( controller.__name__, request.pecan['content_type'], cfg.get('content_types', {}).keys() - ), - RuntimeWarning + ) ) raise exc.HTTPNotFound diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py index 986d98b..13d1c94 100644 --- a/pecan/tests/test_base.py +++ b/pecan/tests/test_base.py @@ -944,11 +944,11 @@ class TestContentTypeByAcceptHeaders(unittest.TestCase): assert r.status_int == 200 assert r.content_type == 'text/html' - def test_no_match(self): + def test_not_acceptable(self): r = self.app_.get('/', headers={ 'Accept': 'application/xml', - }, status=404) - assert r.status_int == 404 + }, status=406) + assert r.status_int == 406 def test_accept_header_missing(self): r = self.app_.get('/') |