Exemplo n.º 1
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.º 2
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.º 3
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.º 4
0
def test_encoders_strings(encoder):
    """
        This test is for testing byte->str decoding
        and str->byte encoding
    """
    assert "" == encoding.decode(b"", encoder)

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

    with pytest.raises(TypeError):
        encoding.encode(b"string", encoder)

    with pytest.raises(TypeError):
        encoding.decode("foobar", encoder)
Exemplo n.º 5
0
def test_encoders(encoder):
    """
        This test is for testing byte->byte encoding/decoding
    """
    assert encoding.decode(None, encoder) is None
    assert encoding.encode(None, encoder) is None

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

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

    with pytest.raises(TypeError):
        encoding.encode("string", encoder)

    with pytest.raises(TypeError):
        encoding.decode("string", encoder)
    with pytest.raises(ValueError):
        encoding.decode(b"foobar", encoder)
Exemplo n.º 6
0
def test_encoders_strings(encoder):
    """
        This test is for testing byte->str decoding
        and str->byte encoding
    """
    assert "" == encoding.decode(b"", encoder)

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

    with pytest.raises(TypeError):
        encoding.encode(b"string", encoder)

    with pytest.raises(TypeError):
        encoding.decode("foobar", encoder)
Exemplo n.º 7
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.º 8
0
def test_encoders(encoder):
    """
        This test is for testing byte->byte encoding/decoding
    """
    assert encoding.decode(None, encoder) is None
    assert encoding.encode(None, encoder) is None

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

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

    with pytest.raises(TypeError):
        encoding.encode("string", encoder)

    with pytest.raises(TypeError):
        encoding.decode("string", encoder)
    with pytest.raises(ValueError):
        encoding.decode(b"foobar", encoder)
Exemplo n.º 9
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.º 10
0
 def set_content(self, value: Optional[bytes]) -> None:
     if value is None:
         self.raw_content = None
         return
     if not isinstance(value, bytes):
         raise TypeError(
             f"Message content must be bytes, not {type(value).__name__}. "
             "Please use .text if you want to assign a str.")
     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.º 11
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.º 12
0
def test_identity(encoder):
    assert b"string" == encoding.decode(b"string", encoder)
    assert b"string" == encoding.encode(b"string", encoder)
    with pytest.raises(ValueError):
        encoding.encode(b"string", "nonexistent encoding")
Exemplo n.º 13
0
def test_identity(encoder):
    assert b"string" == encoding.decode(b"string", encoder)
    assert b"string" == encoding.encode(b"string", encoder)
    with pytest.raises(ValueError):
        encoding.encode(b"string", "nonexistent encoding")