Prevent JSON decode errors in the pre-record hook

In the betamax pre-record hook included in keystoneauth1.fixtures, there
was an assumption that ever request body would have JSON. While most
will, this at least checks that they're not empty prior to trying to
decode the body.

Change-Id: I91b80b143576bd4f99392152c4d4675406e26f66
Closes-bug: #1671195
This commit is contained in:
Ian Cordasco 2017-03-08 12:37:21 -06:00
parent 0cd0191818
commit a68e2c1305
2 changed files with 30 additions and 6 deletions

View File

@ -48,11 +48,13 @@ def pre_record_hook(interaction, cassette):
- set token expiration time to an inifinite time.
"""
request_body = interaction.data['request']['body']
parsed_content = json.loads(request_body['string'])
mask_fixture_values(parsed_content, None)
request_body['string'] = json.dumps(parsed_content)
if request_body.get('string'):
parsed_content = json.loads(request_body['string'])
mask_fixture_values(parsed_content, None)
request_body['string'] = json.dumps(parsed_content)
response_body = interaction.data['response']['body']
parsed_content = json.loads(response_body['string'])
mask_fixture_values(parsed_content, None)
response_body['string'] = json.dumps(parsed_content)
if response_body.get('string'):
parsed_content = json.loads(response_body['string'])
mask_fixture_values(parsed_content, None)
response_body['string'] = json.dumps(parsed_content)

View File

@ -12,6 +12,7 @@
import betamax
import json
import mock
from requests import models
import testtools
@ -174,3 +175,24 @@ class TestBetamaxHooks(testtools.TestCase):
u'dummy')
self.assertEqual(
request_content['auth']['tenantName'], u'dummy')
@mock.patch('keystoneauth1.fixture.hooks.mask_fixture_values')
def test_pre_record_hook_empty_body(self, mask_fixture_values):
interaction = mock.Mock()
interaction.data = {
'request': {
'body': {
'encoding': 'utf-8',
'string': '',
},
},
'response': {
'body': {
'encoding': 'utf-8',
'string': '',
},
},
}
hooks.pre_record_hook(interaction, mock.Mock())
self.assertFalse(mask_fixture_values.called)