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'")