Skip to content

Commit

Permalink
Add CallbackResult class for returning responses from callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
decaz committed Jan 19, 2019
1 parent f7e99a2 commit fd73cb7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ for convenience use *payload* argument to mock out json response. Example below.
import asyncio
import aiohttp
from aioresponses import aioresponses, build_response
from aioresponses import CallbackResult, aioresponses
def callback(url, **kwargs):
return build_response(url, status=418)
return CallbackResult(url, status=418)
@aioresponses()
def test_callback(m, test_client):
Expand Down
3 changes: 2 additions & 1 deletion aioresponses/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
from .compat import build_response
from .compat import CallbackResult
from .core import aioresponses

__version__ = '0.4.1'

__all__ = [
'aioresponses',
'CallbackResult',
]
23 changes: 23 additions & 0 deletions aioresponses/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ def build_response(
return resp


class CallbackResult:

def __init__(self, url: 'Union[URL, str]',
method: str = hdrs.METH_GET,
status: int = 200,
body: str = '',
content_type: str = 'application/json',
payload: Dict = None,
headers: Dict = None,
response_class: 'ClientResponse' = None,
reason: Optional[str] = None):
self.url = url
self.method = method
self.status = status
self.body = body
self.content_type = content_type
self.payload = payload
self.headers = headers
self.response_class = response_class
self.reason = reason


__all__ = [
'URL',
'Pattern',
Expand All @@ -125,4 +147,5 @@ def build_response(
'stream_reader_factory',
'normalize_url',
'build_response',
'CallbackResult',
]
26 changes: 16 additions & 10 deletions aioresponses/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from aiohttp import (
ClientConnectionError,
ClientResponse,
ClientSession,
hdrs,
http
Expand Down Expand Up @@ -83,16 +84,21 @@ async def _build_response(
else:
resp = None
if resp is None:
resp = build_response(
url=url,
method=self.method,
status=self.status,
body=self.body,
content_type=self.content_type,
payload=self.payload,
headers=self.headers,
response_class=self.response_class,
reason=self.reason)
url = url
resp_data = self
else:
url = resp.url
resp_data = resp
resp = build_response(
url=url,
method=resp_data.method,
status=resp_data.status,
body=resp_data.body,
content_type=resp_data.content_type,
payload=resp_data.payload,
headers=resp_data.headers,
response_class=resp_data.response_class,
reason=resp_data.reason)
return resp


Expand Down
6 changes: 3 additions & 3 deletions tests/test_aioresponses.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
)
from aiohttp.http_exceptions import HttpProcessingError

from aioresponses.compat import URL, build_response
from aioresponses import aioresponses
from aioresponses.compat import URL
from aioresponses import CallbackResult, aioresponses


@ddt
Expand Down Expand Up @@ -322,7 +322,7 @@ def test_callback(self, m):
def callback(url, **kwargs):
self.assertEqual(str(url), self.url)
self.assertEqual(kwargs, {'allow_redirects': True})
return build_response(url, body=body)
return CallbackResult(url, body=body)

m.get(self.url, callback=callback)
response = self.run_async(self.request(self.url))
Expand Down

0 comments on commit fd73cb7

Please sign in to comment.