34cca0c4d9
Convert to the requests library to allow for more sophisticated response handling to be added. Want to allow for applications to override the response handling in certain cases where the default is incorrect. Handling of urlopen responses results in version specific handling to ensure correct behaviour across multiple versions of python. Changing to use the requests package, provides a higher level interface and removes some of the version specific handling for exceptions. Change-Id: I5369a0d35be4bf8b3b197a51e60aba21b5742cc7 Depends-On: Iabd70aa457ceb4dbc147d7cbaeec913148cb3b56
47 lines
1.9 KiB
Python
47 lines
1.9 KiB
Python
from mock import patch
|
|
|
|
import jenkins
|
|
from tests.base import JenkinsTestBase
|
|
from tests.helper import build_response_mock
|
|
|
|
|
|
class JenkinsVersionTest(JenkinsTestBase):
|
|
|
|
@patch('jenkins.requests.Session.send', autospec=True)
|
|
def test_some_version(self, session_send_mock):
|
|
session_send_mock.return_value = build_response_mock(
|
|
200, headers={'X-Jenkins': 'Version42', 'Content-Length': 0})
|
|
self.assertEqual(self.j.get_version(), 'Version42')
|
|
|
|
@patch('jenkins.requests.Session.send', autospec=True)
|
|
def test_raise_HTTPError(self, session_send_mock):
|
|
session_send_mock.side_effect = iter([
|
|
build_response_mock(404, reason="Not Found"), # crumb
|
|
build_response_mock(499, reason="Unhandled Error"), # request
|
|
])
|
|
|
|
with self.assertRaises(jenkins.BadHTTPException) as context_manager:
|
|
self.j.get_version()
|
|
self.assertEqual(
|
|
str(context_manager.exception),
|
|
'Error communicating with server[{0}/]'.format(self.base_url))
|
|
|
|
@patch('jenkins.requests.Session.send', autospec=True)
|
|
def test_raise_BadStatusLine(self, session_send_mock):
|
|
session_send_mock.side_effect = jenkins.BadStatusLine('not a valid status line')
|
|
with self.assertRaises(jenkins.BadHTTPException) as context_manager:
|
|
self.j.get_version()
|
|
self.assertEqual(
|
|
str(context_manager.exception),
|
|
'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):
|
|
session_send_mock.return_value = build_response_mock(0)
|
|
with self.assertRaises(jenkins.EmptyResponseException) as context_manager:
|
|
self.j.get_version()
|
|
self.assertEqual(
|
|
str(context_manager.exception),
|
|
'Error communicating with server[{0}/]:'
|
|
' empty response'.format(self.base_url))
|