requests_mock.compat._fake_http_response

Here are the examples of the python api requests_mock.compat._fake_http_response taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

6 Examples 7

Example 1

Project: fjord
Source File: test_mocker.py
View license
    @mock.patch('requests.adapters.HTTPAdapter.send')
    @requests_mock.mock(real_http=True)
    def test_real_http(self, real_send, mocker):
        url = 'http://www.google.com/'

        # NOTE(jamielennox): hack for requests 1.2.3 remove after
        # requirements catches up.
        class FakeHTTPResponse(object):
            _original_response = compat._fake_http_response

        real_send.return_value = requests.Response()
        real_send.return_value.status_code = 200
        real_send.return_value.raw = FakeHTTPResponse()
        requests.get(url)

        self.assertEqual(1, real_send.call_count)
        self.assertEqual(url, real_send.call_args[0][0].url)

Example 2

Project: fjord
Source File: test_mocker.py
View license
    @mock.patch('requests.adapters.HTTPAdapter.send')
    @requests_mock.mock(real_http=True)
    def test_real_http(self, real_send, mocker):
        url = 'http://www.google.com/'

        # NOTE(jamielennox): hack for requests 1.2.3 remove after
        # requirements catches up.
        class FakeHTTPResponse(object):
            _original_response = compat._fake_http_response

        real_send.return_value = requests.Response()
        real_send.return_value.status_code = 200
        real_send.return_value.raw = FakeHTTPResponse()
        requests.get(url)

        self.assertEqual(1, real_send.call_count)
        self.assertEqual(url, real_send.call_args[0][0].url)

Example 3

Project: requests-mock
Source File: test_mocker.py
View license
    @mock.patch('requests.adapters.HTTPAdapter.send')
    @requests_mock.mock(real_http=True)
    def test_real_http(self, real_send, mocker):
        url = 'http://www.google.com/'

        # NOTE(jamielennox): hack for requests 1.2.3 remove after
        # requirements catches up.
        class FakeHTTPResponse(object):
            _original_response = compat._fake_http_response

        real_send.return_value = requests.Response()
        real_send.return_value.status_code = 200
        real_send.return_value.raw = FakeHTTPResponse()
        requests.get(url)

        self.assertEqual(1, real_send.call_count)
        self.assertEqual(url, real_send.call_args[0][0].url)

Example 4

Project: fjord
Source File: response.py
View license
def create_response(request, **kwargs):
    """
    :param int status_code: The status code to return upon a successful
        match. Defaults to 200.
    :param HTTPResponse raw: A HTTPResponse object to return upon a
        successful match.
    :param io.IOBase body: An IO object with a read() method that can
        return a body on successful match.
    :param bytes content: A byte string to return upon a successful match.
    :param unicode text: A text string to return upon a successful match.
    :param object json: A python object to be converted to a JSON string
        and returned upon a successful match.
    :param dict headers: A dictionary object containing headers that are
        returned upon a successful match.
    """
    connection = kwargs.pop('connection', _FakeConnection())

    _check_body_arguments(**kwargs)

    raw = kwargs.pop('raw', None)
    body = kwargs.pop('body', None)
    content = kwargs.pop('content', None)
    text = kwargs.pop('text', None)
    json = kwargs.pop('json', None)
    encoding = None

    if content and not isinstance(content, six.binary_type):
        raise TypeError('Content should be a callback or binary data')
    if text and not isinstance(text, six.string_types):
        raise TypeError('Text should be a callback or string data')

    if json is not None:
        text = jsonutils.dumps(json)
    if text is not None:
        encoding = 'utf-8'
        content = text.encode(encoding)
    if content is not None:
        body = six.BytesIO(content)
    if not raw:
        raw = HTTPResponse(status=kwargs.get('status_code', _DEFAULT_STATUS),
                           headers=kwargs.get('headers', {}),
                           reason=kwargs.get('reason'),
                           body=body or six.BytesIO(six.b('')),
                           decode_content=False,
                           preload_content=False,
                           original_response=compat._fake_http_response)

    response = _http_adapter.build_response(request, raw)
    response.connection = connection
    response.encoding = encoding
    return response

Example 5

Project: fjord
Source File: response.py
View license
def create_response(request, **kwargs):
    """
    :param int status_code: The status code to return upon a successful
        match. Defaults to 200.
    :param HTTPResponse raw: A HTTPResponse object to return upon a
        successful match.
    :param io.IOBase body: An IO object with a read() method that can
        return a body on successful match.
    :param bytes content: A byte string to return upon a successful match.
    :param unicode text: A text string to return upon a successful match.
    :param object json: A python object to be converted to a JSON string
        and returned upon a successful match.
    :param dict headers: A dictionary object containing headers that are
        returned upon a successful match.
    """
    connection = kwargs.pop('connection', _FakeConnection())

    _check_body_arguments(**kwargs)

    raw = kwargs.pop('raw', None)
    body = kwargs.pop('body', None)
    content = kwargs.pop('content', None)
    text = kwargs.pop('text', None)
    json = kwargs.pop('json', None)
    encoding = None

    if content and not isinstance(content, six.binary_type):
        raise TypeError('Content should be a callback or binary data')
    if text and not isinstance(text, six.string_types):
        raise TypeError('Text should be a callback or string data')

    if json is not None:
        text = jsonutils.dumps(json)
    if text is not None:
        encoding = 'utf-8'
        content = text.encode(encoding)
    if content is not None:
        body = six.BytesIO(content)
    if not raw:
        raw = HTTPResponse(status=kwargs.get('status_code', _DEFAULT_STATUS),
                           headers=kwargs.get('headers', {}),
                           reason=kwargs.get('reason'),
                           body=body or six.BytesIO(six.b('')),
                           decode_content=False,
                           preload_content=False,
                           original_response=compat._fake_http_response)

    response = _http_adapter.build_response(request, raw)
    response.connection = connection
    response.encoding = encoding
    return response

Example 6

Project: requests-mock
Source File: response.py
View license
def create_response(request, **kwargs):
    """
    :param int status_code: The status code to return upon a successful
        match. Defaults to 200.
    :param HTTPResponse raw: A HTTPResponse object to return upon a
        successful match.
    :param io.IOBase body: An IO object with a read() method that can
        return a body on successful match.
    :param bytes content: A byte string to return upon a successful match.
    :param unicode text: A text string to return upon a successful match.
    :param object json: A python object to be converted to a JSON string
        and returned upon a successful match.
    :param dict headers: A dictionary object containing headers that are
        returned upon a successful match.
    :param CookieJar cookies: A cookie jar with cookies to set on the
        response.
    """
    connection = kwargs.pop('connection', _FakeConnection())

    _check_body_arguments(**kwargs)

    raw = kwargs.pop('raw', None)
    body = kwargs.pop('body', None)
    content = kwargs.pop('content', None)
    text = kwargs.pop('text', None)
    json = kwargs.pop('json', None)
    encoding = None

    if content and not isinstance(content, six.binary_type):
        raise TypeError('Content should be binary data')
    if text and not isinstance(text, six.string_types):
        raise TypeError('Text should be string data')

    if json is not None:
        text = jsonutils.dumps(json)
    if text is not None:
        encoding = 'utf-8'
        content = text.encode(encoding)
    if content is not None:
        body = _IOReader(content)
    if not raw:
        raw = HTTPResponse(status=kwargs.get('status_code', _DEFAULT_STATUS),
                           headers=kwargs.get('headers', {}),
                           reason=kwargs.get('reason'),
                           body=body or _IOReader(six.b('')),
                           decode_content=False,
                           preload_content=False,
                           original_response=compat._fake_http_response)

    response = _http_adapter.build_response(request, raw)
    response.connection = connection
    response.encoding = encoding

    _extract_cookies(request, response, kwargs.get('cookies'))

    return response