diff options
-rw-r--r-- | app/handlers/response.py | 12 | ||||
-rw-r--r-- | app/handlers/tests/test_handler_response.py | 84 | ||||
-rw-r--r-- | app/tests/__init__.py | 1 |
3 files changed, 91 insertions, 6 deletions
diff --git a/app/handlers/response.py b/app/handlers/response.py index 68d8af8..75c358d 100644 --- a/app/handlers/response.py +++ b/app/handlers/response.py @@ -28,13 +28,13 @@ class HandlerResponse(object): This might be used to pass custom message or set custom headers after an action has been performed. """ - def __init__(self, status_code, message=None, headers=None): + def __init__(self, status_code): + if not isinstance(status_code, IntType): + raise ValueError("Value must be an integer") + self._status_code = status_code - self._message = message - if headers: - self._headers = headers - else: - self._headers = {} + self._message = None + self._headers = {} @property def status_code(self): diff --git a/app/handlers/tests/test_handler_response.py b/app/handlers/tests/test_handler_response.py new file mode 100644 index 0000000..b1beb85 --- /dev/null +++ b/app/handlers/tests/test_handler_response.py @@ -0,0 +1,84 @@ +# Copyright (C) 2014 Linaro Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +"""Test class for HandlerResponse object.""" + +import unittest + +from handlers.response import HandlerResponse + + +class TestHandlerResponse(unittest.TestCase): + + def test_response_constructor_not_valid_input(self): + self.assertRaises(ValueError, HandlerResponse, "1") + + def test_response_setter_not_valid(self): + response = HandlerResponse(1) + + def _setter_call(value): + response.status_code = value + + self.assertRaises(ValueError, _setter_call, "1") + + def test_response_setter_valid(self): + response = HandlerResponse(1) + response.status_code = 200 + + self.assertEqual(response.status_code, 200) + + def test_reponse_creation_default_values(self): + response = HandlerResponse(0) + + self.assertEqual(response.headers, {}) + self.assertIsNone(response.message) + + def test_response_message_setter_not_valid(self): + response = HandlerResponse(0) + + def _setter_call(value): + response.message = value + + self.assertRaises(ValueError, _setter_call, 1) + self.assertRaises(ValueError, _setter_call, True) + self.assertRaises(ValueError, _setter_call, []) + self.assertRaises(ValueError, _setter_call, {}) + + def test_response_message_setter_valid(self): + response = HandlerResponse(1) + + response.message = u'foo' + self.assertEqual('foo', response.message) + + response.message = r'bar' + self.assertEqual('bar', response.message) + + def test_response_headers_setter_not_valid(self): + response = HandlerResponse(0) + + def _setter_call(value): + response.headers = value + + self.assertRaises(ValueError, _setter_call, 1) + self.assertRaises(ValueError, _setter_call, True) + self.assertRaises(ValueError, _setter_call, []) + self.assertRaises(ValueError, _setter_call, ()) + self.assertRaises(ValueError, _setter_call, "1") + + def test_response_headers_setter_valid(self): + response = HandlerResponse(0) + + response.headers = {'foo': 'bar'} + self.assertEqual({'foo': 'bar'}, response.headers) diff --git a/app/tests/__init__.py b/app/tests/__init__.py index 03a2433..9360971 100644 --- a/app/tests/__init__.py +++ b/app/tests/__init__.py @@ -22,6 +22,7 @@ def test_modules(): return [ 'handlers.tests.test_count_handler', 'handlers.tests.test_defconf_handler', + 'handlers.tests.test_handler_response', 'handlers.tests.test_handler_static', 'handlers.tests.test_job_handler', 'handlers.tests.test_subscription_handler', |