Add client header option to requests (#61)

* Add header option

* Add header option

* Add unit test for headers

* Add client header

* Remove environ for HEADER

environ does not support dictionaries, so HEADER can't be passed.
This commit is contained in:
mazhead
2019-03-31 10:18:48 +02:00
committed by mtakaki
parent eae51967c4
commit 3af53ce9b6
4 changed files with 34 additions and 6 deletions

View File

@@ -42,12 +42,31 @@ class ConfigurationTest(unittest.TestCase):
self.assertDictEqual(self.configuration.headers, {'X-Cachet-Token': 'token2'}, 'Header was not set correctly')
self.assertEquals(self.configuration.api_url, 'https://demo.cachethq.io/api/v1',
'Cachet API URL was set incorrectly')
self.assertDictEqual(self.configuration.endpoint_header, {'SOME-HEADER': 'SOME-VALUE'}, 'Header is incorrect')
def test_evaluate(self):
def total_seconds():
return 0.1
def request(method, url, timeout=None):
def request(method, url, headers, timeout=None):
response = mock.Mock()
response.status_code = 200
response.elapsed = mock.Mock()
response.elapsed.total_seconds = total_seconds
response.text = '<body>'
return response
sys.modules['requests'].request = request
self.configuration.evaluate()
self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Component status set incorrectly')
def test_evaluate_without_header(self):
def total_seconds():
return 0.1
def request(method, url, headers=None, timeout=None):
response = mock.Mock()
response.status_code = 200
response.elapsed = mock.Mock()
@@ -65,7 +84,7 @@ class ConfigurationTest(unittest.TestCase):
def total_seconds():
return 0.1
def request(method, url, timeout=None):
def request(method, url, headers, timeout=None):
response = mock.Mock()
# We are expecting a 200 response, so this will fail the expectation.
response.status_code = 400
@@ -81,7 +100,7 @@ class ConfigurationTest(unittest.TestCase):
'Component status set incorrectly')
def test_evaluate_with_timeout(self):
def request(method, url, timeout=None):
def request(method, url, headers, timeout=None):
self.assertEquals(method, 'GET', 'Incorrect HTTP method')
self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
self.assertEquals(timeout, 0.010)
@@ -96,7 +115,7 @@ class ConfigurationTest(unittest.TestCase):
self.mock_logger.warning.assert_called_with('Request timed out')
def test_evaluate_with_connection_error(self):
def request(method, url, timeout=None):
def request(method, url, headers, timeout=None):
self.assertEquals(method, 'GET', 'Incorrect HTTP method')
self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
self.assertEquals(timeout, 0.010)
@@ -111,7 +130,7 @@ class ConfigurationTest(unittest.TestCase):
self.mock_logger.warning.assert_called_with('The URL is unreachable: GET http://localhost:8080/swagger')
def test_evaluate_with_http_error(self):
def request(method, url, timeout=None):
def request(method, url, headers, timeout=None):
self.assertEquals(method, 'GET', 'Incorrect HTTP method')
self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
self.assertEquals(timeout, 0.010)