]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix Status-Line in HTTP response
authorIvan Kolodyazhny <e0ne@e0ne.info>
Tue, 6 Oct 2015 11:52:53 +0000 (14:52 +0300)
committerIvan Kolodyazhny <e0ne@e0ne.info>
Tue, 13 Oct 2015 10:11:10 +0000 (10:11 +0000)
There is a strict rule about constructing status line for HTTP:
'...Status-Line, consisting of the protocol version followed by a
numeric status code and its associated textual phrase, with each
element separated by SP characters'
(http://www.faqs.org/rfcs/rfc2616.html)

This patch coerces filling associated textual phrase.

Also removed unused code from cinder/tests/unit/test_exception.py

Change-Id: Ia9099fb5020cee02bfee2cd0e8e111845918c36c
Closes-Bug: #1496055
Co-Authored-By: Marian Horban <mhorban@mirantis.com>
cinder/exception.py
cinder/tests/unit/test_exception.py

index 9367cae71854351e58339db9331325fbc69c260c..2fed70b0b22b1c0533a2bf3714eff5f201b235e2 100644 (file)
@@ -29,6 +29,8 @@ from oslo_log import log as logging
 from oslo_versionedobjects import exception as obj_exc
 import six
 import webob.exc
+from webob.util import status_generic_reasons
+from webob.util import status_reasons
 
 from cinder.i18n import _, _LE
 
@@ -48,7 +50,21 @@ CONF.register_opts(exc_log_opts)
 class ConvertedException(webob.exc.WSGIHTTPException):
     def __init__(self, code=400, title="", explanation=""):
         self.code = code
-        self.title = title
+        # There is a strict rule about constructing status line for HTTP:
+        # '...Status-Line, consisting of the protocol version followed by a
+        # numeric status code and its associated textual phrase, with each
+        # element separated by SP characters'
+        # (http://www.faqs.org/rfcs/rfc2616.html)
+        # 'code' and 'title' can not be empty because they correspond
+        # to numeric status code and its associated text
+        if title:
+            self.title = title
+        else:
+            try:
+                self.title = status_reasons[self.code]
+            except KeyError:
+                generic_code = self.code // 100
+                self.title = status_generic_reasons[generic_code]
         self.explanation = explanation
         super(ConvertedException, self).__init__()
 
index ae2f0981a27b0bf6d6608d4707592a893dd1a5ba..b8591b43599061baac9524ca120f089d24c5941f 100644 (file)
 from cinder import exception
 from cinder import test
 
+import mock
 import six
-
-
-class FakeNotifier(object):
-    """Acts like the cinder.openstack.common.notifier.api module."""
-    ERROR = 88
-
-    def __init__(self):
-        self.provided_publisher = None
-        self.provided_event = None
-        self.provided_priority = None
-        self.provided_payload = None
-
-    def notify(self, context, publisher, event, priority, payload):
-        self.provided_publisher = publisher
-        self.provided_event = event
-        self.provided_priority = priority
-        self.provided_payload = payload
-
-
-def good_function():
-    return 99
-
-
-def bad_function_error():
-    raise exception.Error()
-
-
-def bad_function_exception():
-    raise test.TestingException()
+import webob.util
 
 
 class CinderExceptionTestCase(test.TestCase):
@@ -128,3 +101,23 @@ class CinderExceptionTestCase(test.TestCase):
         exc1 = Exception(msg)
         exc2 = FakeCinderException(message=exc1)
         self.assertEqual('Exception: test message', six.text_type(exc2))
+
+
+class CinderConvertedExceptionTestCase(test.TestCase):
+    def test_default_args(self):
+        exc = exception.ConvertedException()
+        self.assertNotEqual('', exc.title)
+        self.assertEqual(400, exc.code)
+        self.assertEqual('', exc.explanation)
+
+    def test_standard_status_code(self):
+        with mock.patch.dict(webob.util.status_reasons, {200: 'reason'}):
+            exc = exception.ConvertedException(code=200)
+            self.assertEqual('reason', exc.title)
+
+    @mock.patch.dict(webob.util.status_reasons, {500: 'reason'})
+    def test_generic_status_code(self):
+        with mock.patch.dict(webob.util.status_generic_reasons,
+                             {5: 'generic_reason'}):
+            exc = exception.ConvertedException(code=599)
+            self.assertEqual('generic_reason', exc.title)