From 8d58a3a36160a93d787c32907475ccd4ff75b5be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 14 Aug 2017 22:20:57 +0300 Subject: [PATCH] Encode webhook POST body as utf-8. Fixes #130 --- hc/api/tests/test_notify.py | 18 +++++++++++++++++- hc/api/transports.py | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index 3447abc6..5e0acd14 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -1,3 +1,5 @@ +# coding: utf-8 + from datetime import timedelta as td import json @@ -7,6 +9,7 @@ from hc.api.models import Channel, Check, Notification from hc.test import BaseTestCase from mock import patch from requests.exceptions import ConnectionError, Timeout +from six import binary_type class NotifyTestCase(BaseTestCase): @@ -100,7 +103,8 @@ class NotifyTestCase(BaseTestCase): self.assertEqual(args[1], "http://example.com") # spaces should not have been urlencoded: - self.assertTrue(kwargs["data"].startswith("The Time Is 2")) + payload = kwargs["data"].decode("utf-8") + self.assertTrue(payload.startswith("The Time Is 2")) @patch("hc.api.transports.requests.request") def test_webhooks_dollarsign_escaping(self, mock_get): @@ -129,6 +133,18 @@ class NotifyTestCase(BaseTestCase): "get", "http://bar", headers={"User-Agent": "healthchecks.io"}, timeout=5) + @patch("hc.api.transports.requests.request") + def test_webhooks_handle_unicode_post_body(self, mock_request): + template = u"http://example.com\n\n(╯°□°)╯︵ ┻━┻" + self._setup_data("webhook", template) + self.check.save() + + self.channel.notify(self.check) + args, kwargs = mock_request.call_args + + # unicode should be encoded into utf-8 + self.assertTrue(isinstance(kwargs["data"], binary_type)) + def test_email(self): self._setup_data("email", "alice@example.org") self.channel.notify(self.check) diff --git a/hc/api/transports.py b/hc/api/transports.py index cf2eb362..fc3cdc78 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -156,7 +156,7 @@ class Webhook(HttpTransport): url = self.prepare(url, check, urlencode=True) if self.channel.post_data: payload = self.prepare(self.channel.post_data, check) - return self.post(url, data=payload) + return self.post(url, data=payload.encode("utf-8")) else: return self.get(url)