Use empty crumb on empty response

If jenkins_open fails with an empty response we don't set a crumb. The
rest of the code will figure out the real error later.

Change-Id: Ib1743101b6a474656909fc1cb7989c971a56f187
This commit is contained in:
Guido Günther 2015-02-05 22:38:59 +01:00
parent 5997c70da2
commit b5a11e6c3c
2 changed files with 16 additions and 2 deletions

@ -186,8 +186,7 @@ class Jenkins(object):
try:
response = self.jenkins_open(Request(
self.server + CRUMB_URL), add_crumb=False)
except NotFoundException:
# Don't need crumbs
except (NotFoundException, EmptyResponseException):
self.crumb = False
else:
self.crumb = json.loads(response.decode('utf-8'))

@ -126,6 +126,21 @@ class JenkinsTest(unittest.TestCase):
self.assertEqual(j.crumb, crumb_data)
self.assertEqual(request.headers['.crumb'], crumb_data['crumb'])
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_maybe_add_crumb__empty_response(self, jenkins_mock):
"Don't try to create crumb header from an empty response"
jenkins_mock.side_effect = jenkins.EmptyResponseException("empty response")
j = jenkins.Jenkins('http://example.com/', 'test', 'test')
request = jenkins.Request('http://example.com/job/TestJob')
j.maybe_add_crumb(request)
self.assertEqual(
jenkins_mock.call_args[0][0].get_full_url(),
'http://example.com/crumbIssuer/api/json')
self.assertFalse(j.crumb)
self.assertFalse('.crumb' in request.headers)
@patch('jenkins.urlopen')
def test_jenkins_open(self, jenkins_mock):
crumb_data = {