|
1 | 1 | import base64
|
| 2 | +import json |
2 | 3 | import os
|
3 | 4 | import unittest
|
4 | 5 |
|
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | class WebpushTestCase(unittest.TestCase):
|
14 |
| - def _gen_subscription_info(self, recv_key): |
| 15 | + def _gen_subscription_info(self, recv_key, endpoint="https://example.com"): |
15 | 16 | return {
|
16 |
| - "endpoint": "https://example.com/", |
| 17 | + "endpoint": endpoint, |
17 | 18 | "keys": {
|
18 | 19 | 'auth': base64.urlsafe_b64encode(os.urandom(16)).strip('='),
|
19 | 20 | 'p256dh': base64.urlsafe_b64encode(
|
@@ -114,3 +115,25 @@ def test_ci_dict(self):
|
114 | 115 | eq_('apple', ci.get("Foo"))
|
115 | 116 | del (ci['FOO'])
|
116 | 117 | 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