test_encoding.py 1.0 KB

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