Esempio n. 1
0
 def __init__(self, response=None, status=None, headers=None,
              mimetype=None, content_type=None):
     self.response = response
     self.status = status or 200
     self.headers = headers or []
     self.mimetype = mimetype or "text/html"
     self.content_type = get_content_type(self.mimetype, "utf-8")
Esempio n. 2
0
 def __init__(self, response = None, status = None, headers = None, mimetype = None, content_type = None, direct_passthrough = False):
     if isinstance(headers, Headers):
         self.headers = headers
     elif not headers:
         self.headers = Headers()
     else:
         self.headers = Headers(headers)
     if content_type is None:
         if mimetype is None and 'content-type' not in self.headers:
             mimetype = self.default_mimetype
         if mimetype is not None:
             mimetype = get_content_type(mimetype, self.charset)
         content_type = mimetype
     if content_type is not None:
         self.headers['Content-Type'] = content_type
     if status is None:
         status = self.default_status
     if isinstance(status, (int, long)):
         self.status_code = status
     else:
         self.status = status
     self.direct_passthrough = direct_passthrough
     self._on_close = []
     if response is None:
         self.response = []
     elif isinstance(response, basestring):
         self.data = response
     else:
         self.response = response
Esempio n. 3
0
    def using(self, response=None, status=None, headers=None,
              mimetype=None, content_type=None, direct_passthrough=None):
        """Convenience method that works like :meth:`__init__` for responses
        that have already been created. Particularly useful as a complement
        to the rendering machinery, for example ``return render('json',
        error='authentication required').using(status=401)``."""

        if headers is not None:
            self.headers.extend(headers)

        if content_type is None:
            if mimetype is not None:
                mimetype = get_content_type(mimetype, self.charset)
            content_type = mimetype
        if content_type is not None:
            self.headers['Content-Type'] = content_type

        if status is not None:
            if isinstance(status, (int, long)):
                self.status_code = status
            else:
                self.status = status

        if direct_passthrough is not None:
            self.direct_passthrough = direct_passthrough

        if response is not None:
            if isinstance(response, basestring):
                self.data = response
            else:
                self.response = response

        return self
Esempio n. 4
0
 def __init__(self, response = None, status = None, headers = None, mimetype = None, content_type = None, direct_passthrough = False):
     if isinstance(headers, Headers):
         self.headers = headers
     elif not headers:
         self.headers = Headers()
     else:
         self.headers = Headers(headers)
     if content_type is None:
         if mimetype is None and 'content-type' not in self.headers:
             mimetype = self.default_mimetype
         if mimetype is not None:
             mimetype = get_content_type(mimetype, self.charset)
         content_type = mimetype
     if content_type is not None:
         self.headers['Content-Type'] = content_type
     if status is None:
         status = self.default_status
     if isinstance(status, (int, long)):
         self.status_code = status
     else:
         self.status = status
     self.direct_passthrough = direct_passthrough
     self._on_close = []
     if response is None:
         self.response = []
     elif isinstance(response, basestring):
         self.data = response
     else:
         self.response = response
Esempio n. 5
0
 def adapt(self, mimetype=None):
     try:
         fmt_name = MIME_SUPPORT_MAP[mimetype]
     except KeyError:
         fmt_name, mimetype = 'text', 'text/plain'
     _method = getattr(self, 'to_' + fmt_name)
     self.data = _method()
     self.headers['Content-Type'] = get_content_type(mimetype, self.charset)
Esempio n. 6
0
 def adapt(self, mimetype=None):
     try:
         fmt_name = MIME_SUPPORT_MAP[mimetype]
     except KeyError:
         fmt_name, mimetype = 'text', 'text/plain'
     _method = getattr(self, 'to_' + fmt_name)
     self.data = _method()
     self.headers['Content-Type'] = get_content_type(mimetype, self.charset)
Esempio n. 7
0
def test_build_download_response():
    request = Mock()
    request.range = None

    class Dummy:
        pass

    mocked_response = Dummy()
    mocked_response.content = b'asdsadf'
    mocked_response.status_code = 200
    mocked_response.headers = {}

    requests_session = Dummy()
    requests_session.get = MagicMock(return_value=mocked_response)

    filename = '<<filename>>.xml'
    content_type = mimetypes.guess_type(filename)[0]
    url = f'https://source-lllllll.cccc/{filename}'
    response = build_download_response(request, requests_session, url, url, None)
    assert response.headers["content-type"] == content_type
    assert response.headers.get_all('Content-Disposition')[0] == f'attachment;filename={filename}'

    filename = '<<filename>>'
    url = f'https://source-lllllll.cccc/{filename}'
    response = build_download_response(request, requests_session, url, url, None)
    assert response.headers["content-type"] == get_content_type(response.default_mimetype, response.charset)
    assert response.headers.get_all('Content-Disposition')[0] == f'attachment;filename={filename}'

    filename = '<<filename>>'
    url = f'https://source-lllllll.cccc/{filename}'
    response = build_download_response(request, requests_session, url, url, content_type)
    assert response.headers["content-type"] == content_type
    assert response.headers.get_all('Content-Disposition')[0] == f'attachment;filename={filename+mimetypes.guess_extension(content_type)}'

    mocked_response_with_attachment = deepcopy(mocked_response)
    attachment_file_name = 'test.xml'
    mocked_response_with_attachment.headers = {'content-disposition': f'attachment;filename={attachment_file_name}'}

    requests_session_with_attachment = Dummy()
    requests_session_with_attachment.get = MagicMock(return_value=mocked_response_with_attachment)

    url = 'https://source-lllllll.cccc/not-a-filename'
    response = build_download_response(request, requests_session_with_attachment, url, url, None)
    assert response.headers["content-type"] == mimetypes.guess_type(attachment_file_name)[0]
    assert response.headers.get_all('Content-Disposition')[0] == f'attachment;filename={attachment_file_name}'

    mocked_response_with_content_type = deepcopy(mocked_response)
    response_content_type = 'text/csv'
    mocked_response_with_content_type.headers = {'content-type': response_content_type}

    requests_session_with_content_type = Dummy()
    requests_session_with_content_type.get = MagicMock(return_value=mocked_response_with_content_type)

    filename = 'filename.txt'
    url = f'https://source-lllllll.cccc/{filename}'
    response = build_download_response(request, requests_session_with_content_type, url, url, None)
    assert response.headers["content-type"] == response_content_type
    assert response.headers.get_all('Content-Disposition')[0] == f'attachment;filename={filename}'
Esempio n. 8
0
 def __init__(self,
              response=None,
              status=None,
              headers=None,
              mimetype=None,
              content_type=None):
     self.response = response
     self.status = status or 200
     self.headers = headers or []
     self.mimetype = mimetype or "text/html"
     self.content_type = get_content_type(self.mimetype, "utf-8")
Esempio n. 9
0
    def process_response(self, response):
        """Inject mimetype into response before it's sent to the WSGI server.

        This is only intended for stashable view functions, created by
        :meth:`Tango.build_view`.
        """
        Flask.process_response(self, response)
        ctx = _request_ctx_stack.top
        if hasattr(ctx, 'mimetype'):
            mimetype, charset = (ctx.mimetype, response.charset)
            response.content_type = get_content_type(mimetype, charset)
            response.headers['Content-Type'] = response.content_type
        return response
Esempio n. 10
0
def test_build_download_response():
    request = Mock()
    request.range = None

    class Dummy:
        pass

    response = Dummy()
    response.content = b'asdsadf'
    response.status_code = 200

    requests_session = Dummy()
    requests_session.get = MagicMock(return_value=response)

    filename = '<<filename>>.xml'
    content_type = mimetypes.guess_type(filename)[0]
    url = f'https://source-lllllll.cccc/{filename}'
    response = build_download_response(request, requests_session, url, url,
                                       None)
    assert response.headers["content-type"] == content_type
    assert response.headers.get_all(
        'Content-Disposition')[0] == f'attachment;filename={filename}'

    filename = '<<filename>>'
    url = f'https://source-lllllll.cccc/{filename}'
    response = build_download_response(request, requests_session, url, url,
                                       None)
    assert response.headers["content-type"] == get_content_type(
        response.default_mimetype, response.charset)
    assert response.headers.get_all(
        'Content-Disposition')[0] == f'attachment;filename={filename}'

    filename = '<<filename>>'
    url = f'https://source-lllllll.cccc/{filename}'
    response = build_download_response(request, requests_session, url, url,
                                       content_type)
    assert response.headers["content-type"] == content_type
    assert response.headers.get_all(
        'Content-Disposition'
    )[0] == f'attachment;filename={filename + mimetypes.guess_extension(content_type)}'
    def __init__(self, response=None, status=None, headers=None,
                 mimetype=None, content_type=None):
        """Response can be any kind of iterable or string.  If it's a string
        it's considered being an iterable with one item which is the string
        passed.  Headers can be a list of tuples or a `Headers` object.

        Special note for `mimetype` and `content_type`.  For most mime types
        `mimetype` and `content_type` work the same, the difference affects
        only 'text' mimetypes.  If the mimetype passed with `mimetype` is a
        mimetype starting with `text/` it becomes a charset parameter defined
        with the charset of the response object.  In constrast the
        `content_type` parameter is always added as header unmodified.
        """
        if response is None:
            self.response = []
        elif isinstance(response, basestring):
            self.response = [response]
        else:
            self.response = iter(response)
        if not headers:
            self.headers = Headers()
        elif isinstance(headers, Headers):
            self.headers = headers
        else:
            self.headers = Headers(headers)
        if content_type is None:
            if mimetype is None and 'Content-Type' not in self.headers:
                mimetype = self.default_mimetype
            if mimetype is not None:
                mimetype = get_content_type(mimetype, self.charset)
            content_type = mimetype
        if content_type is not None:
            self.headers['Content-Type'] = content_type
        if status is None:
            status = self.default_status
        if isinstance(status, (int, long)):
            self.status_code = status
        else:
            self.status = status
Esempio n. 12
0
 def set_content_type(self, mime_type: str) -> None:
     self.headers["Content-type"] = get_content_type(
         mime_type, self.charset)
Esempio n. 13
0
 def _set_mimetype(self, value):
     self.headers['Content-Type'] = get_content_type(value, self.charset)
Esempio n. 14
0
 def _set_mimetype(self, value):
     self.content_type = get_content_type(value, self.charset)
 def _set_mimetype(self, value):
     self.headers['Content-Type'] = get_content_type(value, self.charset)
Esempio n. 16
0
 def build_response(self, response_data):
     return Response(
         self.format(response_data),
         content_type=get_content_type(self.content_type, Response.charset)
     )
Esempio n. 17
0
def test_build_download_response():
    request = Mock()
    request.range = None

    class Dummy:
        pass

    mocked_response = Dummy()
    mocked_response.content = b"asdsadf"
    mocked_response.status_code = 200
    mocked_response.headers = {}

    requests_session = Dummy()
    requests_session.get = MagicMock(return_value=mocked_response)

    filename = "<<filename>>.xml"
    content_type = mimetypes.guess_type(filename)[0]
    url = f"https://source-lllllll.cccc/{filename}"
    response = build_download_response(request, requests_session, url, url,
                                       None)
    assert response.headers["content-type"] == content_type
    assert (response.headers.get_all("Content-Disposition")[0] ==
            f"attachment;filename={filename}")

    filename = "<<filename>>"
    url = f"https://source-lllllll.cccc/{filename}"
    response = build_download_response(request, requests_session, url, url,
                                       None)
    assert response.headers["content-type"] == get_content_type(
        response.default_mimetype, response.charset)
    assert (response.headers.get_all("Content-Disposition")[0] ==
            f"attachment;filename={filename}")

    filename = "<<filename>>"
    url = f"https://source-lllllll.cccc/{filename}"
    response = build_download_response(request, requests_session, url, url,
                                       content_type)
    assert response.headers["content-type"] == content_type

    matched_cd = (
        f"attachment;filename={filename+mimetypes.guess_extension(content_type)}"
    )
    assert response.headers.get_all("Content-Disposition")[0] == matched_cd

    mocked_response_with_attachment = deepcopy(mocked_response)
    attachment_file_name = "test.xml"
    mocked_response_with_attachment.headers = {
        "content-disposition": f"attachment;filename={attachment_file_name}"
    }

    requests_session_with_attachment = Dummy()
    requests_session_with_attachment.get = MagicMock(
        return_value=mocked_response_with_attachment)

    url = "https://source-lllllll.cccc/not-a-filename"
    response = build_download_response(request,
                                       requests_session_with_attachment, url,
                                       url, None)
    assert (response.headers["content-type"] == mimetypes.guess_type(
        attachment_file_name)[0])  # noqa

    matched_cd = f"attachment;filename={attachment_file_name}"
    assert response.headers.get_all("Content-Disposition")[0] == matched_cd

    mocked_response_with_content_type = deepcopy(mocked_response)
    response_content_type = "text/csv"
    mocked_response_with_content_type.headers = {
        "content-type": response_content_type
    }

    requests_session_with_content_type = Dummy()
    requests_session_with_content_type.get = MagicMock(
        return_value=mocked_response_with_content_type)

    filename = "filename.txt"
    url = f"https://source-lllllll.cccc/{filename}"
    response = build_download_response(request,
                                       requests_session_with_content_type, url,
                                       url, None)
    assert response.headers["content-type"] == response_content_type
    assert (response.headers.get_all("Content-Disposition")[0] ==
            f"attachment;filename={filename}")
Esempio n. 18
0
 def _set_mimetype(self, value):
     self.content_type = get_content_type(value, self.charset)
Esempio n. 19
0
File: base.py Progetto: ccns1/ccns11
 def mimetype(self, value: str) -> None:
     """Set the mimetype to the value."""
     self.headers["Content-Type"] = get_content_type(value, self.charset)
Esempio n. 20
0
 def build_response(self, response_data):
     return Response(
         self.format(response_data),
         content_type=get_content_type(self.content_type, Response.charset)
     )