def test_wrong_content_type_server_error(self): def my_fault_body_function(): return b'off' resource = wsgi.Resource(None, my_fault_body_function) request = wsgi.Request.blank("/", method='POST', headers={'Content-Type': "unknow"}) response = resource(request) self.assertEqual(500, response.status_int)
def test_dispatch_unknown_controller_action(self): class Controller(object): def index(self, request, pants=None): return pants def my_fault_body_function(): return b'off' resource = wsgi.Resource(Controller(), my_fault_body_function) self.assertRaises(AttributeError, resource.dispatch, resource.controller, 'create', {})
def test_wrong_content_type_throws_unsupported_media_type_error(self): def my_fault_body_function(): return b'off' resource = wsgi.Resource(None, my_fault_body_function) request = wsgi.Request.blank("/", body=b"{some:json}", method='POST', headers={'Content-Type': "xxx"}) response = resource(request) self.assertEqual(400, response.status_int)
def test_malformed_request_body_throws_bad_request(self): def my_fault_body_function(): return b'off' resource = wsgi.Resource(None, my_fault_body_function) request = wsgi.Request.blank( "/", body=b"{mal:formed", method='POST', headers={'Content-Type': "application/json"}) response = resource(request) self.assertEqual(400, response.status_int)
def test_dispatch(self): class Controller(object): def index(self, request, index=None): return index def my_fault_body_function(): return 'off' resource = wsgi.Resource(Controller(), my_fault_body_function) actual = resource.dispatch(resource.controller, 'index', action_args={'index': 'off'}) expected = 'off' self.assertEqual(expected, actual)
def test_type_error(self): class Controller(object): def index(self, request, index=None): return index def my_fault_body_function(): return b'off' resource = wsgi.Resource(Controller(), my_fault_body_function) request = wsgi.Request.blank("/", method='POST', headers={'Content-Type': "json"}) response = resource.dispatch(request, action='index', action_args='test') self.assertEqual(400, response.status_int)
def test_call_resource_class_internal_error(self): class Controller(object): def index(self, request, index=None): return index def my_fault_body_function(): return b'off' class FakeRequest(object): def __init__(self): self.url = 'http://where.no' self.environ = 'environ' self.body = b'{"Content-Type": "json"}' def method(self): pass def best_match_content_type(self): return 'application/json' resource = wsgi.Resource(Controller(), my_fault_body_function) request = FakeRequest() result = resource(request) self.assertEqual(500, result.status_int)