From 26ebe3bc9f99af76110d855afe6f9cdeb110ea05 Mon Sep 17 00:00:00 2001 From: Kurt Griffiths Date: Wed, 24 Dec 2014 13:47:44 -0600 Subject: [PATCH] test(HTTPError): Test to_json helper Update the error serialization test so that it tests to_json, not just to_yaml. Remove the dependency on OrderedDict in the same test. --- falcon/http_error.py | 9 +++------ tests/test_httperror.py | 31 ++++++++++++++++++++----------- tools/test-requires | 1 - tox.ini | 4 ++-- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/falcon/http_error.py b/falcon/http_error.py index dd34504..2ae45d0 100644 --- a/falcon/http_error.py +++ b/falcon/http_error.py @@ -13,15 +13,12 @@ # limitations under the License. import json -import sys import xml.etree.ElementTree as et -if sys.version_info < (2, 7): # pragma: no cover - # NOTE(kgriffs): We could use the module from PyPI, but ordering isn't - # critical in JSON, and Falcon eschews dependencies. - OrderedDict = dict -else: # pragma: no cover +try: # pragma nocover from collections import OrderedDict +except ImportError: # pragma nocover + OrderedDict = dict from falcon.util import uri diff --git a/tests/test_httperror.py b/tests/test_httperror.py index bb18183..2a94062 100644 --- a/tests/test_httperror.py +++ b/tests/test_httperror.py @@ -272,7 +272,6 @@ class TestHTTPError(testing.TestBase): def test_custom_error_serializer(self): headers = { - 'Accept': 'application/x-yaml', 'X-Error-Title': 'Storage service down', 'X-Error-Description': ('The configured storage service is not ' 'responding to requests. Please contact ' @@ -280,12 +279,15 @@ class TestHTTPError(testing.TestBase): 'X-Error-Status': falcon.HTTP_503 } - expected_yaml = (b'{code: 10042, description: The configured storage ' - b'service is not responding to requests.\n ' - b'Please contact your service provider, title: ' - b'Storage service down}\n') + expected_doc = { + 'code': 10042, + 'description': ('The configured storage service is not ' + 'responding to requests. Please contact ' + 'your service provider'), + 'title': 'Storage service down' + } - def my_serializer(req, exception): + def _my_serializer(req, exception): representation = None preferred = req.client_prefers(('application/x-yaml', @@ -293,17 +295,24 @@ class TestHTTPError(testing.TestBase): if preferred is not None: if preferred == 'application/json': - representation = exception.json() + representation = exception.to_json() else: representation = yaml.dump(exception.to_dict(), encoding=None) return (preferred, representation) - self.api.set_error_serializer(my_serializer) - body = self.simulate_request('/fail', headers=headers) - self.assertEqual(self.srmock.status, headers['X-Error-Status']) - self.assertEqual(body, [expected_yaml]) + def _check(media_type, deserializer): + headers['Accept'] = media_type + self.api.set_error_serializer(_my_serializer) + body = self.simulate_request('/fail', headers=headers) + self.assertEqual(self.srmock.status, headers['X-Error-Status']) + + actual_doc = deserializer(body[0].decode('utf-8')) + self.assertEqual(expected_doc, actual_doc) + + # _check('application/x-yaml', yaml.load) + _check('application/json', json.loads) def test_client_does_not_accept_anything(self): headers = { diff --git a/tools/test-requires b/tools/test-requires index 273c85d..577f86b 100644 --- a/tools/test-requires +++ b/tools/test-requires @@ -1,7 +1,6 @@ coverage ddt nose -ordereddict pyyaml requests six diff --git a/tox.ini b/tox.ini index 0017f53..b950eb4 100644 --- a/tox.ini +++ b/tox.ini @@ -2,8 +2,8 @@ envlist = py26, py27, py27_cython, - py33, - py33_cython, + ; py33, + ; py33_cython, py34, py34_cython, pypy,