mitmproxy/test/netlib/test_strutils.py
Aldo Cortesi cccdc98426 Utils reorganisation: add netlib.strutils
Extract a number of string and format-related functions to netlib.strutils.
2016-06-02 12:31:41 +12:00

41 lines
1.5 KiB
Python

# coding=utf-8
from netlib import strutils
def test_clean_bin():
assert strutils.clean_bin(b"one") == b"one"
assert strutils.clean_bin(b"\00ne") == b".ne"
assert strutils.clean_bin(b"\nne") == b"\nne"
assert strutils.clean_bin(b"\nne", False) == b".ne"
assert strutils.clean_bin(u"\u2605".encode("utf8")) == b"..."
assert strutils.clean_bin(u"one") == u"one"
assert strutils.clean_bin(u"\00ne") == u".ne"
assert strutils.clean_bin(u"\nne") == u"\nne"
assert strutils.clean_bin(u"\nne", False) == u".ne"
assert strutils.clean_bin(u"\u2605") == u"\u2605"
def test_safe_subn():
assert strutils.safe_subn("foo", u"bar", "\xc2foo")
def test_bytes_to_escaped_str():
assert strutils.bytes_to_escaped_str(b"foo") == "foo"
assert strutils.bytes_to_escaped_str(b"\b") == r"\x08"
assert strutils.bytes_to_escaped_str(br"&!?=\)") == r"&!?=\\)"
assert strutils.bytes_to_escaped_str(b'\xc3\xbc') == r"\xc3\xbc"
assert strutils.bytes_to_escaped_str(b"'") == r"\'"
assert strutils.bytes_to_escaped_str(b'"') == r'"'
def test_escaped_str_to_bytes():
assert strutils.escaped_str_to_bytes("foo") == b"foo"
assert strutils.escaped_str_to_bytes("\x08") == b"\b"
assert strutils.escaped_str_to_bytes("&!?=\\\\)") == br"&!?=\)"
assert strutils.escaped_str_to_bytes("ü") == b'\xc3\xbc'
assert strutils.escaped_str_to_bytes(u"\\x08") == b"\b"
assert strutils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)"
assert strutils.escaped_str_to_bytes(u"ü") == b'\xc3\xbc'