This commit is contained in:
Maximilian Hils 2016-07-07 18:02:59 -07:00
parent 76473d44e0
commit 00dce24015
2 changed files with 16 additions and 1 deletions

View File

@ -119,7 +119,7 @@ def is_mostly_bin(s):
return sum( return sum(
i < 9 or 13 < i < 32 or 126 < i i < 9 or 13 < i < 32 or 126 < i
for i in six.iterbytes(s[:100]) for i in six.iterbytes(s[:100])
) > 30 ) / len(s[:100]) > 0.3
def is_xml(s): def is_xml(s):

View File

@ -3,6 +3,13 @@ import six
from netlib import strutils, tutils from netlib import strutils, tutils
def test_always_bytes():
assert strutils.always_bytes(bytes(bytearray(range(256)))) == bytes(bytearray(range(256)))
assert strutils.always_bytes("foo") == b"foo"
with tutils.raises(ValueError):
strutils.always_bytes(u"\u2605", "ascii")
def test_native(): def test_native():
with tutils.raises(TypeError): with tutils.raises(TypeError):
strutils.native(42) strutils.native(42)
@ -31,6 +38,9 @@ def test_escape_control_characters():
u'=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~.' u'=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~.'
) )
with tutils.raises(ValueError):
strutils.escape_control_characters(b"foo")
def test_bytes_to_escaped_str(): def test_bytes_to_escaped_str():
assert strutils.bytes_to_escaped_str(b"foo") == "foo" assert strutils.bytes_to_escaped_str(b"foo") == "foo"
@ -68,6 +78,11 @@ def test_escaped_str_to_bytes():
strutils.escaped_str_to_bytes(b"very byte") strutils.escaped_str_to_bytes(b"very byte")
def test_is_mostly_bin():
assert not strutils.is_mostly_bin(b"foo\xFF")
assert strutils.is_mostly_bin(b"foo" + b"\xFF" * 10)
def test_is_xml(): def test_is_xml():
assert not strutils.is_xml(b"foo") assert not strutils.is_xml(b"foo")
assert strutils.is_xml(b"<foo") assert strutils.is_xml(b"<foo")