def test_resource_receives_api_version_request_invalid(self): invalid_version = "2.5.3" class Controller(object): def index(self, req): return 'success' app = fakes.TestRouter(Controller()) req = webob.Request.blank('/tests') req.headers = self._make_microversion_header(invalid_version) response = req.get_response(app) self.assertEqual(400, response.status_int)
def test_resource_receives_api_version_request_default(self): class Controller(object): def index(self, req): if req.api_version_request != \ api_version.APIVersionRequest(wsgi.DEFAULT_API_VERSION): raise webob.exc.HTTPInternalServerError() return 'success' app = fakes.TestRouter(Controller()) req = webob.Request.blank('/tests') response = req.get_response(app) self.assertEqual(response.body, 'success') self.assertEqual(response.status_int, 200)
def test_resource_valid_utf8_body(self): class Controller(object): def update(self, req, id, body): return body req = webob.Request.blank('/tests/test_id', method="PUT") body = b""" {"name": "\xe6\xa6\x82\xe5\xbf\xb5" } """ expected_body = b'{"name": "\\u6982\\u5ff5"}' req.body = body req.headers['Content-Type'] = 'application/json' app = fakes.TestRouter(Controller()) response = req.get_response(app) self.assertEqual(response.body, expected_body) self.assertEqual(response.status_int, 200)
def test_no_request_id_with_str_response_body(self): class Controller(wsgi.Controller): def index(self, req): return 'foo' req = fakes.HTTPRequest.blank('/tests') app = fakes.TestRouter(Controller()) response = req.get_response(app) # NOTE(alaski): This test is really to ensure that a str response # doesn't error. Not having a request_id header is a side effect of # our wsgi setup, ideally it would be there. expected_header = self.get_req_id_header_name(req) self.assertFalse(hasattr(response.headers, expected_header)) self.assertEqual(b'foo', response.body) self.assertEqual(response.status_int, 200)
def test_resource_call_with_method_delete(self): class Controller(object): def delete(self, req, id): return "success" # verify the method: DELETE app = fakes.TestRouter(Controller()) req = webob.Request.blank('/tests/test_id', method="DELETE") response = req.get_response(app) self.assertEqual(response.status_int, 200) self.assertEqual(b'success', response.body) # ignore the body req.body = b'{"body": {"key": "value"}}' response = req.get_response(app) self.assertEqual(response.status_int, 200) self.assertEqual(b'success', response.body)
def test_resource_receives_api_version_request(self): version = "2.5" class Controller(object): def index(self, req): if req.api_version_request != \ api_version.APIVersionRequest(version): raise webob.exc.HTTPInternalServerError() return 'success' app = fakes.TestRouter(Controller()) req = webob.Request.blank('/tests') req.headers = {'X-OpenStack-Compute-API-Version': version} response = req.get_response(app) self.assertEqual(response.body, 'success') self.assertEqual(response.status_int, 200)
def test_resource_receives_api_version_request(self, mock_maxver): version = "2.5" mock_maxver.return_value = api_version.APIVersionRequest(version) class Controller(object): def index(self, req): if req.api_version_request != \ api_version.APIVersionRequest(version): raise webob.exc.HTTPInternalServerError() return 'success' app = fakes.TestRouter(Controller()) req = webob.Request.blank('/tests') req.headers = self._make_microversion_header(version) response = req.get_response(app) self.assertEqual(b'success', response.body) self.assertEqual(response.status_int, 200)
def test_resource_headers_are_utf8(self): resp = webob.Response(status_int=202) resp.headers['x-header1'] = 1 resp.headers['x-header2'] = u'header2' resp.headers['x-header3'] = u'header3' class Controller(object): def index(self, req): return resp req = webob.Request.blank('/tests') app = fakes.TestRouter(Controller()) response = req.get_response(app) for val in six.itervalues(response.headers): # All headers must be utf8 self.assertThat(val, matchers.EncodedByUTF8()) self.assertEqual('1', response.headers['x-header1']) self.assertEqual('header2', response.headers['x-header2']) self.assertEqual('header3', response.headers['x-header3'])
def test_resource_call_with_method_get(self): class Controller(object): def index(self, req): return 'success' app = fakes.TestRouter(Controller()) # the default method is GET req = webob.Request.blank('/tests') response = req.get_response(app) self.assertEqual(b'success', response.body) self.assertEqual(response.status_int, 200) req.body = b'{"body": {"key": "value"}}' response = req.get_response(app) self.assertEqual(b'success', response.body) self.assertEqual(response.status_int, 200) req.content_type = 'application/json' response = req.get_response(app) self.assertEqual(b'success', response.body) self.assertEqual(response.status_int, 200)
def test_resource_headers_are_utf8(self): resp = webob.Response(status_int=202) resp.headers['x-header1'] = 1 resp.headers['x-header2'] = u'header2' resp.headers['x-header3'] = u'header3' class Controller(object): def index(self, req): return resp req = webob.Request.blank('/tests') app = fakes.TestRouter(Controller()) response = req.get_response(app) for hdr, val in response.headers.iteritems(): # All headers must be utf8 self.assertIsInstance(hdr, str) self.assertIsInstance(val, str) self.assertEqual(response.headers['x-header1'], '1') self.assertEqual(response.headers['x-header2'], 'header2') self.assertEqual(response.headers['x-header3'], 'header3')
def test_resource_call_with_method_put(self): class Controller(object): def update(self, req, id, body): if expected_body != body: msg = "The request body invalid" raise webob.exc.HTTPBadRequest(explanation=msg) return "success" # verify the method: PUT app = fakes.TestRouter(Controller()) req = webob.Request.blank('/tests/test_id', method="PUT", content_type='application/json') req.body = b'{"body": {"key": "value"}}' expected_body = {'body': {"key": "value"}} response = req.get_response(app) self.assertEqual(b'success', response.body) self.assertEqual(response.status_int, 200) req.body = None expected_body = None response = req.get_response(app) self.assertEqual(response.status_int, 200) # verify no content_type is contained in the request req = webob.Request.blank('/tests/test_id', method="PUT", content_type='application/xml') req.content_type = 'application/xml' req.body = b'{"body": {"key": "value"}}' response = req.get_response(app) expected_unsupported_type_body = { 'badMediaType': { 'message': 'Unsupported Content-Type', 'code': 415 } } self.assertEqual(response.status_int, 415) self.assertEqual(expected_unsupported_type_body, jsonutils.loads(response.body))
def test_resource_call_with_method_post(self): class Controller(object): @extensions.expected_errors(400) def create(self, req, body): if expected_body != body: msg = "The request body invalid" raise webob.exc.HTTPBadRequest(explanation=msg) return "success" # verify the method: POST app = fakes.TestRouter(Controller()) req = webob.Request.blank('/tests', method="POST", content_type='application/json') req.body = b'{"body": {"key": "value"}}' expected_body = {'body': {"key": "value"}} response = req.get_response(app) self.assertEqual(response.status_int, 200) self.assertEqual(b'success', response.body) # verify without body expected_body = None req.body = None response = req.get_response(app) self.assertEqual(response.status_int, 200) self.assertEqual(b'success', response.body) # the body is validated in the controller expected_body = {'body': None} response = req.get_response(app) expected_unsupported_type_body = { 'badRequest': { 'message': 'The request body invalid', 'code': 400 } } self.assertEqual(response.status_int, 400) self.assertEqual(expected_unsupported_type_body, jsonutils.loads(response.body))