def test_should_be_stream(self):
        BIGGER_THAN_MIN_STREAMING_LENGTH = '5120'
        headers = {'Content-Type': 'application/pdf', 'Content-Length': 'asad'}

        urlopen_mock = get_urlopen_mock(headers=headers)

        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(True, utils.should_stream(urlopen_mock))

        headers['Content-Length'] = BIGGER_THAN_MIN_STREAMING_LENGTH
        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(True, utils.should_stream(urlopen_mock))
Example #2
0
    def test_should_be_stream(self):
        BIGGER_THAN_MIN_STREAMING_LENGTH = "5120"
        headers = {"Content-Type": "application/pdf", "Content-Length": "asad"}

        urlopen_mock = get_urlopen_mock(headers=headers)

        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(True, utils.should_stream(urlopen_mock))

        headers["Content-Length"] = BIGGER_THAN_MIN_STREAMING_LENGTH
        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(True, utils.should_stream(urlopen_mock))
Example #3
0
    def test_not_should_stream(self):
        SMALLER_THAN_MIN_STREAMING_LENGTH = "5"
        headers = {"Content-Type": "text/html", "Content-Length": SMALLER_THAN_MIN_STREAMING_LENGTH}

        urlopen_mock = get_urlopen_mock(headers=headers)

        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(False, utils.should_stream(urlopen_mock))

        headers["Content-Type"] = "application/pdf"
        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(False, utils.should_stream(urlopen_mock))
    def test_should_be_stream(self):
        BIGGER_THAN_MIN_STREAMING_LENGTH = '5120'
        headers = {'Content-Type': 'application/pdf',
                   'Content-Length': 'asad'}

        urlopen_mock = get_urlopen_mock(headers=headers)

        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(True, utils.should_stream(urlopen_mock))

        headers['Content-Length'] = BIGGER_THAN_MIN_STREAMING_LENGTH
        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(True, utils.should_stream(urlopen_mock))
    def test_not_should_stream(self):
        SMALLER_THAN_MIN_STREAMING_LENGTH = '5'
        headers = {'Content-Type': 'text/html',
                   'Content-Length': SMALLER_THAN_MIN_STREAMING_LENGTH}

        urlopen_mock = get_urlopen_mock(headers=headers)

        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(False, utils.should_stream(urlopen_mock))

        headers['Content-Type'] = 'application/pdf'
        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(False, utils.should_stream(urlopen_mock))
Example #6
0
    def test_not_should_stream(self):
        SMALLER_THAN_MIN_STREAMING_LENGTH = '5'
        headers = {'Content-Type': 'text/html',
                   'Content-Length': SMALLER_THAN_MIN_STREAMING_LENGTH}

        urlopen_mock = get_urlopen_mock(headers=headers)

        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(False, utils.should_stream(urlopen_mock))

        headers['Content-Type'] = 'application/pdf'
        type(urlopen_mock).headers = PropertyMock(return_value=headers)
        self.assertEqual(False, utils.should_stream(urlopen_mock))
Example #7
0
def get_django_response(proxy_response, strict_cookies=False):
    """This method is used to create an appropriate response based on the
    Content-Length of the proxy_response. If the content is bigger than
    MIN_STREAMING_LENGTH, which is found on utils.py,
    than django.http.StreamingHttpResponse will be created,
    else a django.http.HTTPResponse will be created instead

    :param proxy_response: An Instance of urllib3.response.HTTPResponse that
                           will create an appropriate response
    :param strict_cookies: Whether to only accept RFC-compliant cookies
    :returns: Returns an appropriate response based on the proxy_response
              content-length
    """
    status = proxy_response.status
    headers = proxy_response.headers

    logger.debug("Proxy response headers: %s", headers)

    content_type = headers.get("Content-Type")

    logger.debug("Content-Type: %s", content_type)

    if should_stream(proxy_response):
        logger.info("Content-Length is bigger than %s", DEFAULT_AMT)
        s = proxy_response.stream(DEFAULT_AMT)
        response = StreamingHttpResponse(s, status=status, content_type=content_type)
    else:
        content = proxy_response.data or b""
        response = HttpResponse(content, status=status, content_type=content_type)

    logger.info("Normalizing response headers")
    set_response_headers(response, headers)

    logger.debug("Response headers: %s", getattr(response, "_headers"))

    cookies = proxy_response.headers.getlist("set-cookie")
    logger.info("Checking for invalid cookies")
    for cookie_string in cookies:
        cookie_dict = cookie_from_string(cookie_string, strict_cookies=strict_cookies)
        # if cookie is invalid cookie_dict will be None
        if cookie_dict:
            response.set_cookie(**cookie_dict)

    logger.debug("Response cookies: %s", response.cookies)

    return response