def test_WSGIHTTPException_wsgi_response(): def start_response(status, headers, exc_info=None): pass environ = {"wsgi.url_scheme": "HTTP", "SERVER_NAME": "localhost", "SERVER_PORT": "80", "REQUEST_METHOD": "HEAD"} excep = WSGIHTTPException() assert_equal(excep.wsgi_response(environ, start_response), [])
def test_WSGIHTTPException_call_w_body(): def start_response(status, headers, exc_info=None): pass environ = {"wsgi.url_scheme": "HTTP", "SERVER_NAME": "localhost", "SERVER_PORT": "80", "REQUEST_METHOD": "PUT"} excep = WSGIHTTPException() excep.body = "test" assert_equal(excep(environ, start_response), ["test"])
def test_WSGIHTTPException_wsgi_response(): def start_response(status, headers, exc_info=None): pass environ = { 'wsgi.url_scheme': 'HTTP', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '80', 'REQUEST_METHOD': 'HEAD' } excep = WSGIHTTPException() assert_equal( excep.wsgi_response(environ,start_response), [] )
def test_WSGIHTTPException_call_w_body(): def start_response(status, headers, exc_info=None): pass environ = { 'wsgi.url_scheme': 'HTTP', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '80', 'REQUEST_METHOD': 'PUT' } excep = WSGIHTTPException() excep.body = b'test' assert_equal( excep(environ,start_response), [b'test'] )
def test_WSGIHTTPException_generate_response(): def start_response(status, headers, exc_info=None): pass environ = { 'wsgi.url_scheme': 'HTTP', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '80', 'REQUEST_METHOD': 'PUT', 'HTTP_ACCEPT': 'text/html' } excep = WSGIHTTPException() assert_equal(excep(environ, start_response), [ '<html>\n' ' <head>\n' ' <title>None None</title>\n' ' </head>\n' ' <body>\n' ' <h1>None None</h1>\n' ' <br /><br />\n' '\n' '\n\n' ' </body>\n' '</html>' ])
def test_WSGIHTTPException_generate_response_w_unencodable_object_html(): from webob.util import html_escape def start_response(status, headers, exc_info=None): pass class Unencodable(object): def __str__(self): raise u'\u00fe'.encode('ascii') environ = { 'wsgi.url_scheme': 'HTTP', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '80', 'REQUEST_METHOD': 'PUT', 'HTTP_ACCEPT': 'text/html', 'BAD': Unencodable() } body_template = "$BAD" excep = WSGIHTTPException(body_template=body_template) assert_equal( excep(environ,start_response), [ b'<html>\n' b' <head>\n' b' <title>None None</title>\n' b' </head>\n' b' <body>\n' b' <h1>None None</h1>\n' + \ b' ' + html_escape(object.__repr__(environ['BAD'])).encode('ascii') + b'\n' + \ b' </body>\n' b'</html>' ] )
def test_WSGIHTTPException_w_body_template(): from string import Template TEMPLATE = '$foo: $bar' exc = WSGIHTTPException(body_template = TEMPLATE) assert_equal(exc.body_template, TEMPLATE) ok_(isinstance(exc.body_template_obj, Template)) eq_(exc.body_template_obj.substitute({'foo': 'FOO', 'bar': 'BAR'}), 'FOO: BAR')
def test_WSGIHTTPException___str__(): exc1 = WSGIHTTPException(detail='Detail') eq_(str(exc1), 'Detail') class Explain(WSGIHTTPException): explanation = 'Explanation' eq_(str(Explain()), 'Explanation')
def test_WSGIHTTPException_exception_no_newstyle(): def start_response(status, headers, exc_info=None): pass environ = { 'wsgi.url_scheme': 'HTTP', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '80', 'REQUEST_METHOD': 'HEAD' } excep = WSGIHTTPException() from webob import exc exc.newstyle_exceptions = False assert_equal( excep(environ,start_response), [] )
def before(self, state): """ Executed before a controller gets called. When an error condition is detected (one of the callables raises a ``SystemCheckError``) it sets the response status to 500 and returns a JSON response with the appropriate reason. """ for check in [ ansible_exists, rabbitmq_is_running, celery_has_workers, database_connection ]: try: check() except SystemCheckError as system_error: message = render('json', {'message': system_error.message}) raise WSGIHTTPException(content_type='application/json', body=message)
def test_WSGIHTTPException_generate_response_w_undecodable_object_plain(): def start_response(status, headers, exc_info=None): pass class Undecodable(object): def __str__(self): return b'\xfe'.decode('ascii') environ = { 'wsgi.url_scheme': 'HTTP', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '80', 'REQUEST_METHOD': 'PUT', 'HTTP_ACCEPT': 'text/plain', 'BAD': Undecodable() } body_template = "$BAD" excep = WSGIHTTPException(body_template=body_template) assert_equal(excep(environ, start_response), [ b'None None\n\n' + object.__repr__(environ['BAD']).replace( '<', '[').replace('>', ']').encode('ascii') ])
def test_WSGIHTTPException_headers(): exc = WSGIHTTPException(headers=[('Set-Cookie', 'a=1'), ('Set-Cookie', 'a=2')]) mixed = exc.headers.mixed() assert mixed['set-cookie'] == ['a=1', 'a=2']
def __init__(self, name, details=None): global _error_lookup self.title = name self.explanation = _error_lookup[name][0] self.code = _error_lookup[name][1] WSGIHTTPException.__init__(self, detail=details)