Commit 556d5f15 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Escape control characters in HTTP body, when json

Fixes #32

This commit ensures that control characters are escaped in all
HTTP response bodies, as long as they are parsed as json strings
Raw content is kept intact
Response header contents were already handled correctly
parent 5b7b1e5e
......@@ -351,7 +351,11 @@ class ResponseManager(Logged):
return loads(self._content)
# Ensure there are no line breaks in json string
results_in_dict = loads(self._content)
results_in_str = dumps(results_in_dict)
# Escape control characters and parse to python object
return loads(results_in_str.encode('unicode_escape'))
except ValueError as err:
raise ClientError('Response not formated in JSON - %s' % err)
......@@ -205,6 +205,14 @@ class ResponseManager(TestCase):
self.assertEqual(self.RM.json, FakeResp.HEADERS)
self.assertTrue(isinstance(perform.call_args[0][0], self.HTTPC))
# Check if some control characters are escaped
sample = dict(k1='Do not \b\b\b\b\b\b\bescape this', k2='Escape \nit')
exp = dict(
k1='Do not \\b\\b\\b\\b\\b\\b\\bescape this', k2='Escape \\nit')
FakeResp.READ = dumps(sample)
self.RM._request_performed = False
self.assertEqual(self.RM.json, exp)
@patch('kamaki.clients.RequestManager.perform', return_value=FakeResp())
def test_all(self, perform):
self.assertEqual(self.RM.content, FakeResp.READ)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment