summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pyramid/config/views.py1
-rw-r--r--pyramid/tests/test_config/test_views.py2
-rw-r--r--pyramid/tests/test_integration.py227
3 files changed, 230 insertions, 0 deletions
diff --git a/pyramid/config/views.py b/pyramid/config/views.py
index 3e4dbea0e..15ceadcbc 100644
--- a/pyramid/config/views.py
+++ b/pyramid/config/views.py
@@ -1235,6 +1235,7 @@ class ViewsConfiguratorMixin(object):
'application/xml',
'text/xml',
'text/plain',
+ 'application/json',
):
self.add_accept_view_order(accept)
diff --git a/pyramid/tests/test_config/test_views.py b/pyramid/tests/test_config/test_views.py
index 730363cf9..abc10d3f5 100644
--- a/pyramid/tests/test_config/test_views.py
+++ b/pyramid/tests/test_config/test_views.py
@@ -2407,6 +2407,7 @@ class TestViewsConfigurationMixin(unittest.TestCase):
'application/xml',
'text/xml',
'text/plain',
+ 'application/json',
])
def test_add_accept_view_order_override(self):
@@ -2426,6 +2427,7 @@ class TestViewsConfigurationMixin(unittest.TestCase):
'text/html',
'text/xml',
'text/plain',
+ 'application/json',
])
def test_add_accept_view_order_throws_on_wildcard(self):
diff --git a/pyramid/tests/test_integration.py b/pyramid/tests/test_integration.py
index c99e89f59..01708eb1b 100644
--- a/pyramid/tests/test_integration.py
+++ b/pyramid/tests/test_integration.py
@@ -712,7 +712,234 @@ class AcceptContentTypeTest(unittest.TestCase):
self.assertEqual(res.content_type, 'application/json')
res = self.testapp.get('/hello', headers={'Accept': 'text/*'}, status=200)
self.assertEqual(res.content_type, 'text/plain')
+ res = self.testapp.get('/hello', headers={'Accept': '*/*'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_no_accept(self):
+ res = self.testapp.get('/hello', status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_invalid_accept(self):
+ res = self.testapp.get('/hello', headers={'Accept': 'foo'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+class AddViewAcceptArgMediaRangeAllTest(unittest.TestCase):
+ def setUp(self):
+ def view(request):
+ return 'text/plain'
+ from pyramid.config import Configurator
+ config = Configurator()
+ config.add_route('root', '/')
+ config.add_view(
+ view, route_name='root', accept='*/*', renderer='string',
+ )
+ app = config.make_wsgi_app()
+ from webtest import TestApp
+ self.testapp = TestApp(app)
+
+ def tearDown(self):
+ import pyramid.config
+ pyramid.config.global_registries.empty()
+
+ def test_no_header(self):
+ res = self.testapp.get('/', headers={}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_all(self):
+ res = self.testapp.get('/', headers={'Accept': '*/*'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_all_subtypes_of_type(self):
+ res = self.testapp.get('/', headers={'Accept': 'text/*'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_specific_media_type(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/plain'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_ruled_out_by_specific_media_type_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/plain;q=0, */*'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+ def test_header_ruled_out_by_type_range_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/*;q=0, text/html'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_ruled_out_by_all_range_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': '*/*;q=0, text/html'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+class AddViewAcceptArgMediaRangeAllSubtypesOfTypeTest(unittest.TestCase):
+ def setUp(self):
+ def view(request):
+ return 'text/plain'
+ from pyramid.config import Configurator
+ config = Configurator()
+ config.add_route('root', '/')
+ config.add_view(
+ view, route_name='root', accept='text/*', renderer='string',
+ )
+ app = config.make_wsgi_app()
+ from webtest import TestApp
+ self.testapp = TestApp(app)
+
+ def tearDown(self):
+ import pyramid.config
+ pyramid.config.global_registries.empty()
+
+ def test_no_header(self):
+ res = self.testapp.get('/', headers={}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_all(self):
+ res = self.testapp.get('/', headers={'Accept': '*/*'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_all_subtypes_of_type(self):
+ res = self.testapp.get('/', headers={'Accept': 'text/*'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_specific_media_type(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/plain'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_none_acceptable(self):
+ self.testapp.get('/', headers={'Accept': 'application/*'}, status=404)
+
+ def test_header_ruled_out_by_specific_media_type_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/plain;q=0, */*'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_ruled_out_by_type_range_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/*;q=0, text/html'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_ruled_out_by_all_range_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': '*/*;q=0, text/html'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+class AddRouteAcceptArgMediaRangeAllTest(unittest.TestCase):
+ def setUp(self):
+ def view(request):
+ return 'text/plain'
+ from pyramid.config import Configurator
+ config = Configurator()
+ config.add_route('root', '/', accept='*/*')
+ config.add_view(view, route_name='root', renderer='string')
+ app = config.make_wsgi_app()
+ from webtest import TestApp
+ self.testapp = TestApp(app)
+
+ def tearDown(self):
+ import pyramid.config
+ pyramid.config.global_registries.empty()
+
+ def test_no_header(self):
+ res = self.testapp.get('/', headers={}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_all(self):
+ res = self.testapp.get('/', headers={'Accept': '*/*'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_all_subtypes_of_type(self):
+ res = self.testapp.get('/', headers={'Accept': 'text/*'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_specific_media_type(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/plain'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_ruled_out_by_specific_media_type_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/plain;q=0, */*'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_ruled_out_by_type_range_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/*;q=0, text/html'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_ruled_out_by_all_range_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': '*/*;q=0, text/html'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+class AddRouteAcceptArgMediaRangeAllSubtypesOfTypeTest(unittest.TestCase):
+ def setUp(self):
+ def view(request):
+ return 'text/plain'
+ from pyramid.config import Configurator
+ config = Configurator()
+ config.add_route('root', '/', accept='text/*')
+ config.add_view(view, route_name='root', renderer='string')
+ app = config.make_wsgi_app()
+ from webtest import TestApp
+ self.testapp = TestApp(app)
+
+ def tearDown(self):
+ import pyramid.config
+ pyramid.config.global_registries.empty()
+
+ def test_no_header(self):
+ res = self.testapp.get('/', headers={}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_all(self):
+ res = self.testapp.get('/', headers={'Accept': '*/*'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_all_subtypes_of_type(self):
+ res = self.testapp.get('/', headers={'Accept': 'text/*'}, status=200)
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_specific_media_type(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/plain'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_none_acceptable(self):
+ self.testapp.get('/', headers={'Accept': 'application/*'}, status=404)
+
+ def test_header_ruled_out_by_specific_media_type_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/plain;q=0, */*'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_ruled_out_by_type_range_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': 'text/*;q=0, text/html'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
+
+ def test_header_ruled_out_by_all_range_q0(self):
+ res = self.testapp.get(
+ '/', headers={'Accept': '*/*;q=0, text/html'}, status=200,
+ )
+ self.assertEqual(res.content_type, 'text/plain')
class DummyContext(object):
pass