Beispiel #1
0
    def _call(self, pool, environ, start_response):
        if pool:
            wsgi_input = environ['wsgi.input']
            if wsgi_input and not isinstance(wsgi_input, GreenStream):
                environ['wsgi.input'] = GreenStream(wsgi_input)

        request = wsgi_request(environ)
        path = request.path
        try:
            self.app.on_request(data=request)
            hnd = self.router.resolve(path, request.method)
            if hnd:
                request.cache.set('app_handler', hnd.router)
                request.cache.set('urlargs', hnd.urlargs)
                response = self.wait(hnd.handler(request))
            else:
                raise Http404

        except Exception as exc:
            response = handle_wsgi_error(environ, exc)

        try:
            self.app.fire_event('on_response', data=(request, response))
        except Exception as exc:
            response = handle_wsgi_error(environ, exc)

        response.start(environ, start_response)
        return response
Beispiel #2
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', ))
Beispiel #3
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',))
Beispiel #4
0
 def test_handle_wsgi_error(self):
     environ = {'wsgi_error_handler': lambda : 'bla'}
     try:
         raise ValueError('just a test')
     except:
         response = wsgi.handle_wsgi_error(environ, content_type='text/html')
     self.assertEqual(response.content_type, 'text/html')
     self.assertEqual(response.status_code, 500)
         
Beispiel #5
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',))
Beispiel #6
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)
Beispiel #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:
         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',))
Beispiel #8
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)
Beispiel #9
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",))
Beispiel #10
0
 async def test_handle_wsgi_error_debug(self):
     cfg = self.cfg.copy()
     cfg.set('debug', True)
     request = await test_wsgi_request()
     request.environ['pulsar.cfg'] = cfg
     try:
         raise ValueError('just a test for debug wsgi error handler')
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(request.environ, exc)
     self.assertEqual(response.status_code, 500)
     self.assertEqual(response.content_type, 'text/plain')
     self.assertEqual(len(response.content), 1)
Beispiel #11
0
    async def test_handle_wsgi_error(self):

        def handler(request, exc):
            return 'exception: %s' % exc

        request = await test_wsgi_request()
        request.environ['error.handler'] = handler
        try:
            raise ValueError('just a test')
        except ValueError as exc:
            response = wsgi.handle_wsgi_error(request.environ, exc)
        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.content, (b'exception: just a test',))
Beispiel #12
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', ))
Beispiel #13
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)
Beispiel #14
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)
Beispiel #15
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)
Beispiel #16
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)
Beispiel #17
0
 async def test_handle_wsgi_error_debug_html(self):
     cfg = self.cfg.copy()
     cfg.set('debug', True)
     request = await test_wsgi_request()
     request.environ['pulsar.cfg'] = cfg
     request.environ['default.content_type'] = 'text/html'
     try:
         raise ValueError('just a test for debug wsgi error handler')
     except ValueError as exc:
         response = wsgi.handle_wsgi_error(request.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)
Beispiel #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)
Beispiel #19
0
    def _call(self, environ, start_response):
        if environ['wsgi.input']:
            environ['wsgi.input'] = GreenStream(environ['wsgi.input'])
        response = None
        try:
            for middleware in self.middleware:
                response = wait(middleware(environ, start_response))
                if response is not None:
                    break
            if response is None:
                raise Http404

        except Exception as exc:
            response = wait(handle_wsgi_error(environ, exc))

        if isinstance(response, WsgiResponse) and not response.started:
            for middleware in self.response_middleware:
                response = wait(middleware(environ, response)) or response
            response.start(start_response)
        return response
Beispiel #20
0
    def _call(self, environ, start_response):
        if environ['wsgi.input']:
            environ['wsgi.input'] = GreenStream(environ['wsgi.input'])
        response = None
        try:
            for middleware in self.middleware:
                response = wait(middleware(environ, start_response))
                if response is not None:
                    break
            if response is None:
                raise Http404

        except Exception as exc:
            response = wait(handle_wsgi_error(environ, exc))

        if isinstance(response, WsgiResponse) and not response.started:
            for middleware in self.response_middleware:
                response = wait(middleware(environ, response)) or response
            response.start(start_response)
        return response
Beispiel #21
0
    def _call(self, environ, start_response):
        wsgi_input = environ['wsgi.input']
        if wsgi_input and not isinstance(wsgi_input, GreenStream):
            environ['wsgi.input'] = GreenStream(wsgi_input)
        response = None
        try:
            for middleware in self.middleware:
                response = wait(middleware(environ, start_response))
                if response is not None:
                    break
            if response is None:
                raise Http404

        except Exception as exc:
            response = wait(handle_wsgi_error(environ, exc))

        if not getattr(response, '__wsgi_started__', True):
            for middleware in self.response_middleware:
                response = wait(middleware(environ, response)) or response
            response.start(environ, start_response)
        return response
Beispiel #22
0
    def _call(self, environ, start_response):
        wsgi_input = environ['wsgi.input']
        if wsgi_input and not isinstance(wsgi_input, GreenStream):
            environ['wsgi.input'] = GreenStream(wsgi_input)
        response = None
        try:
            for middleware in self.middleware:
                response = wait(middleware(environ, start_response))
                if response is not None:
                    break
            if response is None:
                raise Http404

        except Exception as exc:
            response = wait(handle_wsgi_error(environ, exc))

        if not getattr(response, '__wsgi_started__', True):
            for middleware in self.response_middleware:
                response = wait(middleware(environ, response)) or response
            response.start(environ, start_response)
        return response