METADATA 10 KB

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