Skip to content

Commit

Permalink
Merge pull request #19 from pnuckowski/more-docs
Browse files Browse the repository at this point in the history
More docs
  • Loading branch information
pnuckowski committed Nov 3, 2016
2 parents a80c4e8 + f26420a commit 55fb280
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
62 changes: 57 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ aioresponses
:target: https://pyup.io/repos/github/pnuckowski/aioresponses/
:alt: Updates

.. image:: https://img.shields.io/pypi/v/aioresponses.svg
:target: https://pypi.python.org/pypi/aioresponses

.. image:: https://readthedocs.org/projects/aioresponses/badge/?version=latest
:target: https://aioresponses.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status



Aioresponses is a helper for mock/fake web requests in python aiohttp package.

For *requests* module there is a lot of packages that helps us with testing (eg. *httpretty*, *responses*, *requests-mock*).
Expand All @@ -33,12 +35,17 @@ Installing
----------

.. code:: bash
$ pip install aioresponses
Examples
Usage
--------
The package may used in many ways.
The most common and easier way is to use a decorator.

To mock out http request use *aioresponses* as method decorator or as context manager.

Response *status* code, *body*, *payload* (for json response) and *headers* can be mocked.

Supported http methods: **get**, **post**, **put**, **patch**, **delete** and **options**.

.. code:: python
Expand All @@ -56,8 +63,9 @@ The most common and easier way is to use a decorator.
assert resp.status == 200
for convenience use *payload* argument to mock out json response. Example below.

Testing by using context manager.
**as context manager**

.. code:: python
Expand All @@ -76,6 +84,50 @@ Testing by using context manager.
assert dict(foo='bar') == data
**aioresponses allow to mock out any HTTP headers**

.. code:: python
import asyncio
import aiohttp
from aioresponses import aioresponses
@aioresponses()
def test_http_headers(m):
loop = asyncio.get_event_loop()
session = aiohttp.ClientSession()
m.post(
'http://example.com',
payload=dict(),
headers=dict(connection='keep-alive'),
)
resp = loop.run_until_complete(session.post('http://example.com'))
# note that we pass 'connection' but get 'Connection' (capitalized)
# under the neath `multidict` is used to work with HTTP headers
assert resp.headers['Connection'] == 'keep-alive'
**allow to register different response for the same url**

.. code:: python
import asyncio
import aiohttp
from aioresponses import aioresponses
@aioresponses()
def test_multiple_respnses(m):
loop = asyncio.get_event_loop()
session = aiohttp.ClientSession()
m.get('http://example.com', status=500)
m.get('http://example.com', status=200)
resp1 = loop.run_until_complete(session.get('http://example.com'))
resp2 = loop.run_until_complete(session.get('http://example.com'))
assert resp1.status == 500
assert resp2.status == 200
Features
Expand Down
3 changes: 2 additions & 1 deletion aioresponses/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import json
from functools import wraps
from multidict import CIMultiDict

try:
from typing import Dict, Tuple
Expand Down Expand Up @@ -48,7 +49,7 @@ def match(self, method: str, url: str) -> bool:
def build_response(self) -> 'ClientResponse':
self.resp = ClientResponse(self.method, self.url)
# we need to initialize headers manually
self.resp.headers = {hdrs.CONTENT_TYPE: self.content_type}
self.resp.headers = CIMultiDict({hdrs.CONTENT_TYPE: self.content_type})
if self.headers:
self.resp.headers.update(self.headers)
self.resp.status = self.status
Expand Down

0 comments on commit 55fb280

Please sign in to comment.