test: Fix result decoding of error bodies
Some tests were failing under Py3K since the json library behaves a little
differently when it comes to decoding bytestrings. To fix this, the
simulate_request helper method was extended to take a new "decode" arg,
so tests can get a decoded string right off the bat.
Here is the docstring:
decode: If set to a character encoding, such as 'utf-8',
the method will assume the response is a single
byte string and will decode it and return a single
wide string instead of the raw WSGI response iterable.
This commit is contained in:
@@ -69,13 +69,17 @@ class TestBase(unittest.TestCase):
|
||||
|
||||
super(TestBase, self).tearDown()
|
||||
|
||||
def simulate_request(self, path, **kwargs):
|
||||
def simulate_request(self, path, decode=None, **kwargs):
|
||||
""" Simulates a request.
|
||||
|
||||
Simulates a request to the API for testing purposes.
|
||||
|
||||
Args:
|
||||
path: Request path for the desired resource
|
||||
decode: If set to a character encoding, such as 'utf-8',
|
||||
the method will assume the response is a single
|
||||
byte string and will decode it and return a single
|
||||
wide string instead of the raw WSGI response iterable.
|
||||
kwargs: Same as falcon.testing.create_environ()
|
||||
|
||||
"""
|
||||
@@ -83,5 +87,13 @@ class TestBase(unittest.TestCase):
|
||||
if not path:
|
||||
path = '/'
|
||||
|
||||
return self.api(create_environ(path=path, **kwargs),
|
||||
self.srmock)
|
||||
result = self.api(create_environ(path=path, **kwargs),
|
||||
self.srmock)
|
||||
|
||||
if decode is not None:
|
||||
if not result:
|
||||
return ''
|
||||
|
||||
return result[0].decode(decode)
|
||||
|
||||
return result
|
||||
|
||||
@@ -141,8 +141,7 @@ class TestHTTPError(testing.TestBase):
|
||||
|
||||
# Try it with Accept: */*
|
||||
headers['Accept'] = '*/*'
|
||||
body = self.simulate_request('/fail', headers=headers)
|
||||
body = body[0]
|
||||
body = self.simulate_request('/fail', headers=headers, decode='utf-8')
|
||||
|
||||
self.assertEqual(self.srmock.status, headers['X-Error-Status'])
|
||||
self.assertThat(lambda: json.loads(body), Not(raises(ValueError)))
|
||||
@@ -150,8 +149,7 @@ class TestHTTPError(testing.TestBase):
|
||||
|
||||
# Now try it with application/json
|
||||
headers['Accept'] = 'application/json'
|
||||
body = self.simulate_request('/fail', headers=headers)
|
||||
body = body[0]
|
||||
body = self.simulate_request('/fail', headers=headers, decode='utf-8')
|
||||
|
||||
self.assertEqual(self.srmock.status, headers['X-Error-Status'])
|
||||
self.assertThat(lambda: json.loads(body), Not(raises(ValueError)))
|
||||
@@ -206,8 +204,8 @@ class TestHTTPError(testing.TestBase):
|
||||
},
|
||||
}
|
||||
|
||||
body = self.simulate_request('/fail', headers=headers, method='POST')
|
||||
body = body[0]
|
||||
body = self.simulate_request('/fail', headers=headers, method='POST',
|
||||
decode='utf-8')
|
||||
|
||||
self.assertEqual(self.srmock.status, falcon.HTTP_403)
|
||||
self.assertThat(lambda: json.loads(body), Not(raises(ValueError)))
|
||||
@@ -228,8 +226,8 @@ class TestHTTPError(testing.TestBase):
|
||||
},
|
||||
}
|
||||
|
||||
body = self.simulate_request('/fail', headers=headers, method='PUT')
|
||||
body = body[0]
|
||||
body = self.simulate_request('/fail', headers=headers, method='PUT',
|
||||
decode='utf-8')
|
||||
|
||||
self.assertEqual(self.srmock.status, falcon.HTTP_792)
|
||||
self.assertThat(lambda: json.loads(body), Not(raises(ValueError)))
|
||||
@@ -249,12 +247,11 @@ class TestHTTPError(testing.TestBase):
|
||||
}
|
||||
|
||||
self.api.add_route('/unicode', unicode_resource)
|
||||
body = self.simulate_request('/unicode')
|
||||
body = body[0]
|
||||
body = self.simulate_request('/unicode', decode='utf-8')
|
||||
|
||||
self.assertTrue(unicode_resource.called)
|
||||
self.assertEqual(self.srmock.status, falcon.HTTP_792)
|
||||
self.assertEqual(expected_body, json.loads(body, 'utf-8'))
|
||||
self.assertEqual(expected_body, json.loads(body))
|
||||
|
||||
def test_401(self):
|
||||
self.api.add_route('/401', UnauthorizedResource())
|
||||
@@ -319,8 +316,7 @@ class TestHTTPError(testing.TestBase):
|
||||
|
||||
def test_503(self):
|
||||
self.api.add_route('/503', ServiceUnavailableResource())
|
||||
body = self.simulate_request('/503')
|
||||
body = body[0]
|
||||
body = self.simulate_request('/503', decode='utf-8')
|
||||
|
||||
expected_body = {
|
||||
'title': 'Oops',
|
||||
|
||||
@@ -163,7 +163,7 @@ class TestFalconUtils(testtools.TestCase):
|
||||
self.assertEqual(expect, actual)
|
||||
|
||||
|
||||
class TestFalconTesting(testtools.TestCase):
|
||||
class TestFalconTesting(falcon.testing.TestBase):
|
||||
"""Catch some uncommon branches not covered elsewhere."""
|
||||
|
||||
def test_unicode_path_in_create_environ(self):
|
||||
@@ -179,3 +179,7 @@ class TestFalconTesting(testtools.TestCase):
|
||||
def test_none_header_value_in_create_environ(self):
|
||||
env = falcon.testing.create_environ('/', headers={'X-Foo': None})
|
||||
self.assertEqual(env['HTTP_X_FOO'], '')
|
||||
|
||||
def test_decode_empty_result(self):
|
||||
body = self.simulate_request('/', decode='utf-8')
|
||||
self.assertEqual(body, '')
|
||||
|
||||
Reference in New Issue
Block a user