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