test_encoding.py 1004 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import pytest
  2. from itsdangerous.encoding import base64_decode
  3. from itsdangerous.encoding import base64_encode
  4. from itsdangerous.encoding import bytes_to_int
  5. from itsdangerous.encoding import int_to_bytes
  6. from itsdangerous.encoding import want_bytes
  7. from itsdangerous.exc import BadData
  8. @pytest.mark.parametrize("value", ("mañana", b"tomorrow"))
  9. def test_want_bytes(value):
  10. out = want_bytes(value)
  11. assert isinstance(out, bytes)
  12. @pytest.mark.parametrize("value", ("無限", b"infinite"))
  13. def test_base64(value):
  14. enc = base64_encode(value)
  15. assert isinstance(enc, bytes)
  16. dec = base64_decode(enc)
  17. assert dec == want_bytes(value)
  18. def test_base64_bad():
  19. with pytest.raises(BadData):
  20. base64_decode("12345")
  21. @pytest.mark.parametrize(
  22. ("value", "expect"), ((0, b""), (192, b"\xc0"), (18446744073709551615, b"\xff" * 8))
  23. )
  24. def test_int_bytes(value, expect):
  25. enc = int_to_bytes(value)
  26. assert enc == expect
  27. dec = bytes_to_int(enc)
  28. assert dec == value