No project description provided
Project description
===============
testing-aiohttp
===============
.. 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.
Installation
============
::
$ pip install testing-aiohttp
Usage
=====
``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')
License
=======
Licensed under the MIT License.
testing-aiohttp
===============
.. 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.
Installation
============
::
$ pip install testing-aiohttp
Usage
=====
``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')
License
=======
Licensed under the MIT License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
testing-aiohttp-0.0.7.tar.gz
(5.2 kB
view details)
Built Distribution
File details
Details for the file testing-aiohttp-0.0.7.tar.gz
.
File metadata
- Download URL: testing-aiohttp-0.0.7.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bda0e21769b9d908b4f276dfdfb192c17a4d9d62a49e1dd7855d9d5d7427f2b |
|
MD5 | 526f133a1f78ecaf716e08fb246bd730 |
|
BLAKE2b-256 | f83e020d38fc94184008c16d4acfa6815174cc1d77c28124e0436635560d9f3a |
File details
Details for the file testing_aiohttp-0.0.7-py2.py3-none-any.whl
.
File metadata
- Download URL: testing_aiohttp-0.0.7-py2.py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d57d57ac2414d0dceed62e471e59bd5d60268a406e6aec7e2415ba051a2a2bd |
|
MD5 | d96505d69402d6db41255aa656d33fd7 |
|
BLAKE2b-256 | 7b28108466b14e6c2aae4f887d0d012a4c4441c816fc29868278229817c1ae1e |