Skip to content

Commit 4464fb9

Browse files
committed
Merge pull request #11 from jrconlin/bug/10
bug: make header keys case insenstive
2 parents 81b3873 + c01dcb4 commit 4464fb9

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.3.4 (2016-05-17)
2+
bug: make header keys case insenstive
3+
14
## 0.3.3 (2016-05-17)
25
bug: force key string encoding to utf8
36

pywebpush/__init__.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@ class WebPushException(Exception):
1414
pass
1515

1616

17+
class CaseInsensitiveDict(dict):
18+
"""A dictionary that has case-insensitive keys"""
19+
20+
def __init__(self, data={}, **kwargs):
21+
for key in data:
22+
dict.__setitem__(self, key.lower(), data[key])
23+
self.update(kwargs)
24+
25+
def __contains__(self, key):
26+
return dict.__contains__(self, key.lower())
27+
28+
def __setitem__(self, key, value):
29+
dict.__setitem__(self, key.lower(), value)
30+
31+
def __getitem__(self, key):
32+
return dict.__getitem__(self, key.lower())
33+
34+
def __delitem__(self, key):
35+
dict.__delitem__(self, key.lower())
36+
37+
def get(self, key, default=None):
38+
try:
39+
return self.__getitem__(key)
40+
except KeyError:
41+
return default
42+
43+
1744
class WebPusher:
1845
"""WebPusher encrypts a data block using HTTP Encrypted Content Encoding
1946
for WebPush.
@@ -109,12 +136,12 @@ def encode(self, data):
109136
dh=self.receiver_key,
110137
authSecret=self.auth_key)
111138

112-
return {
139+
return CaseInsensitiveDict({
113140
'crypto_key': base64.urlsafe_b64encode(
114141
server_key.get_pubkey()).strip('='),
115142
'salt': base64.urlsafe_b64encode(salt).strip("="),
116143
'body': encrypted,
117-
}
144+
})
118145

119146
def send(self, data, headers={}, ttl=0):
120147
"""Encode and send the data to the Push Service.
@@ -129,6 +156,7 @@ def send(self, data, headers={}, ttl=0):
129156
# Encode the data.
130157
encoded = self.encode(data)
131158
# Append the p256dh to the end of any existing crypto-key
159+
headers = CaseInsensitiveDict(headers)
132160
crypto_key = headers.get("crypto-key", "")
133161
if crypto_key:
134162
crypto_key += ','

pywebpush/tests/test_webpush.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from nose.tools import eq_, ok_
88
import pyelliptic
99

10-
from pywebpush import WebPusher, WebPushException
10+
from pywebpush import WebPusher, WebPushException, CaseInsensitiveDict
1111

1212

1313
class WebpushTestCase(unittest.TestCase):
@@ -94,15 +94,23 @@ def test_encode(self):
9494
def test_send(self, mock_post):
9595
recv_key = pyelliptic.ECC(curve="prime256v1")
9696
subscription_info = self._gen_subscription_info(recv_key)
97-
headers = {"crypto-key": "pre-existing",
98-
"authentication": "bearer vapid"}
97+
headers = {"Crypto-Key": "pre-existing",
98+
"Authentication": "bearer vapid"}
9999
data = "Mary had a little lamb"
100100
WebPusher(subscription_info).send(data, headers)
101101
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
102102
pheaders = mock_post.call_args[1].get('headers')
103103
eq_(pheaders.get('ttl'), 0)
104104
ok_('encryption' in pheaders)
105-
eq_(pheaders.get('authentication'), headers.get('authentication'))
105+
eq_(pheaders.get('AUTHENTICATION'), headers.get('Authentication'))
106106
ckey = pheaders.get('crypto-key')
107107
ok_('pre-existing,' in ckey)
108108
eq_(pheaders.get('content-encoding'), 'aesgcm')
109+
110+
def test_ci_dict(self):
111+
ci = CaseInsensitiveDict({"Foo": "apple", "bar": "banana"})
112+
eq_('apple', ci["foo"])
113+
eq_('apple', ci.get("FOO"))
114+
eq_('apple', ci.get("Foo"))
115+
del (ci['FOO'])
116+
eq_(None, ci.get('Foo'))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from setuptools import find_packages, setup
55

6-
__version__ = "0.3.3"
6+
__version__ = "0.3.4"
77

88

99
def read_from(file):

0 commit comments

Comments
 (0)