Esempio n. 1
0
 def test_media_router_serve_only(self):
     router = MediaRouter('/', serve_only=('json', 'png'))
     self.assertIsInstance(router._serve_only, set)
     self.assertEqual(len(router._serve_only), 2)
     self.assertEqual(router(test_wsgi_environ('/foo')), None)
     self.assertEqual(router(test_wsgi_environ('/foo/bla')), None)
     self.assertRaises(Http404, router, test_wsgi_environ('/foo/bla.png'))
Esempio n. 2
0
 def test_parse_form_data(self):
     environ = wsgi.test_wsgi_environ()
     self.assertRaises(MultipartError, parse_form_data, environ,
                       strict=True)
     environ = wsgi.test_wsgi_environ(method='POST')
     self.assertRaises(MultipartError, parse_form_data, environ,
                       strict=True)
Esempio n. 3
0
 def test_parse_form_data(self):
     environ = wsgi.test_wsgi_environ()
     self.assertRaises(MultipartError, parse_form_data, environ,
                       strict=True)
     environ = wsgi.test_wsgi_environ(method='POST')
     self.assertRaises(MultipartError, parse_form_data, environ,
                       strict=True)
Esempio n. 4
0
 def test_media_router_serve_only(self):
     router = MediaRouter('/', serve_only=('json', 'png'))
     self.assertIsInstance(router._serve_only, set)
     self.assertEqual(len(router._serve_only), 2)
     self.assertEqual(router(test_wsgi_environ('/foo')), None)
     self.assertEqual(router(test_wsgi_environ('/foo/bla')), None)
     self.assertRaises(Http404, router, test_wsgi_environ('/foo/bla.png'))
Esempio n. 5
0
    def wsgi_request(self,
                     environ=None,
                     loop=None,
                     path=None,
                     app_handler=None,
                     urlargs=None,
                     **kw):
        '''Create a :class:`.WsgiRequest` from a wsgi ``environ`` and set the
        ``app`` attribute in the cache.
        Additional keyed-valued parameters can be inserted.
        '''
        if not environ:
            # No WSGI environment, build a test one
            environ = test_wsgi_environ(path=path, loop=loop, **kw)
        request = wsgi_request(environ,
                               app_handler=app_handler,
                               urlargs=urlargs)
        environ['error.handler'] = self.config['ERROR_HANDLER']
        environ['default.content_type'] = self.config['DEFAULT_CONTENT_TYPE']
        # Check if pulsar is serving the application
        if 'pulsar.cfg' not in environ:
            if not self.cfg:
                self.cfg = pulsar.Config(debug=self.debug)
            environ['pulsar.cfg'] = self.cfg

        request.cache.app = self
        return request
Esempio n. 6
0
 def test_handle_wsgi_error(self):
     handler = lambda request, exc: 'exception: %s' % exc
     environ = wsgi.test_wsgi_environ(extra={'error.handler': handler})
     try:
         raise ValueError('just a test')
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(environ, exc)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content, (b'exception: just a test', ))
Esempio n. 7
0
 def test_handle_wsgi_error(self):
     environ = wsgi.test_wsgi_environ(
         extra={'error.handler': lambda request, failure: 'bla'})
     try:
         raise ValueError('just a test')
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(environ, exc)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content, (b'bla',))
Esempio n. 8
0
 def test_handle_wsgi_error(self):
     handle500 = lambda request, exc: 'exception: %s' % exc
     environ = wsgi.test_wsgi_environ(
         extra={'error.handlers': {500: handle500}})
     try:
         raise ValueError('just a test')
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(environ, exc)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content, (b'exception: just a test',))
Esempio n. 9
0
 def test_handle_wsgi_error(self):
     environ = wsgi.test_wsgi_environ(extra=
                         {'error.handler': lambda request, failure: 'bla'})
     try:
         raise ValueError('just a test')
     except ValueError:
         failure = pulsar.Failure(sys.exc_info())
         failure.mute()
         response = wsgi.handle_wsgi_error(environ, failure)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content, (b'bla',))
Esempio n. 10
0
 def test_handle_wsgi_error_debug(self):
     cfg = self.cfg.copy()
     cfg.set('debug', True)
     environ = wsgi.test_wsgi_environ(extra={'pulsar.cfg': cfg})
     try:
         raise ValueError('just a test for debug wsgi error handler')
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(environ, exc)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content_type, None)
     self.assertEqual(len(response.content), 1)
Esempio n. 11
0
    def test_handle_wsgi_error(self):
        def handler(request, exc):
            return "exception: %s" % exc

        environ = wsgi.test_wsgi_environ(extra={"error.handler": handler})
        try:
            raise ValueError("just a test")
        except ValueError as exc:
            response = wsgi.handle_wsgi_error(environ, exc)
        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.content, (b"exception: just a test",))
Esempio n. 12
0
 def test_handle_wsgi_error_debug(self):
     cfg = self.cfg.copy()
     cfg.set('debug', True)
     environ = wsgi.test_wsgi_environ(extra={'pulsar.cfg': cfg})
     try:
         raise ValueError('just a test for debug wsgi error handler')
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(environ, exc)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content_type, None)
     self.assertEqual(len(response.content), 1)
Esempio n. 13
0
    def test_response_wrapper(self):
        def response_wrapper(callable, response):
            raise pulsar.PermissionDenied('Test Response Wrapper')

        router = HttpBin('/', response_wrapper=response_wrapper)

        environ = test_wsgi_environ()
        try:
            router(environ, None)
        except pulsar.PermissionDenied as exc:
            self.assertEqual(str(exc), 'Test Response Wrapper')
        else:
            raise RuntimeError

        environ = test_wsgi_environ('/get')
        try:
            router(environ, None)
        except pulsar.PermissionDenied as exc:
            self.assertEqual(str(exc), 'Test Response Wrapper')
        else:
            raise RuntimeError
Esempio n. 14
0
    def test_response_wrapper(self):

        def response_wrapper(callable, response):
            raise pulsar.PermissionDenied('Test Response Wrapper')

        router = HttpBin('/', response_wrapper=response_wrapper)

        environ = test_wsgi_environ()
        try:
            router(environ, None)
        except pulsar.PermissionDenied as exc:
            self.assertEqual(str(exc), 'Test Response Wrapper')
        else:
            raise RuntimeError

        environ = test_wsgi_environ('/get')
        try:
            router(environ, None)
        except pulsar.PermissionDenied as exc:
            self.assertEqual(str(exc), 'Test Response Wrapper')
        else:
            raise RuntimeError
Esempio n. 15
0
 def test_handle_wsgi_error(self):
     environ = wsgi.test_wsgi_environ(
         extra={
             'error.handler': lambda request, failure: 'bla'
         })
     try:
         raise ValueError('just a test')
     except ValueError:
         failure = pulsar.Failure(sys.exc_info())
         failure.mute()
         response = wsgi.handle_wsgi_error(environ, failure)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content, (b'bla', ))
Esempio n. 16
0
 def wsgi_request(self, environ=None, **kw):
     '''Create a :class:`WsgiRequest` from a wsgi ``environ`` and set the
     ``app`` attribute in the cache.
     Additional keyed-valued parameters can be inserted.
     '''
     if not environ:
         # No WSGI environment, build a test one
         environ = test_wsgi_environ()
     if kw:
         environ.update(kw)
     request = WsgiRequest(environ)
     environ['error.handler'] = self.config['ERROR_HANDLER']
     request.cache.app = self
     return request
Esempio n. 17
0
 def test_handle_wsgi_error_debug(self):
     cfg = self.cfg.copy()
     cfg.set('debug', True)
     environ = wsgi.test_wsgi_environ(extra={'pulsar.cfg': cfg})
     try:
         raise ValueError('just a test for debug wsgi error handler')
     except ValueError:
         exc_info = sys.exc_info()
     failure = pulsar.Failure(exc_info)
     failure.mute()
     response = wsgi.handle_wsgi_error(environ, failure)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content_type, None)
     self.assertEqual(len(response.content), 1)
Esempio n. 18
0
 def test_handle_wsgi_error_debug_html(self):
     cfg = self.cfg.copy()
     cfg.set("debug", True)
     headers = [("Accept", "*/*")]
     environ = wsgi.test_wsgi_environ(extra={"pulsar.cfg": cfg}, headers=headers)
     try:
         raise ValueError("just a test for debug wsgi error handler")
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(environ, exc)
     self.assertEqual(response.status_code, 500)
     html = response.content[0]
     self.assertEqual(response.content_type, "text/html")
     self.assertTrue(html.startswith(b"<!DOCTYPE html>"))
     self.assertTrue(b"<title>500 Internal Server Error</title>" in html)
Esempio n. 19
0
 def wsgi_request(self, environ=None, **kw):
     '''Create a :class:`WsgiRequest` from a wsgi ``environ`` and set the
     ``app`` attribute in the cache.
     Additional keyed-valued parameters can be inserted.
     '''
     if not environ:
         # No WSGI environment, build a test one
         environ = test_wsgi_environ()
     if kw:
         environ.update(kw)
     request = WsgiRequest(environ)
     environ['error.handler'] = self.config['ERROR_HANDLER']
     request.cache.app = self
     return request
Esempio n. 20
0
 def test_handle_wsgi_error_debug(self):
     cfg = self.cfg.copy()
     cfg.set('debug', True)
     environ = wsgi.test_wsgi_environ(extra={'pulsar.cfg': cfg})
     try:
         raise ValueError('just a test for debug wsgi error handler')
     except ValueError:
         exc_info = sys.exc_info()
     failure = pulsar.Failure(exc_info)
     failure.mute()
     response = wsgi.handle_wsgi_error(environ, failure)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content_type, None)
     self.assertEqual(len(response.content), 1)
Esempio n. 21
0
 def test_handle_wsgi_error_debug_html(self):
     cfg = self.cfg.copy()
     cfg.set('debug', True)
     headers = [('Accept', '*/*')]
     environ = wsgi.test_wsgi_environ(extra={'pulsar.cfg': cfg},
                                      headers=headers)
     try:
         raise ValueError('just a test for debug wsgi error handler')
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(environ, exc)
     self.assertEqual(response.status_code, 500)
     html = response.content[0]
     self.assertEqual(response.content_type, 'text/html')
     self.assertTrue(html.startswith(b'<!DOCTYPE html>'))
     self.assertTrue(b'<title>500 Internal Server Error</title>' in html)
Esempio n. 22
0
 def test_handle_wsgi_error_debug_html(self):
     cfg = self.cfg.copy()
     cfg.set('debug', True)
     headers = [('Accept', '*/*')]
     environ = wsgi.test_wsgi_environ(extra={'pulsar.cfg': cfg},
                                      headers=headers)
     try:
         raise ValueError('just a test for debug wsgi error handler')
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(environ, exc)
     self.assertEqual(response.status_code, 500)
     html = response.content[0]
     self.assertEqual(response.content_type, 'text/html')
     self.assertTrue(html.startswith(b'<!DOCTYPE html>'))
     self.assertTrue(b'<title>500 Internal Server Error</title>' in html)
Esempio n. 23
0
    def wsgi_request(self, environ=None, loop=None, path=None,
                     app_handler=None, urlargs=None, **kw):
        '''Create a :class:`.WsgiRequest` from a wsgi ``environ`` and set the
        ``app`` attribute in the cache.
        Additional keyed-valued parameters can be inserted.
        '''
        if not environ:
            # No WSGI environment, build a test one
            environ = test_wsgi_environ(path=path, loop=loop, **kw)
        request = wsgi_request(environ, app_handler=app_handler,
                               urlargs=urlargs)
        environ['error.handler'] = self.config['ERROR_HANDLER']
        environ['default.content_type'] = self.config['DEFAULT_CONTENT_TYPE']
        # Check if pulsar is serving the application
        if 'pulsar.cfg' not in environ:
            if not self.cfg:
                self.cfg = pulsar.Config(debug=self.debug)
            environ['pulsar.cfg'] = self.cfg

        request.cache.app = self
        return request
Esempio n. 24
0
 def request(self, **kwargs):
     environ = wsgi.test_wsgi_environ(**kwargs)
     return wsgi.WsgiRequest(environ)
Esempio n. 25
0
 def request(self, **kwargs):
     environ = wsgi.test_wsgi_environ(**kwargs)
     return wsgi.WsgiRequest(environ)