diff options
Diffstat (limited to 'pecan/tests/test_base.py')
-rw-r--r-- | pecan/tests/test_base.py | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py index 0b5bc88..13d1c94 100644 --- a/pecan/tests/test_base.py +++ b/pecan/tests/test_base.py @@ -908,6 +908,54 @@ class TestFileTypeExtensions(unittest.TestCase): assert r.status_int == 404 +class TestContentTypeByAcceptHeaders(unittest.TestCase): + + @property + def app_(self): + """ + Test that content type is set appropriately based on Accept headers. + """ + class RootController(object): + + @expose(content_type='text/html') + @expose(content_type='application/json') + def index(self, *args): + return 'Foo' + + return TestApp(Pecan(RootController())) + + def test_quality(self): + r = self.app_.get('/', headers={ + 'Accept': 'text/html,application/json;q=0.9,*/*;q=0.8' + }) + assert r.status_int == 200 + assert r.content_type == 'text/html' + + r = self.app_.get('/', headers={ + 'Accept': 'application/json,text/html;q=0.9,*/*;q=0.8' + }) + assert r.status_int == 200 + assert r.content_type == 'application/json' + + def test_file_extension_has_higher_precedence(self): + r = self.app_.get('/index.html', headers={ + 'Accept': 'application/json,text/html;q=0.9,*/*;q=0.8' + }) + assert r.status_int == 200 + assert r.content_type == 'text/html' + + def test_not_acceptable(self): + r = self.app_.get('/', headers={ + 'Accept': 'application/xml', + }, status=406) + assert r.status_int == 406 + + def test_accept_header_missing(self): + r = self.app_.get('/') + assert r.status_int == 200 + assert r.content_type == 'text/html' + + class TestCanonicalRouting(unittest.TestCase): @property @@ -1050,9 +1098,9 @@ class TestNonCanonical(unittest.TestCase): @expose() def index(self): request.testing = True - assert request.testing == True + assert request.testing is True del request.testing - assert hasattr(request, 'testing') == False + assert hasattr(request, 'testing') is False return '/' app = TestApp(make_app(RootController(), debug=True)) @@ -1295,8 +1343,7 @@ class TestEngines(unittest.TestCase): return render('mako.html', dict(name=name)) app = TestApp( - Pecan(RootController(), - template_path=self.template_path) + Pecan(RootController(), template_path=self.template_path) ) r = app.get('/') assert r.status_int == 200 |