| | |
| | | ) |
| | | |
| | | from zope.interface import Interface |
| | | from webtest import TestApp |
| | | |
| | | # 5 years from now (more or less) |
| | | fiveyrsfuture = datetime.datetime.utcnow() + datetime.timedelta(5*365) |
| | |
| | | package=self.package) |
| | | config.include(self.package) |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | self.config = config |
| | | |
| | |
| | | config = self._makeConfig() |
| | | config.include(self.package) |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | res = self.testapp.get('/') |
| | | self.assertTrue(b'a view' in res.body) |
| | |
| | | return Response('this view') |
| | | config.add_view(thisview) |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | res = self.testapp.get('/') |
| | | self.assertTrue(b'this view' in res.body) |
| | |
| | | return Response('this view') |
| | | config.add_view(thisview, route_name='aroute') |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | res = self.testapp.get('/route') |
| | | self.assertTrue(b'this view' in res.body) |
| | |
| | | config = self._makeConfig() |
| | | config.include(self.package) |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | res = self.testapp.get('/protected', status=403) |
| | | self.assertTrue(b'403 Forbidden' in res.body) |
| | |
| | | config.set_authorization_policy(DummySecurityPolicy('fred')) |
| | | config.set_authentication_policy(DummySecurityPolicy(permissive=True)) |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | res = self.testapp.get('/protected', status=200) |
| | | self.assertTrue('protected view' in res) |
| | |
| | | from pyramid.tests.pkgs.includeapp1.root import configure |
| | | configure(config) |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | self.config = config |
| | | |
| | |
| | | from pyramid.tests.test_config.pkgs.selfscan import main |
| | | config = main() |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | self.config = config |
| | | |
| | |
| | | from pyramid.tests.pkgs.wsgiapp2app import main |
| | | config = main() |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | self.config = config |
| | | |
| | |
| | | from pyramid.tests.pkgs.subrequestapp import main |
| | | config = main() |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | self.config = config |
| | | |
| | |
| | | def test_rescan(self): |
| | | self.config.scan('pyramid.tests.pkgs.rendererscanapp') |
| | | app = self.config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | testapp = TestApp(app) |
| | | res = testapp.get('/one', status=200) |
| | | self.assertTrue(b'One!' in res.body) |
| | |
| | | return config |
| | | |
| | | def _makeTestApp(self, config): |
| | | from webtest import TestApp |
| | | app = config.make_wsgi_app() |
| | | return TestApp(app) |
| | | |
| | |
| | | |
| | | |
| | | class AcceptContentTypeTest(unittest.TestCase): |
| | | def setUp(self): |
| | | def _makeConfig(self): |
| | | def hello_view(request): |
| | | return {'message': 'Hello!'} |
| | | from pyramid.config import Configurator |
| | | config = Configurator() |
| | | config.add_route('hello', '/hello') |
| | | config.add_view(hello_view, route_name='hello', accept='text/plain', renderer='string') |
| | | config.add_view(hello_view, route_name='hello', accept='application/json', renderer='json') |
| | | config.add_view(hello_view, route_name='hello', |
| | | accept='text/plain', renderer='string') |
| | | config.add_view(hello_view, route_name='hello', |
| | | accept='application/json', renderer='json') |
| | | def hello_fallback_view(request): |
| | | request.response.content_type = 'text/x-fallback' |
| | | return 'hello fallback' |
| | | config.add_view(hello_fallback_view, route_name='hello', |
| | | renderer='string') |
| | | return config |
| | | |
| | | def _makeTestApp(self, config): |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | return TestApp(app) |
| | | |
| | | def tearDown(self): |
| | | import pyramid.config |
| | | pyramid.config.global_registries.empty() |
| | | pyramid.config.global_registries.empty() |
| | | |
| | | def test_ordering(self): |
| | | res = self.testapp.get('/hello', headers={'Accept': 'application/json; q=1.0, text/plain; q=0.9'}, status=200) |
| | | def test_client_side_ordering(self): |
| | | config = self._makeConfig() |
| | | app = self._makeTestApp(config) |
| | | res = app.get('/hello', headers={ |
| | | 'Accept': 'application/json; q=1.0, text/plain; q=0.9', |
| | | }, status=200) |
| | | self.assertEqual(res.content_type, 'application/json') |
| | | res = self.testapp.get('/hello', headers={'Accept': 'text/plain; q=0.9, application/json; q=1.0'}, status=200) |
| | | res = app.get('/hello', headers={ |
| | | 'Accept': 'text/plain; q=0.9, application/json; q=1.0', |
| | | }, status=200) |
| | | self.assertEqual(res.content_type, 'application/json') |
| | | |
| | | def test_wildcards(self): |
| | | res = self.testapp.get('/hello', headers={'Accept': 'application/*'}, status=200) |
| | | res = app.get('/hello', headers={'Accept': 'application/*'}, status=200) |
| | | self.assertEqual(res.content_type, 'application/json') |
| | | res = self.testapp.get('/hello', headers={'Accept': 'text/*'}, status=200) |
| | | res = app.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') |
| | | res = app.get('/hello', headers={'Accept': 'something/else'}, status=200) |
| | | self.assertEqual(res.content_type, 'text/x-fallback') |
| | | |
| | | def test_no_accept(self): |
| | | res = self.testapp.get('/hello', status=200) |
| | | def test_default_server_side_ordering(self): |
| | | config = self._makeConfig() |
| | | app = self._makeTestApp(config) |
| | | res = app.get('/hello', headers={ |
| | | 'Accept': 'application/json, text/plain', |
| | | }, status=200) |
| | | self.assertEqual(res.content_type, 'text/plain') |
| | | res = app.get('/hello', headers={ |
| | | 'Accept': 'text/plain, application/json', |
| | | }, status=200) |
| | | self.assertEqual(res.content_type, 'text/plain') |
| | | res = app.get('/hello', headers={'Accept': '*/*'}, status=200) |
| | | self.assertEqual(res.content_type, 'text/plain') |
| | | res = app.get('/hello', status=200) |
| | | self.assertEqual(res.content_type, 'text/plain') |
| | | res = app.get('/hello', headers={'Accept': 'invalid'}, status=200) |
| | | self.assertEqual(res.content_type, 'text/plain') |
| | | res = app.get('/hello', headers={'Accept': 'something/else'}, status=200) |
| | | self.assertEqual(res.content_type, 'text/x-fallback') |
| | | |
| | | def test_invalid_accept(self): |
| | | res = self.testapp.get('/hello', headers={'Accept': 'foo'}, status=200) |
| | | self.assertEqual(res.content_type, 'text/plain') |
| | | def test_custom_server_side_ordering(self): |
| | | config = self._makeConfig() |
| | | config.add_accept_view_order( |
| | | 'application/json', weighs_more_than='text/plain') |
| | | app = self._makeTestApp(config) |
| | | res = app.get('/hello', headers={ |
| | | 'Accept': 'application/json, text/plain', |
| | | }, status=200) |
| | | self.assertEqual(res.content_type, 'application/json') |
| | | res = app.get('/hello', headers={ |
| | | 'Accept': 'text/plain, application/json', |
| | | }, status=200) |
| | | self.assertEqual(res.content_type, 'application/json') |
| | | res = app.get('/hello', headers={'Accept': '*/*'}, status=200) |
| | | self.assertEqual(res.content_type, 'application/json') |
| | | res = app.get('/hello', status=200) |
| | | self.assertEqual(res.content_type, 'application/json') |
| | | res = app.get('/hello', headers={'Accept': 'invalid'}, status=200) |
| | | self.assertEqual(res.content_type, 'application/json') |
| | | res = app.get('/hello', headers={'Accept': 'something/else'}, status=200) |
| | | self.assertEqual(res.content_type, 'text/x-fallback') |
| | | |
| | | class AddViewAcceptArgMediaRangeAllTest(unittest.TestCase): |
| | | def setUp(self): |
| | |
| | | view, route_name='root', accept='*/*', renderer='string', |
| | | ) |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | |
| | | def tearDown(self): |
| | |
| | | view, route_name='root', accept='text/*', renderer='string', |
| | | ) |
| | | app = config.make_wsgi_app() |
| | | from webtest import TestApp |
| | | self.testapp = TestApp(app) |
| | | |
| | | def tearDown(self): |
| | |
| | | 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): |
| | |
| | | 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): |