From 332505e1827676806478ab36c9f3f5e5bb3f9afe Mon Sep 17 00:00:00 2001 From: jrconlin Date: Mon, 6 Feb 2017 10:11:34 -0800 Subject: [PATCH] bug: APNs library requires parameters to be strings closes #797 --- autopush/router/apns2.py | 16 ++++++++++++---- autopush/tests/test_router.py | 4 ++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/autopush/router/apns2.py b/autopush/router/apns2.py index 85905f1f..cc7c0b11 100644 --- a/autopush/router/apns2.py +++ b/autopush/router/apns2.py @@ -11,10 +11,15 @@ SANDBOX = 'api.development.push.apple.com' SERVER = 'api.push.apple.com' -APNS_PRIORITY_IMMEDIATE = 10 -APNS_PRIORITY_LOW = 5 APNS_MAX_CONNECTIONS = 20 +# These values are defined by APNs as header values that should be sent. +# The hyper library requires that all header values be strings. +# These values should be considered "opaque" to APNs. +# see https://developer.apple.com/search/?q=%22apns-priority%22 +APNS_PRIORITY_IMMEDIATE = '10' +APNS_PRIORITY_LOW = '5' + class APNSException(Exception): pass @@ -101,13 +106,16 @@ def send(self, router_token, payload, apns_id, """ body = json.dumps(payload) priority = APNS_PRIORITY_IMMEDIATE if priority else APNS_PRIORITY_LOW + # NOTE: Hyper requires that all header values be strings. 'Priority' + # is a integer string, which may be "simplified" and cause an error. + # The added str() function safeguards against that. headers = { 'apns-id': apns_id, - 'apns-priority': priority, + 'apns-priority': str(priority), 'apns-topic': topic or self.topic, } if exp: - headers['apns-expiration'] = exp + headers['apns-expiration'] = str(exp) url = '/3/device/' + router_token connection = self._get_connection() try: diff --git a/autopush/tests/test_router.py b/autopush/tests/test_router.py index d7f65e39..dce99fea 100644 --- a/autopush/tests/test_router.py +++ b/autopush/tests/test_router.py @@ -200,9 +200,9 @@ def test_route_low_priority_notification(self): ok_(self.mock_connection.request.called) body = self.mock_connection.request.call_args[1] headers = body['headers'] - eq_(headers, {'apns-expiration': exp, + eq_(headers, {'apns-expiration': str(exp), 'apns-topic': 'com.example.SomeApp', - 'apns-priority': 5, + 'apns-priority': '5', 'apns-id': 'apnsid'}) @inlineCallbacks