From a2c6f6a8ff3ca87d664f9d8854dd0971e89b247e Mon Sep 17 00:00:00 2001 From: Jan Zerebecki Date: Sat, 5 Jan 2019 02:08:49 +0100 Subject: [PATCH] When updating jobs the response body may be empty Other specific requests may need a body. Thus remove the general check for an empty body and add a more specific one where needed. I noticed this because when updating a Jenkins job with Jenkins Job Builder I got an EmptyResponseException even though updating worked correctly. Change-Id: Ie144e6fb0b922a681e6c65ffbe5120da72a495f0 --- jenkins/__init__.py | 12 ++---------- tests/jobs/test_build.py | 2 +- tests/test_jenkins.py | 8 +++----- tests/test_node.py | 5 ++--- tests/test_version.py | 7 +++---- 5 files changed, 11 insertions(+), 23 deletions(-) diff --git a/jenkins/__init__.py b/jenkins/__init__.py index 9717f58..c1d115f 100755 --- a/jenkins/__init__.py +++ b/jenkins/__init__.py @@ -368,6 +368,8 @@ class Jenkins(object): try: response = self.jenkins_open(requests.Request( 'GET', self._build_url(CRUMB_URL)), add_crumb=False) + if not response: + raise EmptyResponseException("Empty response for crumb") except (NotFoundException, EmptyResponseException): self.crumb = False else: @@ -532,16 +534,6 @@ class Jenkins(object): # raise exceptions if occurred response.raise_for_status() - headers = response.headers - if (headers.get('content-length') is None and - headers.get('transfer-encoding') is None and - headers.get('location') is None and - (response.content is None or len(response.content) <= 0)): - # response body should only exist if one of these is provided - raise EmptyResponseException( - "Error communicating with server[%s]: " - "empty response" % self.server) - # Response objects will automatically return unicode encoded # when accessing .text property return response diff --git a/tests/jobs/test_build.py b/tests/jobs/test_build.py index 611925a..ba495c6 100644 --- a/tests/jobs/test_build.py +++ b/tests/jobs/test_build.py @@ -60,7 +60,7 @@ class JenkinsBuildJobTest(JenkinsJobsTestBase): self.assertEqual( str(context_mgr.exception), - 'Error communicating with server[{0}]: empty response'.format( + "Header 'Location' not found in response from server[{0}]".format( self.make_url(''))) @patch('jenkins.requests.Session.send', autospec=True) diff --git a/tests/test_jenkins.py b/tests/test_jenkins.py index 1bc0624..2403ccc 100644 --- a/tests/test_jenkins.py +++ b/tests/test_jenkins.py @@ -108,7 +108,7 @@ class JenkinsMaybeAddCrumbTest(JenkinsTestBase): @patch.object(jenkins.Jenkins, 'jenkins_open') def test_return_empty_response(self, jenkins_mock): "Don't try to create crumb header from an empty response" - jenkins_mock.side_effect = jenkins.EmptyResponseException("empty response") + jenkins_mock.return_value = '' request = jenkins.requests.Request('GET', 'http://example.com/job/TestJob') self.j.maybe_add_crumb(request) @@ -254,11 +254,9 @@ class JenkinsOpenTest(JenkinsTestBase): j = jenkins.Jenkins('http://example.com', 'test', 'test') self.assertTrue(j.wait_for_normal_op(0)) - @patch.object(jenkins.Jenkins, 'jenkins_open', - side_effect=jenkins.EmptyResponseException()) @patch.object(jenkins.Jenkins, 'get_version', - side_effect=jenkins.EmptyResponseException()) - def test_wait_for_normal_op__empty_response(self, version_mock, jenkins_mock): + side_effect=jenkins.BadHTTPException()) + def test_wait_for_normal_op__failed_response(self, version_mock): j = jenkins.Jenkins('http://example.com', 'test', 'test') self.assertFalse(j.wait_for_normal_op(0)) diff --git a/tests/test_node.py b/tests/test_node.py index 012ce83..6214ca4 100644 --- a/tests/test_node.py +++ b/tests/test_node.py @@ -266,11 +266,10 @@ class JenkinsCreateNodeTest(JenkinsNodesTestBase): @requests_mock.Mocker() def test_already_exists(self, req_mock): - req_mock.get(self.make_url(jenkins.CRUMB_URL)) + req_mock.get(self.make_url(jenkins.CRUMB_URL), content=None) req_mock.get( self.make_url('computer/test_node/api/json?depth=0'), - status_code=200, json=self.node_info, - headers={'content-length': '20'} + status_code=200, json=self.node_info ) with self.assertRaises(jenkins.JenkinsException) as context_manager: diff --git a/tests/test_version.py b/tests/test_version.py index 67efe92..cf89550 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -36,11 +36,10 @@ class JenkinsVersionTest(JenkinsTestBase): 'Error communicating with server[{0}/]'.format(self.base_url)) @patch('jenkins.requests.Session.send', autospec=True) - def test_return_empty_response(self, session_send_mock): + def test_return_without_version_header(self, session_send_mock): session_send_mock.return_value = build_response_mock(0) - with self.assertRaises(jenkins.EmptyResponseException) as context_manager: + with self.assertRaises(KeyError) as context_manager: self.j.get_version() self.assertEqual( str(context_manager.exception), - 'Error communicating with server[{0}/]:' - ' empty response'.format(self.base_url)) + "'x-jenkins'")