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'))
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)
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
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', ))
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',))
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',))
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',))
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)
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",))
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
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', ))
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
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)
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)
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)
def request(self, **kwargs): environ = wsgi.test_wsgi_environ(**kwargs) return wsgi.WsgiRequest(environ)