<s> import mock <EOL> from oslo_serialization import jsonutils <EOL> import webob <EOL> import webob . dec <EOL> import webob . exc <EOL> import nova . api . openstack <EOL> from nova . api . openstack import wsgi <EOL> from nova import exception <EOL> from nova import test <EOL> class TestFaultWrapper ( test . NoDBTestCase ) : <EOL> """<STR_LIT>""" <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_safe_exception_translated ( self , mock_languages , mock_translate ) : <EOL> def fake_translate ( value , locale ) : <EOL> return "<STR_LIT>" <EOL> mock_translate . side_effect = fake_translate <EOL> safe_exception = exception . NotFound ( '<STR_LIT>' ) <EOL> safe_exception . safe = True <EOL> safe_exception . code = <NUM_LIT> <EOL> req = webob . Request . blank ( '<STR_LIT:/>' ) <EOL> def raiser ( * args , ** kwargs ) : <EOL> raise safe_exception <EOL> wrapper = nova . api . openstack . FaultWrapper ( raiser ) <EOL> response = req . get_response ( wrapper ) <EOL> mock_translate . assert_any_call ( u'<STR_LIT>' , None ) <EOL> self . assertIn ( "<STR_LIT>" , response . body . decode ( "<STR_LIT>" ) ) <EOL> class TestFaults ( test . NoDBTestCase ) : <EOL> """<STR_LIT>""" <EOL> def _prepare_xml ( self , xml_string ) : <EOL> """<STR_LIT>""" <EOL> xml_string = xml_string . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) <EOL> xml_string = xml_string . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) <EOL> xml_string = xml_string . replace ( "<STR_LIT:\t>" , "<STR_LIT>" ) <EOL> return xml_string <EOL> def test_400_fault_json ( self ) : <EOL> requests = [ <EOL> webob . Request . blank ( '<STR_LIT>' ) , <EOL> webob . Request . blank ( '<STR_LIT:/>' , headers = { "<STR_LIT>" : "<STR_LIT:application/json>" } ) , <EOL> ] <EOL> for request in requests : <EOL> fault = wsgi . Fault ( webob . exc . HTTPBadRequest ( explanation = '<STR_LIT>' ) ) <EOL> response = request . get_response ( fault ) <EOL> expected = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT:code>" : <NUM_LIT> , <EOL> } , <EOL> } <EOL> actual = jsonutils . loads ( response . body ) <EOL> self . assertEqual ( response . content_type , "<STR_LIT:application/json>" ) <EOL> self . assertEqual ( expected , actual ) <EOL> def test_413_fault_json ( self ) : <EOL> requests = [ <EOL> webob . Request . blank ( '<STR_LIT>' ) , <EOL> webob . Request . blank ( '<STR_LIT:/>' , headers = { "<STR_LIT>" : "<STR_LIT:application/json>" } ) , <EOL> ] <EOL> for request in requests : <EOL> exc = webob . exc . HTTPRequestEntityTooLarge <EOL> fault = wsgi . Fault ( exc ( explanation = '<STR_LIT>' , <EOL> headers = { '<STR_LIT>' : <NUM_LIT:4> } ) ) <EOL> response = request . get_response ( fault ) <EOL> expected = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT:code>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT:4>" , <EOL> } , <EOL> } <EOL> actual = jsonutils . loads ( response . body ) <EOL> self . assertEqual ( response . content_type , "<STR_LIT:application/json>" ) <EOL> self . assertEqual ( expected , actual ) <EOL> def test_429_fault_json ( self ) : <EOL> requests = [ <EOL> webob . Request . blank ( '<STR_LIT>' ) , <EOL> webob . Request . blank ( '<STR_LIT:/>' , headers = { "<STR_LIT>" : "<STR_LIT:application/json>" } ) , <EOL> ] <EOL> for request in requests : <EOL> exc = webob . exc . HTTPTooManyRequests <EOL> fault = wsgi . Fault ( exc ( explanation = '<STR_LIT>' , <EOL> headers = { '<STR_LIT>' : <NUM_LIT:4> } ) ) <EOL> response = request . get_response ( fault ) <EOL> expected = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT:code>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT:4>" , <EOL> } , <EOL> } <EOL> actual = jsonutils . loads ( response . body ) <EOL> self . assertEqual ( response . content_type , "<STR_LIT:application/json>" ) <EOL> self . assertEqual ( expected , actual ) <EOL> def test_fault_has_status_int ( self ) : <EOL> fault = wsgi . Fault ( webob . exc . HTTPBadRequest ( explanation = '<STR_LIT>' ) ) <EOL> self . assertEqual ( fault . status_int , <NUM_LIT> ) </s>
