New upstream version 1.3.0
Colin Watson
5 years ago
0 | 0 | language: python |
1 | 1 | python: |
2 | - 2.6 | |
3 | 2 | - 2.7 |
4 | 3 | - pypy |
5 | - 3.3 | |
6 | 4 | - 3.4 |
7 | 5 | - 3.5 |
6 | - 3.6 | |
8 | 7 | install: |
9 | 8 | - "pip install requests" |
10 | 9 | script: nosetests |
0 | 0 | httmock |
1 | 1 | ======= |
2 | 2 | |
3 | A mocking library for `requests` for Python 2.6, 2.7, 3.2, 3.3 and 3.4. | |
3 | A mocking library for `requests` for Python 2.7 and 3.4+. | |
4 | 4 | |
5 | 5 | Installation |
6 | 6 | ------------ |
3 | 3 | import json |
4 | 4 | import re |
5 | 5 | import requests |
6 | from requests import structures | |
6 | from requests import structures, utils | |
7 | 7 | import sys |
8 | 8 | try: |
9 | 9 | import urlparse |
48 | 48 | res._content = content |
49 | 49 | res._content_consumed = content |
50 | 50 | res.headers = structures.CaseInsensitiveDict(headers or {}) |
51 | res.encoding = utils.get_encoding_from_headers(res.headers) | |
51 | 52 | res.reason = reason |
52 | 53 | res.elapsed = datetime.timedelta(elapsed) |
53 | 54 | res.request = request |
105 | 106 | def handler_init_call(handler): |
106 | 107 | setattr(handler, 'call', { |
107 | 108 | 'count': 0, |
108 | 'called': False | |
109 | 'called': False, | |
110 | 'requests': [] | |
109 | 111 | }) |
110 | 112 | |
111 | 113 | |
113 | 115 | if hasattr(handler, 'call'): |
114 | 116 | handler.call.update({ |
115 | 117 | 'count': 0, |
116 | 'called': False | |
118 | 'called': False, | |
119 | 'requests': [] | |
117 | 120 | }) |
118 | 121 | |
119 | 122 | |
123 | 126 | finally: |
124 | 127 | handler.call['count'] += 1 |
125 | 128 | handler.call['called'] = True |
126 | ||
129 | handler.call['requests'].append(args[1]) | |
127 | 130 | |
128 | 131 | def remember_called(func): |
129 | 132 | handler_init_call(func) |
10 | 10 | |
11 | 11 | setup( |
12 | 12 | name='httmock', |
13 | version='1.2.6', | |
13 | version='1.3.0', | |
14 | 14 | description='A mocking library for requests.', |
15 | 15 | author='Patryk Zawadzki', |
16 | 16 | author_email='patrys@room-303.com', |
18 | 18 | py_modules=['httmock'], |
19 | 19 | keywords=['requests', 'testing', 'mock'], |
20 | 20 | classifiers=[ |
21 | 'Programming Language :: Python', | |
21 | 'Programming Language :: Python :: 2', | |
22 | 'Programming Language :: Python :: 3', | |
22 | 23 | 'Intended Audience :: Developers', |
23 | 24 | 'Topic :: Software Development :: Testing', |
24 | 25 | 'Operating System :: OS Independent'], |
0 | # -*- coding: utf-8 -*- | |
0 | 1 | import requests |
1 | 2 | import unittest |
2 | 3 | |
39 | 40 | @remember_called |
40 | 41 | def facebook_mock_count(url, request): |
41 | 42 | return 'Hello from Facebook' |
43 | ||
44 | @urlmatch(netloc=r'(.*\.)?google\.com$', path=r'^/$', method='POST') | |
45 | @remember_called | |
46 | def google_mock_store_requests(url, request): | |
47 | return 'Posting at Google' | |
48 | ||
49 | ||
50 | @all_requests | |
51 | def charset_utf8(url, request): | |
52 | return { | |
53 | 'content': u'Motörhead'.encode('utf-8'), | |
54 | 'status_code': 200, | |
55 | 'headers': { | |
56 | 'Content-Type': 'text/plain; charset=utf-8' | |
57 | } | |
58 | } | |
42 | 59 | |
43 | 60 | |
44 | 61 | def any_mock(url, request): |
111 | 128 | with HTTMock(response_content): |
112 | 129 | self.assertRaises(TypeError, requests.get, 'http://example.com/') |
113 | 130 | |
131 | def test_encoding_from_contenttype(self): | |
132 | with HTTMock(charset_utf8): | |
133 | r = requests.get('http://example.com/') | |
134 | self.assertEqual(r.encoding, 'utf-8') | |
135 | self.assertEqual(r.text, u'Motörhead') | |
136 | self.assertEqual(r.content, r.text.encode('utf-8')) | |
137 | ||
114 | 138 | |
115 | 139 | class DecoratorTest(unittest.TestCase): |
116 | 140 | |
333 | 357 | |
334 | 358 | self.several_calls(1, requests.get, 'http://facebook.com/') |
335 | 359 | self.assertEquals(facebook_mock_count.call['count'], 4) |
360 | ||
361 | def test_store_several_requests(self): | |
362 | with HTTMock(google_mock_store_requests): | |
363 | payload = {"query": "foo"} | |
364 | requests.post('http://google.com', data=payload) | |
365 | ||
366 | self.assertTrue(google_mock_store_requests.call['called']) | |
367 | self.assertEqual(google_mock_store_requests.call['count'], 1) | |
368 | request = google_mock_store_requests.call['requests'][0] | |
369 | self.assertEqual(request.body, 'query=foo') |