Skip to content

Commit 82af32a

Browse files
authored
Merge pull request #23 from web-push-libs/bug/18
Bug/18
2 parents 1a2e565 + 79f3bca commit 82af32a

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

pywebpush/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def encode(self, data):
152152
'body': encrypted,
153153
})
154154

155-
def send(self, data, headers={}, ttl=0, gcm_key=None, reg_id=None):
155+
def send(self, data, headers=None, ttl=0, gcm_key=None, reg_id=None):
156156
"""Encode and send the data to the Push Service.
157157
158158
:param data: A serialized block of data (see encode() ).
@@ -167,6 +167,8 @@ def send(self, data, headers={}, ttl=0, gcm_key=None, reg_id=None):
167167
168168
"""
169169
# Encode the data.
170+
if headers is None:
171+
headers = dict()
170172
encoded = self.encode(data)
171173
# Append the p256dh to the end of any existing crypto-key
172174
headers = CaseInsensitiveDict(headers)
@@ -192,7 +194,7 @@ def send(self, data, headers={}, ttl=0, gcm_key=None, reg_id=None):
192194
if not reg_id:
193195
reg_id = self.subscription_info['endpoint'].rsplit('/', 1)[-1]
194196
reg_ids.append(reg_id)
195-
data = {}
197+
data = dict()
196198
data['registration_ids'] = reg_ids
197199
data['raw_data'] = base64.b64encode(
198200
encoded.get('body')).decode('utf8')
@@ -206,7 +208,7 @@ def send(self, data, headers={}, ttl=0, gcm_key=None, reg_id=None):
206208
endpoint = self.subscription_info['endpoint']
207209

208210
if 'ttl' not in headers or ttl:
209-
headers['ttl'] = str(ttl)
211+
headers['ttl'] = str(ttl or 0)
210212
# Additionally useful headers:
211213
# Authorization / Crypto-Key (VAPID headers)
212214
return self._post(endpoint, encoded_data, headers)

pywebpush/tests/test_webpush.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,25 @@ def test_send(self, mock_post):
102102
WebPusher(subscription_info).send(data, headers)
103103
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
104104
pheaders = mock_post.call_args[1].get('headers')
105-
eq_(pheaders.get('ttl'), "0")
105+
eq_(pheaders.get('ttl'), '0')
106106
ok_('encryption' in pheaders)
107107
eq_(pheaders.get('AUTHENTICATION'), headers.get('Authentication'))
108108
ckey = pheaders.get('crypto-key')
109109
ok_('pre-existing' in ckey)
110110
eq_(pheaders.get('content-encoding'), 'aesgcm')
111111

112+
@patch("requests.post")
113+
def test_send_no_headers(self, mock_post):
114+
recv_key = pyelliptic.ECC(curve="prime256v1")
115+
subscription_info = self._gen_subscription_info(recv_key)
116+
data = "Mary had a little lamb"
117+
WebPusher(subscription_info).send(data)
118+
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
119+
pheaders = mock_post.call_args[1].get('headers')
120+
eq_(pheaders.get('ttl'), '0')
121+
ok_('encryption' in pheaders)
122+
eq_(pheaders.get('content-encoding'), 'aesgcm')
123+
112124
def test_ci_dict(self):
113125
ci = CaseInsensitiveDict({"Foo": "apple", "bar": "banana"})
114126
eq_('apple', ci["foo"])

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
http-ece==0.5.0
2-
python-jose==1.2.0
3-
requests==2.11.*
2+
python-jose
3+
requests
44
flake8

0 commit comments

Comments
 (0)