forked from GithubBackups/healthchecks
GET webhooks support custom headers too. Can override the User-Agent header.
This commit is contained in:
parent
fbf28e4038
commit
620d8523d7
@ -311,7 +311,7 @@ class Channel(models.Model):
|
|||||||
return parts[0]
|
return parts[0]
|
||||||
|
|
||||||
doc = json.loads(self.value)
|
doc = json.loads(self.value)
|
||||||
return doc["url_down"]
|
return doc.get("url_down")
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -322,7 +322,7 @@ class Channel(models.Model):
|
|||||||
return parts[1] if len(parts) > 1 else ""
|
return parts[1] if len(parts) > 1 else ""
|
||||||
|
|
||||||
doc = json.loads(self.value)
|
doc = json.loads(self.value)
|
||||||
return doc["url_up"]
|
return doc.get("url_up")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def post_data(self):
|
def post_data(self):
|
||||||
@ -332,16 +332,16 @@ class Channel(models.Model):
|
|||||||
return parts[2] if len(parts) > 2 else ""
|
return parts[2] if len(parts) > 2 else ""
|
||||||
|
|
||||||
doc = json.loads(self.value)
|
doc = json.loads(self.value)
|
||||||
return doc["post_data"]
|
return doc.get("post_data")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def headers(self):
|
def headers(self):
|
||||||
assert self.kind == "webhook"
|
assert self.kind == "webhook"
|
||||||
if not self.value.startswith("{"):
|
if not self.value.startswith("{"):
|
||||||
return ""
|
return {}
|
||||||
|
|
||||||
doc = json.loads(self.value)
|
doc = json.loads(self.value)
|
||||||
return doc["headers"]
|
return doc.get("headers", {})
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def slack_team(self):
|
def slack_team(self):
|
||||||
|
@ -147,35 +147,34 @@ class NotifyTestCase(BaseTestCase):
|
|||||||
|
|
||||||
@patch("hc.api.transports.requests.request")
|
@patch("hc.api.transports.requests.request")
|
||||||
def test_webhooks_handle_json_value(self, mock_request):
|
def test_webhooks_handle_json_value(self, mock_request):
|
||||||
self._setup_data("webhook", '{"url_down": "http://foo.com", '
|
definition = {"url_down": "http://foo.com"}
|
||||||
'"url_up": "", "post_data": "", "headers": ""}')
|
self._setup_data("webhook", json.dumps(definition))
|
||||||
self.channel.notify(self.check)
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
headers = {
|
headers = {"User-Agent": "healthchecks.io"}
|
||||||
"User-Agent": "healthchecks.io"
|
|
||||||
}
|
|
||||||
mock_request.assert_called_with(
|
mock_request.assert_called_with(
|
||||||
"get", "http://foo.com", headers=headers,
|
"get", "http://foo.com", headers=headers, timeout=5)
|
||||||
timeout=5)
|
|
||||||
|
|
||||||
@patch("hc.api.transports.requests.request")
|
@patch("hc.api.transports.requests.request")
|
||||||
def test_webhooks_handle_json_up_event(self, mock_request):
|
def test_webhooks_handle_json_up_event(self, mock_request):
|
||||||
self._setup_data("webhook", '{"url_down": "", '
|
definition = {"url_up": "http://bar"}
|
||||||
'"url_up": "http://bar", "post_data": "", "headers": ""}', status="up")
|
|
||||||
|
self._setup_data("webhook", json.dumps(definition), status="up")
|
||||||
self.channel.notify(self.check)
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
headers = {
|
headers = {"User-Agent": "healthchecks.io"}
|
||||||
"User-Agent": "healthchecks.io"
|
|
||||||
}
|
|
||||||
mock_request.assert_called_with(
|
mock_request.assert_called_with(
|
||||||
"get", "http://bar", headers=headers,
|
"get", "http://bar", headers=headers, timeout=5)
|
||||||
timeout=5)
|
|
||||||
|
|
||||||
@patch("hc.api.transports.requests.request")
|
@patch("hc.api.transports.requests.request")
|
||||||
def test_webhooks_handle_headers(self, mock_request):
|
def test_webhooks_handle_post_headers(self, mock_request):
|
||||||
self._setup_data("webhook", '{"url_down": "http://foo.com", '
|
definition = {
|
||||||
'"url_up": "", "post_data": "data", '
|
"url_down": "http://foo.com",
|
||||||
'"headers": {"Content-Type": "application/json"}}')
|
"post_data": "data",
|
||||||
|
"headers": {"Content-Type": "application/json"}
|
||||||
|
}
|
||||||
|
|
||||||
|
self._setup_data("webhook", json.dumps(definition))
|
||||||
self.channel.notify(self.check)
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
@ -185,6 +184,37 @@ class NotifyTestCase(BaseTestCase):
|
|||||||
mock_request.assert_called_with(
|
mock_request.assert_called_with(
|
||||||
"post", "http://foo.com", data=b"data", headers=headers, timeout=5)
|
"post", "http://foo.com", data=b"data", headers=headers, timeout=5)
|
||||||
|
|
||||||
|
@patch("hc.api.transports.requests.request")
|
||||||
|
def test_webhooks_handle_get_headers(self, mock_request):
|
||||||
|
definition = {
|
||||||
|
"url_down": "http://foo.com",
|
||||||
|
"headers": {"Content-Type": "application/json"}
|
||||||
|
}
|
||||||
|
|
||||||
|
self._setup_data("webhook", json.dumps(definition))
|
||||||
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"User-Agent": "healthchecks.io",
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
mock_request.assert_called_with(
|
||||||
|
"get", "http://foo.com", headers=headers, timeout=5)
|
||||||
|
|
||||||
|
@patch("hc.api.transports.requests.request")
|
||||||
|
def test_webhooks_allow_user_agent_override(self, mock_request):
|
||||||
|
definition = {
|
||||||
|
"url_down": "http://foo.com",
|
||||||
|
"headers": {"User-Agent": "My-Agent"}
|
||||||
|
}
|
||||||
|
|
||||||
|
self._setup_data("webhook", json.dumps(definition))
|
||||||
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
|
headers = {"User-Agent": "My-Agent"}
|
||||||
|
mock_request.assert_called_with(
|
||||||
|
"get", "http://foo.com", headers=headers, timeout=5)
|
||||||
|
|
||||||
def test_email(self):
|
def test_email(self):
|
||||||
self._setup_data("email", "alice@example.org")
|
self._setup_data("email", "alice@example.org")
|
||||||
self.channel.notify(self.check)
|
self.channel.notify(self.check)
|
||||||
|
@ -80,10 +80,10 @@ class HttpTransport(Transport):
|
|||||||
def _request(cls, method, url, **kwargs):
|
def _request(cls, method, url, **kwargs):
|
||||||
try:
|
try:
|
||||||
options = dict(kwargs)
|
options = dict(kwargs)
|
||||||
|
options["timeout"] = 5
|
||||||
if "headers" not in options:
|
if "headers" not in options:
|
||||||
options["headers"] = {}
|
options["headers"] = {}
|
||||||
|
if "User-Agent" not in options["headers"]:
|
||||||
options["timeout"] = 5
|
|
||||||
options["headers"]["User-Agent"] = "healthchecks.io"
|
options["headers"]["User-Agent"] = "healthchecks.io"
|
||||||
|
|
||||||
r = requests.request(method, url, **options)
|
r = requests.request(method, url, **options)
|
||||||
@ -96,10 +96,10 @@ class HttpTransport(Transport):
|
|||||||
return "Connection failed"
|
return "Connection failed"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get(cls, url):
|
def get(cls, url, **kwargs):
|
||||||
# Make 3 attempts--
|
# Make 3 attempts--
|
||||||
for x in range(0, 3):
|
for x in range(0, 3):
|
||||||
error = cls._request("get", url)
|
error = cls._request("get", url, **kwargs)
|
||||||
if error is None:
|
if error is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -166,14 +166,12 @@ class Webhook(HttpTransport):
|
|||||||
assert url
|
assert url
|
||||||
|
|
||||||
url = self.prepare(url, check, urlencode=True)
|
url = self.prepare(url, check, urlencode=True)
|
||||||
|
headers = self.channel.headers
|
||||||
if self.channel.post_data:
|
if self.channel.post_data:
|
||||||
payload = self.prepare(self.channel.post_data, check)
|
payload = self.prepare(self.channel.post_data, check)
|
||||||
headers = {}
|
|
||||||
if self.channel.headers:
|
|
||||||
headers = self.channel.headers
|
|
||||||
return self.post(url, data=payload.encode("utf-8"), headers=headers)
|
return self.post(url, data=payload.encode("utf-8"), headers=headers)
|
||||||
else:
|
else:
|
||||||
return self.get(url)
|
return self.get(url, headers=headers)
|
||||||
|
|
||||||
|
|
||||||
class Slack(HttpTransport):
|
class Slack(HttpTransport):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user