Пример #1
0
    def send(self, request, *args, **kwargs):
        try:
            resp = super(ContentTypeBlockAdapter, self).send(request, *args, **kwargs)
        except Exception as e:
            resp = self.handle_error(e)

        location = resp.headers.get("location")
        if location:
            return resp

        content_type = resp.headers.get("content-type")
        if not content_type:
            return self.send_block(request, code=500, *args, **kwargs)

        content_type, params = _parse_content_type_header(content_type)

        if content_type == "text/plain":
            try:
                resp.text
            except Exception as e:
                logger.error("text/plain which isnt plain text: {}".format(e))
                return self.send_block(request, code=406, *args, **kwargs)

        parts = content_type.split("/")
        if parts[0] not in ["text", "application"]:
            return self.send_block(request, code=406, *args, **kwargs)

        if parts[1] in ["json"]:
            return resp

        if parts[1] in ["javascript", "css"]:
            return self.send_block(request, code=406, *args, **kwargs)

        if parts[0] == "application":
            if "html" not in parts[1] and "xml" not in parts[1]:
                return self.send_block(request, code=406, *args, **kwargs)

        return resp
Пример #2
0
def test__parse_content_type_header(value, expected):
    assert _parse_content_type_header(value) == expected
Пример #3
0
def test__parse_content_type_header(value, expected):
    assert _parse_content_type_header(value) == expected