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

@@ -19,6 +19,8 @@ This project is available at PyPI: [https://pypi.python.org/pypi/cachet-url-moni
endpoint: endpoint:
url: http://www.google.com url: http://www.google.com
method: GET method: GET
header:
SOME-HEADER: SOME-VALUE
timeout: 1 # seconds timeout: 1 # seconds
expectation: expectation:
- type: HTTP_STATUS - type: HTTP_STATUS
@@ -44,6 +46,7 @@ frequency: 30
- **endpoint**, the configuration about the URL that will be monitored. - **endpoint**, the configuration about the URL that will be monitored.
- **url**, the URL that is going to be monitored. - **url**, the URL that is going to be monitored.
- **method**, the HTTP method that will be used by the monitor. - **method**, the HTTP method that will be used by the monitor.
- **header**, client header passed to the request. Remove if you do not want to pass a header.
- **timeout**, how long we'll wait to consider the request failed. The unit of it is seconds. - **timeout**, how long we'll wait to consider the request failed. The unit of it is seconds.
- **expectation**, the list of expectations set for the URL. - **expectation**, the list of expectations set for the URL.
- **HTTP_STATUS**, we will verify if the response status code falls into the expected range. Please keep in mind the range is inclusive on the first number and exclusive on the second number. If just one value is specified, it will default to only the given value, for example `200` will be converted to `200-201`. - **HTTP_STATUS**, we will verify if the response status code falls into the expected range. Please keep in mind the range is inclusive on the first number and exclusive on the second number. If just one value is specified, it will default to only the given value, for example `200` will be converted to `200-201`.

View File

@@ -97,6 +97,7 @@ class Configuration(object):
self.endpoint_url = os.environ.get('ENDPOINT_URL') or self.data['endpoint']['url'] self.endpoint_url = os.environ.get('ENDPOINT_URL') or self.data['endpoint']['url']
self.endpoint_url = normalize_url(self.endpoint_url) self.endpoint_url = normalize_url(self.endpoint_url)
self.endpoint_timeout = os.environ.get('ENDPOINT_TIMEOUT') or self.data['endpoint'].get('timeout') or 1 self.endpoint_timeout = os.environ.get('ENDPOINT_TIMEOUT') or self.data['endpoint'].get('timeout') or 1
self.endpoint_header = self.data['endpoint'].get('header') or None
self.allowed_fails = os.environ.get('ALLOWED_FAILS') or self.data['endpoint'].get('allowed_fails') or 0 self.allowed_fails = os.environ.get('ALLOWED_FAILS') or self.data['endpoint'].get('allowed_fails') or 0
self.api_url = os.environ.get('CACHET_API_URL') or self.data['cachet']['api_url'] self.api_url = os.environ.get('CACHET_API_URL') or self.data['cachet']['api_url']
@@ -172,7 +173,10 @@ class Configuration(object):
according to the expectation results. according to the expectation results.
""" """
try: try:
self.request = requests.request(self.endpoint_method, self.endpoint_url, timeout=self.endpoint_timeout) if self.endpoint_header is not None:
self.request = requests.request(self.endpoint_method, self.endpoint_url, timeout=self.endpoint_timeout, headers=self.endpoint_header)
else:
self.request = requests.request(self.endpoint_method, self.endpoint_url, timeout=self.endpoint_timeout)
self.current_timestamp = int(time.time()) self.current_timestamp = int(time.time())
except requests.ConnectionError: except requests.ConnectionError:
self.message = 'The URL is unreachable: %s %s' % (self.endpoint_method, self.endpoint_url) self.message = 'The URL is unreachable: %s %s' % (self.endpoint_method, self.endpoint_url)

View File

@@ -1,6 +1,8 @@
endpoint: endpoint:
url: http://localhost:8080/swagger url: http://localhost:8080/swagger
method: GET method: GET
header:
SOME-HEADER: SOME-VALUE
timeout: 0.01 timeout: 0.01
expectation: expectation:
- type: HTTP_STATUS - type: HTTP_STATUS

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