Exemplo n.º 1
0
def test_identity():
    assert b"string" == encoding.decode("identity", b"string")
    assert b"string" == encoding.encode("identity", b"string")
    assert b"string" == encoding.encode(b"identity", b"string")
    assert b"string" == encoding.decode(b"identity", b"string")
    assert not encoding.encode("nonexistent", b"string")
    assert not encoding.decode("nonexistent encoding", b"string")
Exemplo n.º 2
0
def test_deflate():
    assert b"string" == encoding.decode("deflate",
                                        encoding.encode("deflate", b"string"))
    assert b"string" == encoding.decode(
        "deflate",
        encoding.encode("deflate", b"string")[2:-4])
    assert encoding.decode("deflate", b"bogus") is None
Exemplo n.º 3
0
def test_deflate():
    assert b"string" == encoding.decode(encoding.encode(b"string", "deflate"),
                                        "deflate")
    assert b"string" == encoding.decode(
        encoding.encode(b"string", "deflate")[2:-4], "deflate")
    with tutils.raises(ValueError):
        encoding.decode(b"bogus", "deflate")
Exemplo n.º 4
0
def test_cache():
    decode_gzip = mock.MagicMock()
    decode_gzip.return_value = b"decoded"
    encode_gzip = mock.MagicMock()
    encode_gzip.return_value = b"encoded"

    with mock.patch.dict(encoding.custom_decode, gzip=decode_gzip):
        with mock.patch.dict(encoding.custom_encode, gzip=encode_gzip):
            assert encoding.decode(b"encoded", "gzip") == b"decoded"
            assert decode_gzip.call_count == 1

            # should be cached
            assert encoding.decode(b"encoded", "gzip") == b"decoded"
            assert decode_gzip.call_count == 1

            # the other way around as well
            assert encoding.encode(b"decoded", "gzip") == b"encoded"
            assert encode_gzip.call_count == 0

            # different encoding
            decode_gzip.return_value = b"bar"
            assert encoding.encode(b"decoded", "deflate") != b"decoded"
            assert encode_gzip.call_count == 0

            # This is not in the cache anymore
            assert encoding.encode(b"decoded", "gzip") == b"encoded"
            assert encode_gzip.call_count == 1
Exemplo n.º 5
0
def test_deflate():
    assert "string" == encoding.decode("deflate",
                                       encoding.encode("deflate", "string"))
    assert "string" == encoding.decode(
        "deflate",
        encoding.encode("deflate", "string")[2:-4])
    assert None == encoding.decode("deflate", "bogus")
    def test_get_content_view(self):
        r = cv.get_content_view(cv.get("Raw"),
                                [["content-type", "application/json"]],
                                "[1, 2, 3]", 1000, False)
        assert "Raw" in r[0]

        r = cv.get_content_view(cv.get("Auto"),
                                [["content-type", "application/json"]],
                                "[1, 2, 3]", 1000, False)
        assert r[0] == "JSON"

        r = cv.get_content_view(cv.get("Auto"),
                                [["content-type", "application/json"]],
                                "[1, 2", 1000, False)
        assert "Raw" in r[0]

        r = cv.get_content_view(cv.get("AMF"), [], "[1, 2", 1000, False)
        assert "Raw" in r[0]

        r = cv.get_content_view(cv.get("Auto"),
                                [["content-type", "application/json"],
                                 ["content-encoding", "gzip"]],
                                encoding.encode('gzip',
                                                "[1, 2, 3]"), 1000, False)
        assert "decoded gzip" in r[0]
        assert "JSON" in r[0]

        r = cv.get_content_view(cv.get("XML"),
                                [["content-type", "application/json"],
                                 ["content-encoding", "gzip"]],
                                encoding.encode('gzip',
                                                "[1, 2, 3]"), 1000, False)
        assert "decoded gzip" in r[0]
        assert "Raw" in r[0]
Exemplo n.º 7
0
def test_cache():
    decode_gzip = mock.MagicMock()
    decode_gzip.return_value = b"decoded"
    encode_gzip = mock.MagicMock()
    encode_gzip.return_value = b"encoded"

    with mock.patch.dict(encoding.custom_decode, gzip=decode_gzip):
        with mock.patch.dict(encoding.custom_encode, gzip=encode_gzip):
            assert encoding.decode(b"encoded", "gzip") == b"decoded"
            assert decode_gzip.call_count == 1

            # should be cached
            assert encoding.decode(b"encoded", "gzip") == b"decoded"
            assert decode_gzip.call_count == 1

            # the other way around as well
            assert encoding.encode(b"decoded", "gzip") == b"encoded"
            assert encode_gzip.call_count == 0

            # different encoding
            decode_gzip.return_value = b"bar"
            assert encoding.encode(b"decoded", "deflate") != b"decoded"
            assert encode_gzip.call_count == 0

            # This is not in the cache anymore
            assert encoding.encode(b"decoded", "gzip") == b"encoded"
            assert encode_gzip.call_count == 1
Exemplo n.º 8
0
    def test_get_content_view(self):
        r = cv.get_content_view(
            cv.get("Raw"),
            b"[1, 2, 3]",
            headers=Headers(content_type="application/json")
        )
        assert "Raw" in r[0]

        r = cv.get_content_view(
            cv.get("Auto"),
            b"[1, 2, 3]",
            headers=Headers(content_type="application/json")
        )
        assert r[0] == "JSON"

        r = cv.get_content_view(
            cv.get("Auto"),
            b"[1, 2",
            headers=Headers(content_type="application/json")
        )
        assert "Raw" in r[0]

        r = cv.get_content_view(
            cv.get("Auto"),
            b"[1, 2, 3]",
            headers=Headers(content_type="application/vnd.api+json")
        )
        assert r[0] == "JSON"

        tutils.raises(
            ContentViewException,
            cv.get_content_view,
            cv.get("AMF"),
            b"[1, 2",
            headers=Headers()
        )

        r = cv.get_content_view(
            cv.get("Auto"),
            encoding.encode('gzip', b"[1, 2, 3]"),
            headers=Headers(
                content_type="application/json",
                content_encoding="gzip"
            )
        )
        assert "decoded gzip" in r[0]
        assert "JSON" in r[0]

        r = cv.get_content_view(
            cv.get("XML"),
            encoding.encode('gzip', b"[1, 2, 3]"),
            headers=Headers(
                content_type="application/json",
                content_encoding="gzip"
            )
        )
        assert "decoded gzip" in r[0]
        assert "Raw" in r[0]
Exemplo n.º 9
0
def test_encoders(encoder):
    assert "" == encoding.decode("", encoder)
    assert b"" == encoding.decode(b"", encoder)

    assert "string" == encoding.decode(encoding.encode("string", encoder),
                                       encoder)
    assert b"string" == encoding.decode(encoding.encode(b"string", encoder),
                                        encoder)

    with tutils.raises(ValueError):
        encoding.decode(b"foobar", encoder)
Exemplo n.º 10
0
    def test_get_content_view(self):
        r = cv.get_content_view(
            cv.get("Raw"),
            "[1, 2, 3]",
            headers=Headers(content_type="application/json")
        )
        assert "Raw" in r[0]

        r = cv.get_content_view(
            cv.get("Auto"),
            "[1, 2, 3]",
            headers=Headers(content_type="application/json")
        )
        assert r[0] == "JSON"

        r = cv.get_content_view(
            cv.get("Auto"),
            "[1, 2",
            headers=Headers(content_type="application/json")
        )
        assert "Raw" in r[0]

        tutils.raises(
            ContentViewException,
            cv.get_content_view,
            cv.get("AMF"),
            "[1, 2",
            headers=Headers()
        )

        r = cv.get_content_view(
            cv.get("Auto"),
            encoding.encode('gzip', "[1, 2, 3]"),
            headers=Headers(
                content_type="application/json",
                content_encoding="gzip"
            )
        )
        assert "decoded gzip" in r[0]
        assert "JSON" in r[0]

        r = cv.get_content_view(
            cv.get("XML"),
            encoding.encode('gzip', "[1, 2, 3]"),
            headers=Headers(
                content_type="application/json",
                content_encoding="gzip"
            )
        )
        assert "decoded gzip" in r[0]
        assert "Raw" in r[0]
Exemplo n.º 11
0
def test_deflate():
    assert "string" == encoding.decode(
        "deflate",
        encoding.encode(
            "deflate",
            "string"))
    assert "string" == encoding.decode(
        "deflate",
        encoding.encode(
            "deflate",
            "string")[
            2:-
            4])
    assert None == encoding.decode("deflate", "bogus")
Exemplo n.º 12
0
 def set_content(self, value):
     if value is None:
         self.raw_content = None
         return
     if not isinstance(value, bytes):
         raise TypeError(
             "Message content must be bytes, not {}. "
             "Please use .text if you want to assign a str."
             .format(type(value).__name__)
         )
     ce = self.headers.get("content-encoding")
     cached = (
         self._content_cache.decoded == value and
         self._content_cache.encoding == ce and
         self._content_cache.strict
     )
     if not cached:
         try:
             encoded = encoding.encode(value, ce or "identity")
         except ValueError:
             # So we have an invalid content-encoding?
             # Let's remove it!
             del self.headers["content-encoding"]
             ce = None
             encoded = value
         self._content_cache = CachedDecode(encoded, ce, True, value)
     self.raw_content = self._content_cache.encoded
     self.headers["content-length"] = str(len(self.raw_content))
Exemplo n.º 13
0
def test_gzip():
    assert "string" == encoding.decode(
        "gzip",
        encoding.encode(
            "gzip",
            "string"))
    assert None == encoding.decode("gzip", "bogus")
Exemplo n.º 14
0
def test_deflate():
    assert b"string" == encoding.decode(
        "deflate",
        encoding.encode(
            "deflate",
            b"string"
        )
    )
    assert b"string" == encoding.decode(
        "deflate",
        encoding.encode(
            "deflate",
            b"string"
        )[2:-4]
    )
    assert encoding.decode("deflate", b"bogus") is None
Exemplo n.º 15
0
def test_deflate():
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            "deflate"
        ),
        "deflate"
    )
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            "deflate"
        )[2:-4],
        "deflate"
    )
    with tutils.raises(ValueError):
        encoding.decode(b"bogus", "deflate")
Exemplo n.º 16
0
 def encode(self, e):
     """
         Encodes body with the encoding e, where e is "gzip", "deflate"
         or "identity".
     """
     # FIXME: Error if there's an existing encoding header?
     self.content = encoding.encode(e, self.content)
     self.headers["content-encoding"] = e
Exemplo n.º 17
0
def test_gzip():
    assert b"string" == encoding.decode(
        "gzip",
        encoding.encode(
            "gzip",
            b"string"
        )
    )
    assert encoding.decode("gzip", b"bogus") is None
Exemplo n.º 18
0
def test_gzip():
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            "gzip"
        ),
        "gzip"
    )
    with tutils.raises(ValueError):
        encoding.decode(b"bogus", "gzip")
Exemplo n.º 19
0
def test_brotli():
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            "br"
        ),
        "br"
    )
    with tutils.raises(ValueError):
        encoding.decode(b"bogus", "br")
Exemplo n.º 20
0
def test_encoders(encoder):
    assert "" == encoding.decode("", encoder)
    assert b"" == encoding.decode(b"", encoder)

    assert "string" == encoding.decode(
        encoding.encode(
            "string",
            encoder
        ),
        encoder
    )
    assert b"string" == encoding.decode(
        encoding.encode(
            b"string",
            encoder
        ),
        encoder
    )

    with tutils.raises(ValueError):
        encoding.decode(b"foobar", encoder)
Exemplo n.º 21
0
    def encode(self, e):
        """
            Encodes body with the encoding e, where e is "gzip", "deflate" or "identity".

            Returns:
                True, if decoding succeeded.
                False, otherwise.
        """
        data = encoding.encode(e, self.content)
        if data is None:
            return False
        self.content = data
        self.headers["content-encoding"] = e
        return True
Exemplo n.º 22
0
    def encode(self, e):
        """
            Encodes body with the encoding e, where e is "gzip", "deflate" or "identity".

            Returns:
                True, if decoding succeeded.
                False, otherwise.
        """
        data = encoding.encode(e, self.content)
        if data is None:
            return False
        self.content = data
        self.headers["content-encoding"] = e
        return True
Exemplo n.º 23
0
    def set_text(self, text):
        if text is None:
            self.content = None
            return
        enc = self._guess_encoding()

        try:
            self.content = encoding.encode(text, enc)
        except ValueError:
            # Fall back to UTF-8 and update the content-type header.
            ct = headers.parse_content_type(self.headers.get("content-type", "")) or ("text", "plain", {})
            ct[2]["charset"] = "utf-8"
            self.headers["content-type"] = headers.assemble_content_type(*ct)
            enc = "utf8"
            self.content = text.encode(enc, "replace" if six.PY2 else "surrogateescape")
Exemplo n.º 24
0
    def set_text(self, text):
        if text is None:
            self.content = None
            return
        enc = self._guess_encoding()

        try:
            self.content = encoding.encode(text, enc)
        except ValueError:
            # Fall back to UTF-8 and update the content-type header.
            ct = headers.parse_content_type(
                self.headers.get("content-type", "")) or ("text", "plain", {})
            ct[2]["charset"] = "utf-8"
            self.headers["content-type"] = headers.assemble_content_type(*ct)
            enc = "utf8"
            self.content = text.encode(enc, "surrogateescape")
Exemplo n.º 25
0
 def set_content(self, value):
     if value is None:
         self.raw_content = None
         return
     if not isinstance(value, bytes):
         raise TypeError(
             "Message content must be bytes, not {}. "
             "Please use .text if you want to assign a str.".format(
                 type(value).__name__))
     ce = self.headers.get("content-encoding")
     try:
         self.raw_content = encoding.encode(value, ce or "identity")
     except ValueError:
         # So we have an invalid content-encoding?
         # Let's remove it!
         del self.headers["content-encoding"]
         self.raw_content = value
     self.headers["content-length"] = str(len(self.raw_content))
Exemplo n.º 26
0
 def set_content(self, value):
     if value is None:
         self.raw_content = None
         return
     if not isinstance(value, bytes):
         raise TypeError(
             "Message content must be bytes, not {}. "
             "Please use .text if you want to assign a str."
             .format(type(value).__name__)
         )
     ce = self.headers.get("content-encoding")
     try:
         self.raw_content = encoding.encode(value, ce or "identity")
     except ValueError:
         # So we have an invalid content-encoding?
         # Let's remove it!
         del self.headers["content-encoding"]
         self.raw_content = value
     self.headers["content-length"] = str(len(self.raw_content))
Exemplo n.º 27
0
    def set_text(self, text):
        if text is None:
            self.content = None
            return
        enc = self._guess_encoding()

        cached = (
            self._text_cache.decoded == text and
            self._text_cache.encoding == enc and
            self._text_cache.strict
        )
        if not cached:
            try:
                encoded = encoding.encode(text, enc)
            except ValueError:
                # Fall back to UTF-8 and update the content-type header.
                ct = headers.parse_content_type(self.headers.get("content-type", "")) or ("text", "plain", {})
                ct[2]["charset"] = "utf-8"
                self.headers["content-type"] = headers.assemble_content_type(*ct)
                enc = "utf8"
                encoded = text.encode(enc, "replace" if six.PY2 else "surrogateescape")
            self._text_cache = CachedDecode(encoded, enc, True, text)
        self.content = self._text_cache.encoded
Exemplo n.º 28
0
def test_brotli():
    assert b"string" == encoding.decode(encoding.encode(b"string", "br"), "br")
    with tutils.raises(ValueError):
        encoding.decode(b"bogus", "br")
Exemplo n.º 29
0
    def test_get_content_view(self):
        r = cv.get_content_view(
            cv.get("Raw"),
            Headers(content_type="application/json"),
            "[1, 2, 3]",
            1000,
            False
        )
        assert "Raw" in r[0]

        r = cv.get_content_view(
            cv.get("Auto"),
            Headers(content_type="application/json"),
            "[1, 2, 3]",
            1000,
            False
        )
        assert r[0] == "JSON"

        r = cv.get_content_view(
            cv.get("Auto"),
            Headers(content_type="application/json"),
            "[1, 2",
            1000,
            False
        )
        assert "Raw" in r[0]

        r = cv.get_content_view(
            cv.get("AMF"),
            Headers(),
            "[1, 2",
            1000,
            False
        )
        assert "Raw" in r[0]

        r = cv.get_content_view(
            cv.get("Auto"),
            Headers(
                content_type="application/json",
                content_encoding="gzip"
            ),
            encoding.encode('gzip', "[1, 2, 3]"),
            1000,
            False
        )
        assert "decoded gzip" in r[0]
        assert "JSON" in r[0]

        r = cv.get_content_view(
            cv.get("XML"),
            Headers(
                content_type="application/json",
                content_encoding="gzip"
            ),
            encoding.encode('gzip', "[1, 2, 3]"),
            1000,
            False
        )
        assert "decoded gzip" in r[0]
        assert "Raw" in r[0]
Exemplo n.º 30
0
def test_gzip():
    assert b"string" == encoding.decode(encoding.encode(b"string", "gzip"),
                                        "gzip")
    with tutils.raises(ValueError):
        encoding.decode(b"bogus", "gzip")
Exemplo n.º 31
0
def test_gzip():
    assert b"string" == encoding.decode("gzip",
                                        encoding.encode("gzip", b"string"))
    assert encoding.decode("gzip", b"bogus") is None
Exemplo n.º 32
0
def test_identity():
    assert b"string" == encoding.decode(b"string", "identity")
    assert b"string" == encoding.encode(b"string", "identity")
    with tutils.raises(ValueError):
        encoding.encode(b"string", "nonexistent encoding")
Exemplo n.º 33
0
def test_identity():
    assert b"string" == encoding.decode(b"string", "identity")
    assert b"string" == encoding.encode(b"string", "identity")
    with tutils.raises(ValueError):
        encoding.encode(b"string", "nonexistent encoding")
Exemplo n.º 34
0
    def test_get_content_view(self):
        r = cv.get_content_view(
            cv.get("Raw"),
            [["content-type", "application/json"]],
            "[1, 2, 3]",
            1000,
            False
        )
        assert "Raw" in r[0]

        r = cv.get_content_view(
            cv.get("Auto"),
            [["content-type", "application/json"]],
            "[1, 2, 3]",
            1000,
            False
        )
        assert r[0] == "JSON"

        r = cv.get_content_view(
            cv.get("Auto"),
            [["content-type", "application/json"]],
            "[1, 2",
            1000,
            False
        )
        assert "Raw" in r[0]

        r = cv.get_content_view(
            cv.get("AMF"),
            [],
            "[1, 2",
            1000,
            False
        )
        assert "Raw" in r[0]

        r = cv.get_content_view(
            cv.get("Auto"),
            [
                ["content-type", "application/json"],
                ["content-encoding", "gzip"]
            ],
            encoding.encode('gzip', "[1, 2, 3]"),
            1000,
            False
        )
        assert "decoded gzip" in r[0]
        assert "JSON" in r[0]

        r = cv.get_content_view(
            cv.get("XML"),
            [
                ["content-type", "application/json"],
                ["content-encoding", "gzip"]
            ],
            encoding.encode('gzip', "[1, 2, 3]"),
            1000,
            False
        )
        assert "decoded gzip" in r[0]
        assert "Raw" in r[0]
Exemplo n.º 35
0
def test_gzip():
    assert "string" == encoding.decode("gzip",
                                       encoding.encode("gzip", "string"))
    assert None == encoding.decode("gzip", "bogus")
Exemplo n.º 36
0
def test_identity():
    assert "string" == encoding.decode("identity", "string")
    assert "string" == encoding.encode("identity", "string")
    assert not encoding.encode("nonexistent", "string")
    assert None == encoding.decode("nonexistent encoding", "string")