Add decode to utf8 for python 3.x

Before this patch python 3.x would cause TypeError's when communicating
to the jenkins server.

- Added Python 3 support for get_version

Change-Id: I24f0cd46663e5e8b3a06f52e1e0c57dd99a22c5f
This commit is contained in:
Richard Pijnenburg 2015-05-14 11:32:12 +02:00 committed by Darragh Bailey
parent af7857ed70
commit 5b29a91ded
2 changed files with 17 additions and 6 deletions

View File

@ -198,7 +198,7 @@ class Jenkins(object):
except (NotFoundException, EmptyResponseException):
self.crumb = False
else:
self.crumb = json.loads(response.decode('utf-8'))
self.crumb = json.loads(response)
if self.crumb:
req.add_header(self.crumb['crumbRequestField'], self.crumb['crumb'])
@ -282,7 +282,7 @@ class Jenkins(object):
raise EmptyResponseException(
"Error communicating with server[%s]: "
"empty response" % self.server)
return response
return response.decode('utf-8')
except HTTPError as e:
# Jenkins's funky authentication means its nigh impossible to
# distinguish errors.
@ -415,7 +415,13 @@ class Jenkins(object):
raise EmptyResponseException(
"Error communicating with server[%s]: "
"empty response" % self.server)
return response.info().getheader('X-Jenkins')
if six.PY2:
return response.info().getheader('X-Jenkins')
if six.PY3:
return response.getheader('X-Jenkins')
except (HTTPError, BadStatusLine):
raise BadHTTPException("Error communicating with server[%s]"
% self.server)

View File

@ -76,7 +76,7 @@ class JenkinsTest(unittest.TestCase):
j = jenkins.Jenkins('http://example.com', long_str, long_str)
self.assertNotIn(b"\n", j.auth)
self.assertEqual(j.auth.decode(), 'Basic %s' % (
self.assertEqual(j.auth.decode('utf-8'), 'Basic %s' % (
long_str_b64 + 'Om' + long_str_b64[2:] + 'YQ=='))
def test_constructor_default_timeout(self):
@ -162,7 +162,7 @@ class JenkinsTest(unittest.TestCase):
self.assertEqual(
jenkins_mock.call_args[0][0].get_full_url(),
'http://example.com/job/TestJob')
self.assertEqual(response, json.dumps(data).encode('utf-8'))
self.assertEqual(response, json.dumps(data))
self.assertEqual(j.crumb, crumb_data)
self.assertEqual(request.headers['.crumb'], crumb_data['crumb'])
@ -631,7 +631,12 @@ class JenkinsTest(unittest.TestCase):
j = jenkins.Jenkins('http://example.com/', 'test', 'test')
mock_response = Mock()
config = {'info.return_value.getheader.return_value': 'Version42'}
if six.PY2:
config = {'info.return_value.getheader.return_value': 'Version42'}
if six.PY3:
config = {'getheader.return_value': 'Version42'}
mock_response.configure_mock(**config)
urlopen_mock.side_effect = [mock_response]
self.assertEqual(j.get_version(), 'Version42')