]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fixing API behaviour for throwing 400 error on invalid body.
authorSalvatore Orlando <salvatore.orlando@eu.citrix.com>
Mon, 1 Aug 2011 14:40:29 +0000 (15:40 +0100)
committerSalvatore Orlando <salvatore.orlando@eu.citrix.com>
Mon, 1 Aug 2011 14:40:29 +0000 (15:40 +0100)
Adding unit test for creating a port without request body.

quantum/api/api_common.py
tests/unit/test_api.py
tests/unit/testlib_api.py

index c588a83567d54fa522a25993aa2c4b61badb2f04..140a7c5b9b6ba166ed60c7c71648be116ed524e7 100644 (file)
@@ -43,7 +43,13 @@ class QuantumController(wsgi.Controller):
                 des_body = self._deserialize(req.body,
                                              req.best_match_content_type())
                 data = des_body and des_body.get(self._resource_name, None)
-                param_value = data and data.get(param_name, None)
+                if not data:
+                    msg = ("Failed to parse request. Resource: " +
+                           self._resource_name + " not found in request body")
+                    for line in msg.split('\n'):
+                        LOG.error(line)
+                    raise exc.HTTPBadRequest(msg)
+                param_value = data.get(param_name, None)
             if not param_value:
                 # 2- parse request headers
                 # prepend param name with a 'x-' prefix
index d17e6ed87fad0c4add107a6b1c6adc959644e2a7..9cad7ed41fead1fbb5985b4e31069e3d6fc4dec8 100644 (file)
@@ -285,6 +285,21 @@ class APITest(unittest.TestCase):
         self.assertEqual(show_port_res.status_int, 430)
         LOG.debug("_test_show_port_portnotfound - format:%s - END", format)
 
+    def _test_create_port_noreqbody(self, format):
+        LOG.debug("_test_create_port_noreqbody - format:%s - START", format)
+        content_type = "application/%s" % format
+        network_id = self._create_network(format)
+        port_id = self._create_port(network_id, None, format,
+                                    custom_req_body='')
+        show_port_req = testlib.show_port_request(self.tenant_id,
+                                                  network_id, port_id, format)
+        show_port_res = show_port_req.get_response(self.api)
+        self.assertEqual(show_port_res.status_int, 200)
+        port_data = self._port_serializer.deserialize(
+                        show_port_res.body, content_type)
+        self.assertEqual(port_id, port_data['port']['id'])
+        LOG.debug("_test_create_port_noreqbody - format:%s - END", format)
+
     def _test_create_port(self, format):
         LOG.debug("_test_create_port - format:%s - START", format)
         content_type = "application/%s" % format
@@ -741,6 +756,12 @@ class APITest(unittest.TestCase):
     def test_create_port_xml(self):
         self._test_create_port('xml')
 
+    def test_create_port_noreqbody_json(self):
+        self._test_create_port_noreqbody('json')
+
+    def test_create_port_noreqbody_xml(self):
+        self._test_create_port_noreqbody('xml')
+
     def test_create_port_networknotfound_json(self):
         self._test_create_port_networknotfound('json')
 
index 1a731be1545bf196a438e48376f0c9cf651f2125..0a50d616e06ad1f3a72a1b70546d24c127c0ec2b 100644 (file)
@@ -77,9 +77,10 @@ def new_port_request(tenant_id, network_id, port_state,
     method = 'POST'
     path = "/tenants/%(tenant_id)s/networks/" \
            "%(network_id)s/ports.%(format)s" % locals()
-    data = custom_req_body or {'port': {'port-state': '%s' % port_state}}
+    data = custom_req_body or port_state and \
+           {'port': {'port-state': '%s' % port_state}}
     content_type = "application/%s" % format
-    body = Serializer().serialize(data, content_type)
+    body = data and Serializer().serialize(data, content_type)
     return create_request(path, body, content_type, method)