Beispiel #1
0
def make_response(loop,
                  method,
                  url,
                  data=None,
                  content_type='text/plain',
                  charset='utf-8'):
    if LooseVersion(aiohttp_version) >= LooseVersion('3.3.0'):
        response = ClientResponse(method,
                                  URL(url),
                                  writer=mock.Mock(),
                                  continue100=None,
                                  timer=None,
                                  request_info=mock.Mock(),
                                  traces=[],
                                  loop=loop,
                                  session=mock.Mock())
    elif LooseVersion(aiohttp_version) >= LooseVersion('3.1.0'):
        response = ClientResponse(method,
                                  URL(url),
                                  writer=mock.Mock(),
                                  continue100=None,
                                  timer=None,
                                  request_info=mock.Mock(),
                                  auto_decompress=True,
                                  traces=[],
                                  loop=loop,
                                  session=mock.Mock())
    else:
        response = ClientResponse(method,
                                  URL(url),
                                  writer=mock.Mock(),
                                  continue100=None,
                                  timer=None,
                                  request_info=mock.Mock(),
                                  auto_decompress=True)
        response._post_init(loop, mock.Mock())

    def side_effect(*args, **kwargs):
        fut = loop.create_future()
        fut.set_result(str(data).encode(charset))
        return fut

    if LooseVersion(aiohttp_version) >= LooseVersion('3.3.0'):
        response._headers = {
            'Content-Type': '%s; charset=%s' % (content_type, charset)
        }
    else:
        response.headers = {
            'Content-Type': '%s; charset=%s' % (content_type, charset)
        }
    content = response.content = mock.Mock()
    if data:
        content.read.side_effect = side_effect

    return response
Beispiel #2
0
async def prepare_fake_response(url,
                                *,
                                method='GET',
                                req_headers=None,
                                resp_headers=None,
                                resp_status=200,
                                resp_reason='OK',
                                resp_content=b''):
    resp = ClientResponse(method,
                          URL(val=url),
                          request_info=RequestInfo(url, method, req_headers
                                                   or {}))
    resp._content = resp_content
    resp.status = resp_status
    resp.reason = resp_reason
    resp.headers = resp_headers or {}
    future = asyncio.Future()
    future.set_result(resp)
    return future
Beispiel #3
0
def make_response(loop,
                  method,
                  url,
                  data=None,
                  content_type='text/plain',
                  charset='utf-8'):
    response = ClientResponse(method, URL(url))
    response._post_init(loop, mock.Mock())

    def side_effect(*args, **kwargs):
        fut = loop.create_future()
        fut.set_result(str(data).encode(charset))
        return fut

    response.headers = {
        'Content-Type': '%s; charset=%s' % (content_type, charset)
    }
    content = response.content = mock.Mock()
    if data:
        content.read.side_effect = side_effect

    return response