
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
.. image:: https://travis-ci.org/genericclient/testing-aiohttp.svg?branch=master :target: https://travis-ci.org/genericclient/testing-aiohttp
Testing utilities for aiohttp
. Python 3.5+ only.
::
$ pip install testing-aiohttp
RouteManager
RouteManager
will mock up responses for `aiohttp.Client``.
The API is inspired by the responses
library::
from aiohttp import ClientSession
from asynctest import TestCase
from testing_aiohttp import RouteManager
# Create your tests here.
class MyTestCase(TestCase):
async def test_response_data(self):
with RouteManager() as rsps:
rsps.add('GET', 'http://example.org/users', json=[
{
'id': 1,
'username': 'user1',
'group': 'watchers',
},
{
'id': 2,
'username': 'user2',
'group': 'watchers',
},
])
async with ClientSession() as session:
response = await session.get('http://example.org/users')
self.assertEqual(response, 200)
users = await response.json()
self.assertEqual(len(users), 2)
::
from aiohttp import ClientSession
from asynctest import TestCase
from testing_aiohttp import RouteManager
async def request_callback(request):
return (200, {}, 'ok')
class MyTestCase(TestCase):
async def test_endpoint_detail_route(self):
with RouteManager() as rsps:
rsps.add_callback(
'POST', 'http://example.org/users/2/notify',
callback=request_callback,
content_type='application/json',
)
async with ClientSession() as session:
response = await session.post('http://example.org/users/2/notify')
self.assertEqual(await response.text(), 'ok')
:: from aiohttp import ClientSession from asynctest import TestCase
from testing_aiohttp.rsps import RouteManager, RouteNotFoundError
class MyTestCase(TestCase):
async def test_response_match_querystring(self):
with RouteManager() as rsps:
rsps.add('GET', 'http://example.org/users?username=user1', json=[
{
'id': 1,
'username': 'user1',
'group': 'watchers',
},
], match_querystring=True)
with ClientSession() as session:
response = await session.get('http://example.org/users', params={'username': 'user1'})
self.assertEqual(response.status, 200)
users = await response.json()
self.assertEqual(len(users), 1)
with self.assertRaises(RouteNotFoundError):
with RouteManager() as rsps:
rsps.add('GET', 'http://example.org/users?username=user1', json=[
{
'id': 1,
'username': 'user1',
'group': 'watchers',
},
], match_querystring=True)
with ClientSession() as session:
await session.get('http://example.org/users')
Licensed under the MIT License.
FAQs
Unknown package
We found that testing-aiohttp demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.