METADATA 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. Metadata-Version: 2.1
  2. Name: aioresponses
  3. Version: 0.7.6
  4. Summary: Mock out requests made by ClientSession from aiohttp package
  5. Home-page: https://github.com/pnuckowski/aioresponses
  6. Author: Pawel Nuckowski
  7. Author-email: p.nuckowski@gmail.com
  8. Classifier: Development Status :: 4 - Beta
  9. Classifier: Intended Audience :: Developers
  10. Classifier: Operating System :: OS Independent
  11. Classifier: Topic :: Internet :: WWW/HTTP
  12. Classifier: Topic :: Software Development :: Testing
  13. Classifier: Topic :: Software Development :: Testing :: Mocking
  14. Classifier: License :: OSI Approved :: MIT License
  15. Classifier: Natural Language :: English
  16. Classifier: Programming Language :: Python :: 3
  17. Classifier: Programming Language :: Python :: 3.7
  18. Classifier: Programming Language :: Python :: 3.8
  19. Classifier: Programming Language :: Python :: 3.9
  20. Classifier: Programming Language :: Python :: 3.10
  21. Classifier: Programming Language :: Python :: 3.11
  22. License-File: LICENSE
  23. License-File: AUTHORS
  24. License-File: AUTHORS.rst
  25. Requires-Dist: aiohttp (<4.0.0,>=3.3.0)
  26. ===============================
  27. aioresponses
  28. ===============================
  29. .. image:: https://travis-ci.org/pnuckowski/aioresponses.svg?branch=master
  30. :target: https://travis-ci.org/pnuckowski/aioresponses
  31. .. image:: https://coveralls.io/repos/github/pnuckowski/aioresponses/badge.svg?branch=master
  32. :target: https://coveralls.io/github/pnuckowski/aioresponses?branch=master
  33. .. image:: https://landscape.io/github/pnuckowski/aioresponses/master/landscape.svg?style=flat
  34. :target: https://landscape.io/github/pnuckowski/aioresponses/master
  35. :alt: Code Health
  36. .. image:: https://pyup.io/repos/github/pnuckowski/aioresponses/shield.svg
  37. :target: https://pyup.io/repos/github/pnuckowski/aioresponses/
  38. :alt: Updates
  39. .. image:: https://img.shields.io/pypi/v/aioresponses.svg
  40. :target: https://pypi.python.org/pypi/aioresponses
  41. .. image:: https://readthedocs.org/projects/aioresponses/badge/?version=latest
  42. :target: https://aioresponses.readthedocs.io/en/latest/?badge=latest
  43. :alt: Documentation Status
  44. Aioresponses is a helper to mock/fake web requests in python aiohttp package.
  45. For *requests* module there are a lot of packages that help us with testing (eg. *httpretty*, *responses*, *requests-mock*).
  46. When it comes to testing asynchronous HTTP requests it is a bit harder (at least at the beginning).
  47. The purpose of this package is to provide an easy way to test asynchronous HTTP requests.
  48. Installing
  49. ----------
  50. .. code:: bash
  51. $ pip install aioresponses
  52. Supported versions
  53. ------------------
  54. - Python 3.7+
  55. - aiohttp>=3.3.0,<4.0.0
  56. Usage
  57. --------
  58. To mock out HTTP request use *aioresponses* as a method decorator or as a context manager.
  59. Response *status* code, *body*, *payload* (for json response) and *headers* can be mocked.
  60. Supported HTTP methods: **GET**, **POST**, **PUT**, **PATCH**, **DELETE** and **OPTIONS**.
  61. .. code:: python
  62. import aiohttp
  63. import asyncio
  64. from aioresponses import aioresponses
  65. @aioresponses()
  66. def test_request(mocked):
  67. loop = asyncio.get_event_loop()
  68. mocked.get('http://example.com', status=200, body='test')
  69. session = aiohttp.ClientSession()
  70. resp = loop.run_until_complete(session.get('http://example.com'))
  71. assert resp.status == 200
  72. mocked.assert_called_once_with('http://example.com')
  73. for convenience use *payload* argument to mock out json response. Example below.
  74. **as a context manager**
  75. .. code:: python
  76. import asyncio
  77. import aiohttp
  78. from aioresponses import aioresponses
  79. def test_ctx():
  80. loop = asyncio.get_event_loop()
  81. session = aiohttp.ClientSession()
  82. with aioresponses() as m:
  83. m.get('http://test.example.com', payload=dict(foo='bar'))
  84. resp = loop.run_until_complete(session.get('http://test.example.com'))
  85. data = loop.run_until_complete(resp.json())
  86. assert dict(foo='bar') == data
  87. m.assert_called_once_with('http://test.example.com')
  88. **aioresponses allows to mock out any HTTP headers**
  89. .. code:: python
  90. import asyncio
  91. import aiohttp
  92. from aioresponses import aioresponses
  93. @aioresponses()
  94. def test_http_headers(m):
  95. loop = asyncio.get_event_loop()
  96. session = aiohttp.ClientSession()
  97. m.post(
  98. 'http://example.com',
  99. payload=dict(),
  100. headers=dict(connection='keep-alive'),
  101. )
  102. resp = loop.run_until_complete(session.post('http://example.com'))
  103. # note that we pass 'connection' but get 'Connection' (capitalized)
  104. # under the neath `multidict` is used to work with HTTP headers
  105. assert resp.headers['Connection'] == 'keep-alive'
  106. m.assert_called_once_with('http://example.com', method='POST')
  107. **allows to register different responses for the same url**
  108. .. code:: python
  109. import asyncio
  110. import aiohttp
  111. from aioresponses import aioresponses
  112. @aioresponses()
  113. def test_multiple_responses(m):
  114. loop = asyncio.get_event_loop()
  115. session = aiohttp.ClientSession()
  116. m.get('http://example.com', status=500)
  117. m.get('http://example.com', status=200)
  118. resp1 = loop.run_until_complete(session.get('http://example.com'))
  119. resp2 = loop.run_until_complete(session.get('http://example.com'))
  120. assert resp1.status == 500
  121. assert resp2.status == 200
  122. **Repeat response for the same url**
  123. E.g. for cases you want to test retrying mechanisms
  124. .. code:: python
  125. import asyncio
  126. import aiohttp
  127. from aioresponses import aioresponses
  128. @aioresponses()
  129. def test_multiple_responses(m):
  130. loop = asyncio.get_event_loop()
  131. session = aiohttp.ClientSession()
  132. m.get('http://example.com', status=500, repeat=True)
  133. m.get('http://example.com', status=200) # will not take effect
  134. resp1 = loop.run_until_complete(session.get('http://example.com'))
  135. resp2 = loop.run_until_complete(session.get('http://example.com'))
  136. assert resp1.status == 500
  137. assert resp2.status == 500
  138. **match URLs with regular expressions**
  139. .. code:: python
  140. import asyncio
  141. import aiohttp
  142. import re
  143. from aioresponses import aioresponses
  144. @aioresponses()
  145. def test_regexp_example(m):
  146. loop = asyncio.get_event_loop()
  147. session = aiohttp.ClientSession()
  148. pattern = re.compile(r'^http://example\.com/api\?foo=.*$')
  149. m.get(pattern, status=200)
  150. resp = loop.run_until_complete(session.get('http://example.com/api?foo=bar'))
  151. assert resp.status == 200
  152. **allows to make redirects responses**
  153. .. code:: python
  154. import asyncio
  155. import aiohttp
  156. from aioresponses import aioresponses
  157. @aioresponses()
  158. def test_redirect_example(m):
  159. loop = asyncio.get_event_loop()
  160. session = aiohttp.ClientSession()
  161. # absolute urls are supported
  162. m.get(
  163. 'http://example.com/',
  164. headers={'Location': 'http://another.com/'},
  165. status=307
  166. )
  167. resp = loop.run_until_complete(
  168. session.get('http://example.com/', allow_redirects=True)
  169. )
  170. assert resp.url == 'http://another.com/'
  171. # and also relative
  172. m.get(
  173. 'http://example.com/',
  174. headers={'Location': '/test'},
  175. status=307
  176. )
  177. resp = loop.run_until_complete(
  178. session.get('http://example.com/', allow_redirects=True)
  179. )
  180. assert resp.url == 'http://example.com/test'
  181. **allows to passthrough to a specified list of servers**
  182. .. code:: python
  183. import asyncio
  184. import aiohttp
  185. from aioresponses import aioresponses
  186. @aioresponses(passthrough=['http://backend'])
  187. def test_passthrough(m, test_client):
  188. session = aiohttp.ClientSession()
  189. # this will actually perform a request
  190. resp = loop.run_until_complete(session.get('http://backend/api'))
  191. **aioresponses allows to throw an exception**
  192. .. code:: python
  193. import asyncio
  194. from aiohttp import ClientSession
  195. from aiohttp.http_exceptions import HttpProcessingError
  196. from aioresponses import aioresponses
  197. @aioresponses()
  198. def test_how_to_throw_an_exception(m, test_client):
  199. loop = asyncio.get_event_loop()
  200. session = ClientSession()
  201. m.get('http://example.com/api', exception=HttpProcessingError('test'))
  202. # calling
  203. # loop.run_until_complete(session.get('http://example.com/api'))
  204. # will throw an exception.
  205. **aioresponses allows to use callbacks to provide dynamic responses**
  206. .. code:: python
  207. import asyncio
  208. import aiohttp
  209. from aioresponses import CallbackResult, aioresponses
  210. def callback(url, **kwargs):
  211. return CallbackResult(status=418)
  212. @aioresponses()
  213. def test_callback(m, test_client):
  214. loop = asyncio.get_event_loop()
  215. session = ClientSession()
  216. m.get('http://example.com', callback=callback)
  217. resp = loop.run_until_complete(session.get('http://example.com'))
  218. assert resp.status == 418
  219. **aioresponses can be used in a pytest fixture**
  220. .. code:: python
  221. import pytest
  222. from aioresponses import aioresponses
  223. @pytest.fixture
  224. def mock_aioresponse():
  225. with aioresponses() as m:
  226. yield m
  227. Features
  228. --------
  229. * Easy to mock out HTTP requests made by *aiohttp.ClientSession*
  230. License
  231. -------
  232. * Free software: MIT license
  233. Credits
  234. -------
  235. This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.
  236. .. _Cookiecutter: https://github.com/audreyr/cookiecutter
  237. .. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage