requests.cookies.MockResponse

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

3 Examples 7

Example 1

Project: requests-mock Source File: response.py
def _extract_cookies(request, response, cookies):
    """Add cookies to the response.

    Cookies in requests are extracted from the headers in the original_response
    httplib.HTTPMessage which we don't create so we have to do this step
    manually.
    """
    # This will add cookies set manually via the Set-Cookie or Set-Cookie2
    # header but this only allows 1 cookie to be set.
    http_message = compat._FakeHTTPMessage(response.headers)
    response.cookies.extract_cookies(MockResponse(http_message),
                                     MockRequest(request))

    # This allows you to pass either a CookieJar or a dictionary to request_uri
    # or directly to create_response. To allow more than one cookie to be set.
    if cookies:
        merge_cookies(response.cookies, cookies)

Example 2

Project: django-wham Source File: httmock.py
def response(status_code=200, content='', headers=None, reason=None, elapsed=0,
             request=None):
    res = requests.Response()
    res.status_code = status_code
    if isinstance(content, dict):
        if sys.version_info[0] == 3:
            content = bytes(json.dumps(content), 'utf-8')
        else:
            content = json.dumps(content)
    res._content = content
    res._content_consumed = content
    res.headers = structures.CaseInsensitiveDict(headers or {})
    res.reason = reason
    res.elapsed = datetime.timedelta(elapsed)
    res.request = request
    if hasattr(request, 'url'):
        res.url = request.url
        if isinstance(request.url, bytes):
            res.url = request.url.decode('utf-8')
    if 'set-cookie' in res.headers:
        res.cookies.extract_cookies(cookies.MockResponse(Headers(res)),
                                    cookies.MockRequest(request))

    # normally this closes the underlying connection,
    #  but we have nothing to free.
    res.close = lambda *args, **kwargs: None

    return res

Example 3

Project: t1-python Source File: requests_patch.py
def patched_extract_cookies_to_jar(jar, request, response):
    """Patched version to support mocked HTTPResponses from Responses.

    :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
    :param request: our own requests.Request object
    :param response: urllib3.HTTPResponse object
    """

    # massive hack, needs a get_all function that returns a list of headers.
    # we are only interested in the one session cookie header,
    # so don't really need to handle any other cases
    def get_all(self, name, default=[]):
        return [self.get(name, default)]

    if not (hasattr(response, '_original_response') and
            response._original_response):
        # just grab the headers from the mocked response object
        if not hasattr(response.headers, 'get_all'):
            response.headers.get_all = types.MethodType(get_all, response.headers)

        res = requests.cookies.MockResponse(response.headers)
    else:
        # the _original_response field is the wrapped httplib.HTTPResponse object
        # pull out the HTTPMessage with the headers and put it in the mock:
        res = requests.cookies.MockResponse(response._original_response.msg)

    req = requests.cookies.MockRequest(request)
    jar.extract_cookies(res, req)