Codebase list httmock / upstream/latest
New upstream version 1.3.0 Colin Watson 5 years ago
5 changed file(s) with 46 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
00 language: python
11 python:
2 - 2.6
32 - 2.7
43 - pypy
5 - 3.3
64 - 3.4
75 - 3.5
6 - 3.6
87 install:
98 - "pip install requests"
109 script: nosetests
00 httmock
11 =======
22
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+.
44
55 Installation
66 ------------
33 import json
44 import re
55 import requests
6 from requests import structures
6 from requests import structures, utils
77 import sys
88 try:
99 import urlparse
4848 res._content = content
4949 res._content_consumed = content
5050 res.headers = structures.CaseInsensitiveDict(headers or {})
51 res.encoding = utils.get_encoding_from_headers(res.headers)
5152 res.reason = reason
5253 res.elapsed = datetime.timedelta(elapsed)
5354 res.request = request
105106 def handler_init_call(handler):
106107 setattr(handler, 'call', {
107108 'count': 0,
108 'called': False
109 'called': False,
110 'requests': []
109111 })
110112
111113
113115 if hasattr(handler, 'call'):
114116 handler.call.update({
115117 'count': 0,
116 'called': False
118 'called': False,
119 'requests': []
117120 })
118121
119122
123126 finally:
124127 handler.call['count'] += 1
125128 handler.call['called'] = True
126
129 handler.call['requests'].append(args[1])
127130
128131 def remember_called(func):
129132 handler_init_call(func)
1010
1111 setup(
1212 name='httmock',
13 version='1.2.6',
13 version='1.3.0',
1414 description='A mocking library for requests.',
1515 author='Patryk Zawadzki',
1616 author_email='patrys@room-303.com',
1818 py_modules=['httmock'],
1919 keywords=['requests', 'testing', 'mock'],
2020 classifiers=[
21 'Programming Language :: Python',
21 'Programming Language :: Python :: 2',
22 'Programming Language :: Python :: 3',
2223 'Intended Audience :: Developers',
2324 'Topic :: Software Development :: Testing',
2425 'Operating System :: OS Independent'],
0 # -*- coding: utf-8 -*-
01 import requests
12 import unittest
23
3940 @remember_called
4041 def facebook_mock_count(url, request):
4142 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 }
4259
4360
4461 def any_mock(url, request):
111128 with HTTMock(response_content):
112129 self.assertRaises(TypeError, requests.get, 'http://example.com/')
113130
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
114138
115139 class DecoratorTest(unittest.TestCase):
116140
333357
334358 self.several_calls(1, requests.get, 'http://facebook.com/')
335359 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')