summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2018-10-01 23:17:35 -0500
committerMichael Merickel <michael@merickel.org>2018-10-01 23:17:35 -0500
commitbd82c8b17d2f7b0b0c6ecb0e80bc36393a7d807e (patch)
treead2d3fc16d7b1598ca6eb7287865a4a36d5c0325
parentf2294fb6969a7bc04642f2f987dec5ee131ad98c (diff)
downloadpyramid-bd82c8b17d2f7b0b0c6ecb0e80bc36393a7d807e.tar.gz
pyramid-bd82c8b17d2f7b0b0c6ecb0e80bc36393a7d807e.tar.bz2
pyramid-bd82c8b17d2f7b0b0c6ecb0e80bc36393a7d807e.zip
fix coverage
-rw-r--r--pyramid/config/views.py15
-rw-r--r--pyramid/tests/test_config/test_views.py66
2 files changed, 76 insertions, 5 deletions
diff --git a/pyramid/config/views.py b/pyramid/config/views.py
index 26e86d7d3..3e4dbea0e 100644
--- a/pyramid/config/views.py
+++ b/pyramid/config/views.py
@@ -141,7 +141,7 @@ class MultiView(object):
accepts = set(self.accepts)
accepts.add(accept)
if accept_order:
- accept_order = accept_order.sorted()
+ accept_order = [v for _, v in accept_order.sorted()]
self.accepts = sort_accept_offers(accepts, accept_order)
def get_views(self, request):
@@ -1235,7 +1235,6 @@ class ViewsConfiguratorMixin(object):
'application/xml',
'text/xml',
'text/plain',
- 'application/json',
):
self.add_accept_view_order(accept)
@@ -1260,7 +1259,13 @@ class ViewsConfiguratorMixin(object):
``application/json`` or ``text/html``.
``weighs_more_than`` and ``weighs_less_than`` control the ordering
- of media types. Each value may be a string or a list of strings.
+ of media types. Each value may be a string or a list of strings. If
+ all options for ``weighs_more_than`` (or ``weighs_less_than``) cannot
+ be found, it is an error.
+
+ Earlier calls to ``add_accept_view_order`` are given higher priority
+ over later calls, assuming similar constraints but standard conflict
+ resolution mechanisms can be used to override constraints.
See :ref:`accept_content_negotiation` for more information.
@@ -1324,8 +1329,8 @@ class ViewsConfiguratorMixin(object):
self.registry.registerUtility(sorter, IAcceptOrder)
sorter.add(
value, value,
- after=weighs_more_than,
- before=weighs_less_than,
+ before=weighs_more_than,
+ after=weighs_less_than,
)
self.action(discriminator, register, introspectables=(intr,),
order=PHASE1_CONFIG) # must be registered before add_view
diff --git a/pyramid/tests/test_config/test_views.py b/pyramid/tests/test_config/test_views.py
index db15a39fb..730363cf9 100644
--- a/pyramid/tests/test_config/test_views.py
+++ b/pyramid/tests/test_config/test_views.py
@@ -2389,6 +2389,72 @@ class TestViewsConfigurationMixin(unittest.TestCase):
request.exception = Exception()
self.assertEqual(derived_view(None, request), 'OK')
+ def test_add_view_does_not_accept_iterable_accept(self):
+ from pyramid.exceptions import ConfigurationError
+ config = self._makeOne(autocommit=True)
+ self.assertRaises(
+ ConfigurationError, config.add_view, accept=['image/*', 'text/*'],
+ )
+
+ def test_default_accept_view_order(self):
+ from pyramid.interfaces import IAcceptOrder
+ config = self._makeOne(autocommit=True)
+ order = config.registry.getUtility(IAcceptOrder)
+ result = [v for _, v in order.sorted()]
+ self.assertEqual(result, [
+ 'text/html',
+ 'application/xhtml+xml',
+ 'application/xml',
+ 'text/xml',
+ 'text/plain',
+ ])
+
+ def test_add_accept_view_order_override(self):
+ from pyramid.interfaces import IAcceptOrder
+ config = self._makeOne(autocommit=False)
+ config.add_accept_view_order(
+ 'text/html',
+ weighs_more_than='text/xml',
+ weighs_less_than='application/xml',
+ )
+ config.commit()
+ order = config.registry.getUtility(IAcceptOrder)
+ result = [v for _, v in order.sorted()]
+ self.assertEqual(result, [
+ 'application/xhtml+xml',
+ 'application/xml',
+ 'text/html',
+ 'text/xml',
+ 'text/plain',
+ ])
+
+ def test_add_accept_view_order_throws_on_wildcard(self):
+ from pyramid.exceptions import ConfigurationError
+ config = self._makeOne(autocommit=True)
+ self.assertRaises(
+ ConfigurationError, config.add_accept_view_order, '*/*',
+ )
+
+ def test_add_accept_view_order_throws_on_type_mismatch(self):
+ config = self._makeOne(autocommit=True)
+ self.assertRaises(
+ ConfigurationError, config.add_accept_view_order,
+ 'text/*', weighs_more_than='text/html',
+ )
+ self.assertRaises(
+ ConfigurationError, config.add_accept_view_order,
+ 'text/html', weighs_less_than='application/*',
+ )
+ self.assertRaises(
+ ConfigurationError, config.add_accept_view_order,
+ 'text/html', weighs_more_than='text/html;charset=utf8',
+ )
+ self.assertRaises(
+ ConfigurationError, config.add_accept_view_order,
+ 'text/html;charset=utf8',
+ weighs_more_than='text/plain;charset=utf8',
+ )
+
class Test_runtime_exc_view(unittest.TestCase):
def _makeOne(self, view1, view2):
from pyramid.config.views import runtime_exc_view