Example #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")
Example #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
Example #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")
Example #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
Example #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]
Example #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
    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]
Example #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)
Example #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]
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")
Example #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))
def test_gzip():
    assert "string" == encoding.decode(
        "gzip",
        encoding.encode(
            "gzip",
            "string"))
    assert None == encoding.decode("gzip", "bogus")
Example #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
Example #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")
Example #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
Example #17
0
def test_gzip():
    assert b"string" == encoding.decode(
        "gzip",
        encoding.encode(
            "gzip",
            b"string"
        )
    )
    assert encoding.decode("gzip", b"bogus") is None
Example #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")
Example #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")
Example #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)
Example #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
Example #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
Example #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")
Example #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")
Example #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))
Example #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))
Example #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
Example #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")
    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]
Example #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")
Example #31
0
def test_gzip():
    assert b"string" == encoding.decode("gzip",
                                        encoding.encode("gzip", b"string"))
    assert encoding.decode("gzip", b"bogus") is None
Example #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")
Example #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")
    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]
Example #35
0
def test_gzip():
    assert "string" == encoding.decode("gzip",
                                       encoding.encode("gzip", "string"))
    assert None == encoding.decode("gzip", "bogus")
Example #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")