2015-09-05 16:15:47 +00:00
|
|
|
from netlib import utils, tutils
|
|
|
|
from netlib.http import Headers
|
2015-04-29 21:04:22 +00:00
|
|
|
|
|
|
|
def test_bidi():
|
|
|
|
b = utils.BiDi(a=1, b=2)
|
|
|
|
assert b.a == 1
|
2015-04-30 00:10:08 +00:00
|
|
|
assert b.get_name(1) == "a"
|
|
|
|
assert b.get_name(5) is None
|
2015-04-29 21:04:22 +00:00
|
|
|
tutils.raises(AttributeError, getattr, b, "c")
|
2015-04-30 00:10:08 +00:00
|
|
|
tutils.raises(ValueError, utils.BiDi, one=1, two=1)
|
2015-04-29 21:04:22 +00:00
|
|
|
|
2012-09-23 23:21:48 +00:00
|
|
|
|
|
|
|
def test_hexdump():
|
2015-05-27 09:18:54 +00:00
|
|
|
assert utils.hexdump("one\0" * 10)
|
2012-09-23 23:21:48 +00:00
|
|
|
|
|
|
|
|
2015-09-17 00:14:14 +00:00
|
|
|
def test_clean_bin():
|
2015-09-12 15:03:09 +00:00
|
|
|
assert utils.clean_bin(b"one") == b"one"
|
|
|
|
assert utils.clean_bin(b"\00ne") == b".ne"
|
|
|
|
assert utils.clean_bin(b"\nne") == b"\nne"
|
|
|
|
assert utils.clean_bin(b"\nne", False) == b".ne"
|
|
|
|
assert utils.clean_bin(u"\u2605".encode("utf8")) == b"..."
|
|
|
|
|
|
|
|
assert utils.clean_bin(u"one") == u"one"
|
|
|
|
assert utils.clean_bin(u"\00ne") == u".ne"
|
|
|
|
assert utils.clean_bin(u"\nne") == u"\nne"
|
|
|
|
assert utils.clean_bin(u"\nne", False) == u".ne"
|
|
|
|
assert utils.clean_bin(u"\u2605") == u"\u2605"
|
2015-04-30 00:10:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pretty_size():
|
|
|
|
assert utils.pretty_size(100) == "100B"
|
|
|
|
assert utils.pretty_size(1024) == "1kB"
|
2015-05-27 09:18:54 +00:00
|
|
|
assert utils.pretty_size(1024 + (1024 / 2.0)) == "1.5kB"
|
|
|
|
assert utils.pretty_size(1024 * 1024) == "1MB"
|
2015-08-01 08:39:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_url():
|
2015-09-15 17:12:15 +00:00
|
|
|
with tutils.raises(ValueError):
|
|
|
|
utils.parse_url("")
|
2015-08-01 08:39:14 +00:00
|
|
|
|
2015-09-15 17:12:15 +00:00
|
|
|
s, h, po, pa = utils.parse_url(b"http://foo.com:8888/test")
|
|
|
|
assert s == b"http"
|
|
|
|
assert h == b"foo.com"
|
2015-08-01 08:39:14 +00:00
|
|
|
assert po == 8888
|
2015-09-15 17:12:15 +00:00
|
|
|
assert pa == b"/test"
|
2015-08-01 08:39:14 +00:00
|
|
|
|
|
|
|
s, h, po, pa = utils.parse_url("http://foo/bar")
|
2015-09-15 17:12:15 +00:00
|
|
|
assert s == b"http"
|
|
|
|
assert h == b"foo"
|
2015-08-01 08:39:14 +00:00
|
|
|
assert po == 80
|
2015-09-15 17:12:15 +00:00
|
|
|
assert pa == b"/bar"
|
2015-08-01 08:39:14 +00:00
|
|
|
|
2015-09-15 17:12:15 +00:00
|
|
|
s, h, po, pa = utils.parse_url(b"http://user:pass@foo/bar")
|
|
|
|
assert s == b"http"
|
|
|
|
assert h == b"foo"
|
2015-08-01 08:39:14 +00:00
|
|
|
assert po == 80
|
2015-09-15 17:12:15 +00:00
|
|
|
assert pa == b"/bar"
|
2015-08-01 08:39:14 +00:00
|
|
|
|
2015-09-15 17:12:15 +00:00
|
|
|
s, h, po, pa = utils.parse_url(b"http://foo")
|
|
|
|
assert pa == b"/"
|
2015-08-01 08:39:14 +00:00
|
|
|
|
2015-09-15 17:12:15 +00:00
|
|
|
s, h, po, pa = utils.parse_url(b"https://foo")
|
2015-08-01 08:39:14 +00:00
|
|
|
assert po == 443
|
|
|
|
|
2015-09-15 17:12:15 +00:00
|
|
|
with tutils.raises(ValueError):
|
|
|
|
utils.parse_url(b"https://foo:bar")
|
2015-08-01 08:39:14 +00:00
|
|
|
|
|
|
|
# Invalid IDNA
|
2015-09-15 17:12:15 +00:00
|
|
|
with tutils.raises(ValueError):
|
|
|
|
utils.parse_url("http://\xfafoo")
|
2015-08-01 08:39:14 +00:00
|
|
|
# Invalid PATH
|
2015-09-15 17:12:15 +00:00
|
|
|
with tutils.raises(ValueError):
|
|
|
|
utils.parse_url("http:/\xc6/localhost:56121")
|
2015-08-01 08:39:14 +00:00
|
|
|
# Null byte in host
|
2015-09-15 17:12:15 +00:00
|
|
|
with tutils.raises(ValueError):
|
|
|
|
utils.parse_url("http://foo\0")
|
2015-08-01 08:39:14 +00:00
|
|
|
# Port out of range
|
2015-09-15 17:12:15 +00:00
|
|
|
_, _, port, _ = utils.parse_url("http://foo:999999")
|
|
|
|
assert port == 80
|
2015-08-01 08:39:14 +00:00
|
|
|
# Invalid IPv6 URL - see http://www.ietf.org/rfc/rfc2732.txt
|
2015-09-15 17:12:15 +00:00
|
|
|
with tutils.raises(ValueError):
|
|
|
|
utils.parse_url('http://lo[calhost')
|
2015-08-01 08:39:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_unparse_url():
|
2015-09-17 14:31:50 +00:00
|
|
|
assert utils.unparse_url(b"http", b"foo.com", 99, b"") == b"http://foo.com:99"
|
|
|
|
assert utils.unparse_url(b"http", b"foo.com", 80, b"/bar") == b"http://foo.com/bar"
|
|
|
|
assert utils.unparse_url(b"https", b"foo.com", 80, b"") == b"https://foo.com:80"
|
|
|
|
assert utils.unparse_url(b"https", b"foo.com", 443, b"") == b"https://foo.com"
|
2015-08-01 08:39:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_urlencode():
|
|
|
|
assert utils.urlencode([('foo', 'bar')])
|
|
|
|
|
|
|
|
|
|
|
|
def test_urldecode():
|
|
|
|
s = "one=two&three=four"
|
|
|
|
assert len(utils.urldecode(s)) == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_header_tokens():
|
2015-09-05 16:15:47 +00:00
|
|
|
headers = Headers()
|
|
|
|
assert utils.get_header_tokens(headers, "foo") == []
|
|
|
|
headers["foo"] = "bar"
|
2015-09-17 13:16:12 +00:00
|
|
|
assert utils.get_header_tokens(headers, "foo") == [b"bar"]
|
2015-09-05 16:15:47 +00:00
|
|
|
headers["foo"] = "bar, voing"
|
2015-09-17 13:16:12 +00:00
|
|
|
assert utils.get_header_tokens(headers, "foo") == [b"bar", b"voing"]
|
2015-09-05 16:15:47 +00:00
|
|
|
headers.set_all("foo", ["bar, voing", "oink"])
|
2015-09-17 13:16:12 +00:00
|
|
|
assert utils.get_header_tokens(headers, "foo") == [b"bar", b"voing", b"oink"]
|
2015-08-05 19:32:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_multipartdecode():
|
2015-09-15 17:12:15 +00:00
|
|
|
boundary = b'somefancyboundary'
|
2015-09-05 16:15:47 +00:00
|
|
|
headers = Headers(
|
2015-09-15 17:12:15 +00:00
|
|
|
content_type=b'multipart/form-data; boundary=' + boundary
|
|
|
|
)
|
|
|
|
content = (
|
|
|
|
"--{0}\n"
|
|
|
|
"Content-Disposition: form-data; name=\"field1\"\n\n"
|
|
|
|
"value1\n"
|
|
|
|
"--{0}\n"
|
|
|
|
"Content-Disposition: form-data; name=\"field2\"\n\n"
|
|
|
|
"value2\n"
|
2015-09-17 14:31:50 +00:00
|
|
|
"--{0}--".format(boundary.decode()).encode()
|
2015-09-05 16:15:47 +00:00
|
|
|
)
|
2015-08-05 19:32:53 +00:00
|
|
|
|
|
|
|
form = utils.multipartdecode(headers, content)
|
|
|
|
|
|
|
|
assert len(form) == 2
|
2015-09-15 17:12:15 +00:00
|
|
|
assert form[0] == (b"field1", b"value1")
|
|
|
|
assert form[1] == (b"field2", b"value2")
|
2015-08-05 19:32:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_content_type():
|
|
|
|
p = utils.parse_content_type
|
2015-09-17 13:16:12 +00:00
|
|
|
assert p(b"text/html") == (b"text", b"html", {})
|
|
|
|
assert p(b"text") is None
|
2015-08-05 19:32:53 +00:00
|
|
|
|
2015-09-17 13:16:12 +00:00
|
|
|
v = p(b"text/html; charset=UTF-8")
|
|
|
|
assert v == (b'text', b'html', {b'charset': b'UTF-8'})
|