Skip to content

Commit 2bdfaaa

Browse files
committed
Merge pull request #6 from jrconlin/bug/04
feat: Add tests for GCM compatibility
2 parents 9578a49 + 66227f9 commit 2bdfaaa

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

pywebpush/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def get(self, key, default=None):
4141
except KeyError:
4242
return default
4343

44+
def update(self, data):
45+
for key in data:
46+
self.__setitem__(key, data[key])
47+
4448

4549
class WebPusher:
4650
"""WebPusher encrypts a data block using HTTP Encrypted Content Encoding
@@ -173,7 +177,6 @@ def send(self, data, headers={}, ttl=0, gcm_key=None, reg_id=None):
173177
})
174178
gcm_endpoint = 'https://android.googleapis.com/gcm/send'
175179
if self.subscription_info['endpoint'].startswith(gcm_endpoint):
176-
177180
if not gcm_key:
178181
raise WebPushException("API key not provided for gcm endpoint")
179182
endpoint = gcm_endpoint
@@ -183,7 +186,8 @@ def send(self, data, headers={}, ttl=0, gcm_key=None, reg_id=None):
183186
reg_ids.append(reg_id)
184187
data = {}
185188
data['registration_ids'] = reg_ids
186-
data['raw_data'] = base64.b64encode(encoded.get('body'))
189+
data['raw_data'] = base64.b64encode(
190+
encoded.get('body')).decode('utf8')
187191
encoded_data = json.dumps(data)
188192
headers.update({
189193
'Authorization': 'key='+gcm_key,

pywebpush/tests/test_webpush.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import json
23
import os
34
import unittest
45

@@ -11,9 +12,9 @@
1112

1213

1314
class WebpushTestCase(unittest.TestCase):
14-
def _gen_subscription_info(self, recv_key):
15+
def _gen_subscription_info(self, recv_key, endpoint="https://example.com"):
1516
return {
16-
"endpoint": "https://example.com/",
17+
"endpoint": endpoint,
1718
"keys": {
1819
'auth': base64.urlsafe_b64encode(os.urandom(16)).strip('='),
1920
'p256dh': base64.urlsafe_b64encode(
@@ -114,3 +115,25 @@ def test_ci_dict(self):
114115
eq_('apple', ci.get("Foo"))
115116
del (ci['FOO'])
116117
eq_(None, ci.get('Foo'))
118+
119+
@patch("requests.post")
120+
def test_gcm(self, mock_post):
121+
recv_key = pyelliptic.ECC(curve="prime256v1")
122+
subscription_info = self._gen_subscription_info(
123+
recv_key,
124+
endpoint="https://android.googleapis.com/gcm/send/regid123")
125+
headers = {"Crypto-Key": "pre-existing",
126+
"Authentication": "bearer vapid"}
127+
data = "Mary had a little lamb"
128+
wp = WebPusher(subscription_info)
129+
self.assertRaises(
130+
WebPushException,
131+
wp.send,
132+
data,
133+
headers)
134+
wp.send(data, headers, gcm_key="gcm_key_value")
135+
pdata = json.loads(mock_post.call_args[1].get('data'))
136+
pheaders = mock_post.call_args[1].get('headers')
137+
eq_(pdata["registration_ids"][0], "regid123")
138+
eq_(pheaders.get("authorization"), "key=gcm_key_value")
139+
eq_(pheaders.get("content-type"), "application/json")

0 commit comments

Comments
 (0)