Merge "pass the empty body into the controller"
This commit is contained in:
commit
4f8be715d7
@ -95,7 +95,7 @@ class Controller(object):
|
||||
return dict(consoles=[_translate_keys(console)
|
||||
for console in consoles])
|
||||
|
||||
def create(self, req, server_id):
|
||||
def create(self, req, server_id, body):
|
||||
"""Creates a new console."""
|
||||
self.console_api.create_console(
|
||||
req.environ['nova.context'],
|
||||
@ -113,7 +113,7 @@ class Controller(object):
|
||||
raise exc.HTTPNotFound()
|
||||
return _translate_detail_keys(console)
|
||||
|
||||
def update(self, req, server_id, id):
|
||||
def update(self, req, server_id, id, body):
|
||||
"""You can't update a console."""
|
||||
raise exc.HTTPNotImplemented()
|
||||
|
||||
|
@ -63,7 +63,7 @@ class ConsolesController(object):
|
||||
|
||||
@extensions.expected_errors(404)
|
||||
@wsgi.response(201)
|
||||
def create(self, req, server_id):
|
||||
def create(self, req, server_id, body):
|
||||
"""Creates a new console."""
|
||||
try:
|
||||
self.console_api.create_console(
|
||||
|
@ -169,7 +169,7 @@ class ExtensionsResource(wsgi.Resource):
|
||||
def delete(self, req, id):
|
||||
raise webob.exc.HTTPNotFound()
|
||||
|
||||
def create(self, req):
|
||||
def create(self, req, body):
|
||||
raise webob.exc.HTTPNotFound()
|
||||
|
||||
|
||||
|
@ -73,6 +73,11 @@ _ROUTES_METHODS = [
|
||||
'update',
|
||||
]
|
||||
|
||||
_METHODS_WITH_BODY = [
|
||||
'POST',
|
||||
'PUT',
|
||||
]
|
||||
|
||||
|
||||
class Request(webob.Request):
|
||||
"""Add some OpenStack API-specific logic to the base webob.Request."""
|
||||
@ -823,14 +828,6 @@ class Resource(wsgi.Application):
|
||||
LOG.debug(_("Unrecognized Content-Type provided in request"))
|
||||
return None, ''
|
||||
|
||||
if not content_type:
|
||||
LOG.debug(_("No Content-Type provided in request"))
|
||||
return None, ''
|
||||
|
||||
if len(request.body) <= 0:
|
||||
LOG.debug(_("Empty body provided in request"))
|
||||
return None, ''
|
||||
|
||||
return content_type, request.body
|
||||
|
||||
def deserialize(self, meth, content_type, body):
|
||||
@ -910,6 +907,9 @@ class Resource(wsgi.Application):
|
||||
|
||||
return None
|
||||
|
||||
def _should_have_body(self, request):
|
||||
return request.method in _METHODS_WITH_BODY
|
||||
|
||||
@webob.dec.wsgify(RequestClass=Request)
|
||||
def __call__(self, request):
|
||||
"""WSGI method that controls (de)serialization and method dispatch."""
|
||||
@ -955,10 +955,13 @@ class Resource(wsgi.Application):
|
||||
|
||||
# Now, deserialize the request body...
|
||||
try:
|
||||
if content_type:
|
||||
contents = self.deserialize(meth, content_type, body)
|
||||
else:
|
||||
contents = {}
|
||||
contents = {}
|
||||
if self._should_have_body(request):
|
||||
#allow empty body with PUT and POST
|
||||
if request.content_length == 0:
|
||||
contents = {'body': None}
|
||||
else:
|
||||
contents = self.deserialize(meth, content_type, body)
|
||||
except exception.InvalidContentType:
|
||||
msg = _("Unsupported Content-Type")
|
||||
return Fault(webob.exc.HTTPBadRequest(explanation=msg))
|
||||
|
@ -143,7 +143,7 @@ class ConsolesControllerTest(test.NoDBTestCase):
|
||||
self.stubs.Set(console.api.API, 'create_console', fake_create_console)
|
||||
|
||||
req = fakes.HTTPRequestV3.blank(self.url)
|
||||
self.controller.create(req, self.uuid)
|
||||
self.controller.create(req, self.uuid, None)
|
||||
self.assertEqual(self.controller.create.wsgi_code, 201)
|
||||
|
||||
def test_create_console_unknown_instance(self):
|
||||
@ -153,7 +153,7 @@ class ConsolesControllerTest(test.NoDBTestCase):
|
||||
|
||||
req = fakes.HTTPRequestV3.blank(self.url)
|
||||
self.assertRaises(webob.exc.HTTPNotFound, self.controller.create,
|
||||
req, self.uuid)
|
||||
req, self.uuid, None)
|
||||
|
||||
def test_show_console(self):
|
||||
def fake_get_console(cons_self, context, instance_id, console_id):
|
||||
|
@ -144,7 +144,7 @@ class ConsolesControllerTest(test.NoDBTestCase):
|
||||
self.stubs.Set(console.api.API, 'create_console', fake_create_console)
|
||||
|
||||
req = fakes.HTTPRequest.blank(self.url)
|
||||
self.controller.create(req, self.uuid)
|
||||
self.controller.create(req, self.uuid, None)
|
||||
|
||||
def test_show_console(self):
|
||||
def fake_get_console(cons_self, context, instance_id, console_id):
|
||||
|
@ -49,7 +49,7 @@ class StubController(object):
|
||||
def index(self, req):
|
||||
return self.body
|
||||
|
||||
def create(self, req):
|
||||
def create(self, req, body):
|
||||
msg = 'All aboard the fail train!'
|
||||
raise webob.exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
import inspect
|
||||
import webob
|
||||
|
||||
from nova.api.openstack import extensions
|
||||
from nova.api.openstack import wsgi
|
||||
from nova import exception
|
||||
from nova.openstack.common import gettextutils
|
||||
@ -324,16 +325,108 @@ class XMLDeserializerTest(test.NoDBTestCase):
|
||||
|
||||
|
||||
class ResourceTest(test.NoDBTestCase):
|
||||
def test_resource_call(self):
|
||||
def test_resource_call_with_method_get(self):
|
||||
class Controller(object):
|
||||
def index(self, req):
|
||||
return 'off'
|
||||
return 'success'
|
||||
|
||||
req = webob.Request.blank('/tests')
|
||||
app = fakes.TestRouter(Controller())
|
||||
# the default method is GET
|
||||
req = webob.Request.blank('/tests')
|
||||
response = req.get_response(app)
|
||||
self.assertEqual(response.body, 'off')
|
||||
self.assertEqual(response.body, 'success')
|
||||
self.assertEqual(response.status_int, 200)
|
||||
req.body = '{"body": {"key": "value"}}'
|
||||
response = req.get_response(app)
|
||||
self.assertEqual(response.body, 'success')
|
||||
self.assertEqual(response.status_int, 200)
|
||||
req.content_type = 'application/json'
|
||||
response = req.get_response(app)
|
||||
self.assertEqual(response.body, 'success')
|
||||
self.assertEqual(response.status_int, 200)
|
||||
|
||||
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 = '{"body": {"key": "value"}}'
|
||||
expected_body = {'body': {
|
||||
"key": "value"
|
||||
}
|
||||
}
|
||||
response = req.get_response(app)
|
||||
self.assertEqual(response.status_int, 200)
|
||||
self.assertEqual(response.body, 'success')
|
||||
# verify without body
|
||||
expected_body = None
|
||||
req.body = None
|
||||
response = req.get_response(app)
|
||||
self.assertEqual(response.status_int, 200)
|
||||
self.assertEqual(response.body, 'success')
|
||||
# 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, response.body)
|
||||
|
||||
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 = '{"body": {"key": "value"}}'
|
||||
expected_body = {'body': {
|
||||
"key": "value"
|
||||
}
|
||||
}
|
||||
response = req.get_response(app)
|
||||
self.assertEqual(response.body, 'success')
|
||||
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.content_type = None
|
||||
req.body = '{"body": {"key": "value"}}'
|
||||
response = req.get_response(app)
|
||||
expected_unsupported_type_body = ('{"badRequest": '
|
||||
'{"message": "Unsupported Content-Type", "code": 400}}')
|
||||
self.assertEqual(response.status_int, 400)
|
||||
self.assertEqual(expected_unsupported_type_body, response.body)
|
||||
|
||||
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(response.body, 'success')
|
||||
# ignore the body
|
||||
req.body = '{"body": {"key": "value"}}'
|
||||
response = req.get_response(app)
|
||||
self.assertEqual(response.status_int, 200)
|
||||
self.assertEqual(response.body, 'success')
|
||||
|
||||
def test_resource_not_authorized(self):
|
||||
class Controller(object):
|
||||
@ -493,7 +586,7 @@ class ResourceTest(test.NoDBTestCase):
|
||||
|
||||
content_type, body = resource.get_body(request)
|
||||
self.assertIsNone(content_type)
|
||||
self.assertEqual(body, '')
|
||||
self.assertEqual(body, 'foo')
|
||||
|
||||
def test_get_body_no_content_body(self):
|
||||
class Controller(object):
|
||||
@ -508,7 +601,7 @@ class ResourceTest(test.NoDBTestCase):
|
||||
request.body = ''
|
||||
|
||||
content_type, body = resource.get_body(request)
|
||||
self.assertIsNone(content_type)
|
||||
self.assertEqual('application/json', content_type)
|
||||
self.assertEqual(body, '')
|
||||
|
||||
def test_get_body(self):
|
||||
@ -945,10 +1038,10 @@ class ResourceTest(test.NoDBTestCase):
|
||||
|
||||
def test_resource_valid_utf8_body(self):
|
||||
class Controller(object):
|
||||
def index(self, req, body):
|
||||
def update(self, req, id, body):
|
||||
return body
|
||||
|
||||
req = webob.Request.blank('/tests')
|
||||
req = webob.Request.blank('/tests/test_id', method="PUT")
|
||||
body = """ {"name": "\xe6\xa6\x82\xe5\xbf\xb5" } """
|
||||
expected_body = '{"name": "\\u6982\\u5ff5"}'
|
||||
req.body = body
|
||||
@ -960,10 +1053,10 @@ class ResourceTest(test.NoDBTestCase):
|
||||
|
||||
def test_resource_invalid_utf8(self):
|
||||
class Controller(object):
|
||||
def index(self, req, body):
|
||||
def update(self, req, id, body):
|
||||
return body
|
||||
|
||||
req = webob.Request.blank('/tests')
|
||||
req = webob.Request.blank('/tests/test_id', method="PUT")
|
||||
body = """ {"name": "\xf0\x28\x8c\x28" } """
|
||||
req.body = body
|
||||
req.headers['Content-Type'] = 'application/json'
|
||||
|
Loading…
Reference in New Issue
Block a user