|
3 | 3 | from __future__ import print_function, absolute_import, division
|
4 | 4 |
|
5 | 5 | import unittest
|
| 6 | +import re |
6 | 7 |
|
7 | 8 | from pusher import Pusher
|
8 | 9 | from pusher.http import Request
|
@@ -41,13 +42,34 @@ def test_post_signature_generation(self):
|
41 | 42 | }
|
42 | 43 |
|
43 | 44 | with mock.patch('time.time', return_value=1000):
|
44 |
| - # patching this, because json can be unambiguously parsed, but not |
| 45 | + # patching this, because json can be unambiguously parsed, but not |
45 | 46 | # unambiguously generated (think whitespace).
|
46 | 47 | with mock.patch('json.dumps', return_value='{"foo": "bar"}') as json_dumps_mock:
|
47 | 48 | req = Request(conf, u'POST', u'/some/obscure/api', {u'foo': u'bar'})
|
48 | 49 | self.assertEqual(req.query_params, expected)
|
49 | 50 |
|
50 | 51 | json_dumps_mock.assert_called_once_with({u"foo": u"bar"})
|
51 | 52 |
|
| 53 | + # Copied wholesale from https://github.com/python/cpython/blob/2d305e1c46abfcd609bf8b2dff8d2065e6af8ab2/Lib/unittest/case.py#L1279-L1289 |
| 54 | + # This can be removed when we no longer support Python 2.6 |
| 55 | + def assertRegexpMatches(self, text, expected_regex, msg=None): |
| 56 | + """Fail the test unless the text matches the regular expression.""" |
| 57 | + if isinstance(expected_regex, (str, bytes)): |
| 58 | + assert expected_regex, "expected_regex must not be empty." |
| 59 | + expected_regex = re.compile(expected_regex) |
| 60 | + if not expected_regex.search(text): |
| 61 | + standardMsg = "Regex didn't match: %r not found in %r" % ( |
| 62 | + expected_regex.pattern, text) |
| 63 | + # _formatMessage ensures the longMessage option is respected |
| 64 | + msg = self._formatMessage(msg, standardMsg) |
| 65 | + raise self.failureException(msg) |
| 66 | + |
| 67 | + def test_x_pusher_library_header(self): |
| 68 | + conf = Pusher.from_url(u'http://key:secret@somehost/apps/4') |
| 69 | + req = Request(conf, u'GET', u'/some/obscure/api', {u'foo': u'bar'}) |
| 70 | + self.assertTrue('X-Pusher-Library' in req.headers) |
| 71 | + pusherLib = req.headers['X-Pusher-Library'] |
| 72 | + self.assertRegexpMatches(pusherLib, r'^pusher-http-python \d+(\.\d+)+(rc\d+)?$') |
| 73 | + |
52 | 74 | if __name__ == '__main__':
|
53 | 75 | unittest.main()
|
0 commit comments