diff options
author | Tim Burke <tim.burke@gmail.com> | 2015-05-21 22:44:36 -0700 |
---|---|---|
committer | Tim Burke <tim.burke@gmail.com> | 2016-08-30 15:38:55 -0700 |
commit | f728027bed59d08a6491ae9c14d2f5968f8d6fa3 (patch) | |
tree | 5dad1390ace6471f55d2c017a151e424db1e3d18 /tests/unit/test_utils.py | |
parent | 20e0c515bf23841015102fb72d61ab18c826c036 (diff) | |
download | python-swiftclient-f728027bed59d08a6491ae9c14d2f5968f8d6fa3.tar.gz |
Accept gzip-encoded API responses
Previously, we would accept gzip-encoded responses, but only because we
were letting requests decode *all* responses (even object data). This
restores the previous capability, but with tighter controls about which
requests will accept gzipped responses and where the decoding happens.
Change-Id: I4fd8b97207b9ab01b1bcf825cc16efd8ad46344a
Related-Bug: 1282861
Related-Bug: 1338464
Diffstat (limited to 'tests/unit/test_utils.py')
-rw-r--r-- | tests/unit/test_utils.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 0f210a3..f0de79c 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import gzip import unittest import mock import six @@ -394,3 +395,39 @@ class TestGroupers(unittest.TestCase): result = list(u.n_groups(range(100), 12)) self.assertEqual([9] * 11 + [1], list(map(len, result))) + + +class TestApiResponeParser(unittest.TestCase): + + def test_utf8_default(self): + result = u.parse_api_response( + {}, u'{"test": "\u2603"}'.encode('utf8')) + self.assertEqual({'test': u'\u2603'}, result) + + result = u.parse_api_response( + {}, u'{"test": "\\u2603"}'.encode('utf8')) + self.assertEqual({'test': u'\u2603'}, result) + + def test_bad_json(self): + self.assertRaises(ValueError, u.parse_api_response, + {}, b'{"foo": "bar}') + + def test_bad_utf8(self): + self.assertRaises(UnicodeDecodeError, u.parse_api_response, + {}, b'{"foo": "b\xffr"}') + + def test_latin_1(self): + result = u.parse_api_response( + {'content-type': 'application/json; charset=iso8859-1'}, + b'{"t\xe9st": "\xff"}') + self.assertEqual({u't\xe9st': u'\xff'}, result) + + def test_gzipped_utf8(self): + buf = six.BytesIO() + gz = gzip.GzipFile(fileobj=buf, mode='w') + gz.write(u'{"test": "\u2603"}'.encode('utf8')) + gz.close() + result = u.parse_api_response( + {'content-encoding': 'gzip'}, + buf.getvalue()) + self.assertEqual({'test': u'\u2603'}, result) |