diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/http/test_models.py | 8 | ||||
-rw-r--r-- | test/test_encoding.py | 10 | ||||
-rw-r--r-- | test/test_odict.py | 40 | ||||
-rw-r--r-- | test/test_socks.py | 55 | ||||
-rw-r--r-- | test/test_utils.py | 10 |
5 files changed, 49 insertions, 74 deletions
diff --git a/test/http/test_models.py b/test/http/test_models.py index 8fce2e9d..c3ab4d0f 100644 --- a/test/http/test_models.py +++ b/test/http/test_models.py @@ -36,8 +36,8 @@ class TestRequest(object): assert isinstance(req.headers, Headers) def test_equal(self): - a = tutils.treq() - b = tutils.treq() + a = tutils.treq(timestamp_start=42, timestamp_end=43) + b = tutils.treq(timestamp_start=42, timestamp_end=43) assert a == b assert not a == 'foo' @@ -319,8 +319,8 @@ class TestResponse(object): assert isinstance(resp.headers, Headers) def test_equal(self): - a = tutils.tresp() - b = tutils.tresp() + a = tutils.tresp(timestamp_start=42, timestamp_end=43) + b = tutils.tresp(timestamp_start=42, timestamp_end=43) assert a == b assert not a == 'foo' diff --git a/test/test_encoding.py b/test/test_encoding.py index 9da3a38d..90f99338 100644 --- a/test/test_encoding.py +++ b/test/test_encoding.py @@ -2,10 +2,12 @@ from netlib import encoding def test_identity(): - assert "string" == encoding.decode("identity", "string") - assert "string" == encoding.encode("identity", "string") - assert not encoding.encode("nonexistent", "string") - assert None == encoding.decode("nonexistent encoding", "string") + assert b"string" == encoding.decode("identity", b"string") + assert b"string" == encoding.encode("identity", b"string") + assert b"string" == encoding.encode(b"identity", b"string") + assert b"string" == encoding.decode(b"identity", b"string") + assert not encoding.encode("nonexistent", b"string") + assert not encoding.decode("nonexistent encoding", b"string") def test_gzip(): diff --git a/test/test_odict.py b/test/test_odict.py index be3d862d..962c0daa 100644 --- a/test/test_odict.py +++ b/test/test_odict.py @@ -1,7 +1,7 @@ from netlib import odict, tutils -class TestODict: +class TestODict(object): def setUp(self): self.od = odict.ODict() @@ -13,21 +13,10 @@ class TestODict: def test_str_err(self): h = odict.ODict() - tutils.raises(ValueError, h.__setitem__, "key", "foo") - - def test_dictToHeader1(self): - self.od.add("one", "uno") - self.od.add("two", "due") - self.od.add("two", "tre") - expected = [ - "one: uno\r\n", - "two: due\r\n", - "two: tre\r\n", - "\r\n" - ] - out = self.od.format() - for i in expected: - assert out.find(i) >= 0 + with tutils.raises(ValueError): + h["key"] = u"foo" + with tutils.raises(ValueError): + h["key"] = b"foo" def test_getset_state(self): self.od.add("foo", 1) @@ -40,23 +29,6 @@ class TestODict: b.load_state(state) assert b == self.od - def test_dictToHeader2(self): - self.od["one"] = ["uno"] - expected1 = "one: uno\r\n" - expected2 = "\r\n" - out = self.od.format() - assert out.find(expected1) >= 0 - assert out.find(expected2) >= 0 - - def test_match_re(self): - h = odict.ODict() - h.add("one", "uno") - h.add("two", "due") - h.add("two", "tre") - assert h.match_re("uno") - assert h.match_re("two: due") - assert not h.match_re("nonono") - def test_in_any(self): self.od["one"] = ["atwoa", "athreea"] assert self.od.in_any("one", "two") @@ -122,7 +94,7 @@ class TestODict: assert a["a"] == ["b", "b"] -class TestODictCaseless: +class TestODictCaseless(object): def setUp(self): self.od = odict.ODictCaseless() diff --git a/test/test_socks.py b/test/test_socks.py index 3d109f42..65a0f0eb 100644 --- a/test/test_socks.py +++ b/test/test_socks.py @@ -1,12 +1,12 @@ -from cStringIO import StringIO +from io import BytesIO import socket from nose.plugins.skip import SkipTest from netlib import socks, tcp, tutils def test_client_greeting(): - raw = tutils.treader("\x05\x02\x00\xBE\xEF") - out = StringIO() + raw = tutils.treader(b"\x05\x02\x00\xBE\xEF") + out = BytesIO() msg = socks.ClientGreeting.from_file(raw) msg.assert_socks5() msg.to_file(out) @@ -19,11 +19,11 @@ def test_client_greeting(): def test_client_greeting_assert_socks5(): - raw = tutils.treader("\x00\x00") + raw = tutils.treader(b"\x00\x00") msg = socks.ClientGreeting.from_file(raw) tutils.raises(socks.SocksError, msg.assert_socks5) - raw = tutils.treader("HTTP/1.1 200 OK" + " " * 100) + raw = tutils.treader(b"HTTP/1.1 200 OK" + " " * 100) msg = socks.ClientGreeting.from_file(raw) try: msg.assert_socks5() @@ -33,7 +33,7 @@ def test_client_greeting_assert_socks5(): else: assert False - raw = tutils.treader("GET / HTTP/1.1" + " " * 100) + raw = tutils.treader(b"GET / HTTP/1.1" + " " * 100) msg = socks.ClientGreeting.from_file(raw) try: msg.assert_socks5() @@ -43,7 +43,7 @@ def test_client_greeting_assert_socks5(): else: assert False - raw = tutils.treader("XX") + raw = tutils.treader(b"XX") tutils.raises( socks.SocksError, socks.ClientGreeting.from_file, @@ -52,8 +52,8 @@ def test_client_greeting_assert_socks5(): def test_server_greeting(): - raw = tutils.treader("\x05\x02") - out = StringIO() + raw = tutils.treader(b"\x05\x02") + out = BytesIO() msg = socks.ServerGreeting.from_file(raw) msg.assert_socks5() msg.to_file(out) @@ -64,7 +64,7 @@ def test_server_greeting(): def test_server_greeting_assert_socks5(): - raw = tutils.treader("HTTP/1.1 200 OK" + " " * 100) + raw = tutils.treader(b"HTTP/1.1 200 OK" + " " * 100) msg = socks.ServerGreeting.from_file(raw) try: msg.assert_socks5() @@ -74,7 +74,7 @@ def test_server_greeting_assert_socks5(): else: assert False - raw = tutils.treader("GET / HTTP/1.1" + " " * 100) + raw = tutils.treader(b"GET / HTTP/1.1" + " " * 100) msg = socks.ServerGreeting.from_file(raw) try: msg.assert_socks5() @@ -86,36 +86,37 @@ def test_server_greeting_assert_socks5(): def test_message(): - raw = tutils.treader("\x05\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF") - out = StringIO() + raw = tutils.treader(b"\x05\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF") + out = BytesIO() msg = socks.Message.from_file(raw) msg.assert_socks5() - assert raw.read(2) == "\xBE\xEF" + assert raw.read(2) == b"\xBE\xEF" msg.to_file(out) assert out.getvalue() == raw.getvalue()[:-2] assert msg.ver == 5 assert msg.msg == 0x01 assert msg.atyp == 0x03 - assert msg.addr == ("example.com", 0xDEAD) + assert msg.addr == (b"example.com", 0xDEAD) def test_message_assert_socks5(): - raw = tutils.treader("\xEE\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF") + raw = tutils.treader(b"\xEE\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF") msg = socks.Message.from_file(raw) tutils.raises(socks.SocksError, msg.assert_socks5) def test_message_ipv4(): # Test ATYP=0x01 (IPV4) - raw = tutils.treader("\x05\x01\x00\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF") - out = StringIO() + raw = tutils.treader(b"\x05\x01\x00\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF") + out = BytesIO() msg = socks.Message.from_file(raw) - assert raw.read(2) == "\xBE\xEF" + left = raw.read(2) + assert left == b"\xBE\xEF" msg.to_file(out) assert out.getvalue() == raw.getvalue()[:-2] - assert msg.addr == ("127.0.0.1", 0xDEAD) + assert msg.addr == (b"127.0.0.1", 0xDEAD) def test_message_ipv6(): @@ -125,14 +126,14 @@ def test_message_ipv6(): ipv6_addr = "2001:db8:85a3:8d3:1319:8a2e:370:7344" raw = tutils.treader( - "\x05\x01\x00\x04" + + b"\x05\x01\x00\x04" + socket.inet_pton( socket.AF_INET6, ipv6_addr) + - "\xDE\xAD\xBE\xEF") - out = StringIO() + b"\xDE\xAD\xBE\xEF") + out = BytesIO() msg = socks.Message.from_file(raw) - assert raw.read(2) == "\xBE\xEF" + assert raw.read(2) == b"\xBE\xEF" msg.to_file(out) assert out.getvalue() == raw.getvalue()[:-2] @@ -140,13 +141,13 @@ def test_message_ipv6(): def test_message_invalid_rsv(): - raw = tutils.treader("\x05\x01\xFF\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF") + raw = tutils.treader(b"\x05\x01\xFF\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF") tutils.raises(socks.SocksError, socks.Message.from_file, raw) def test_message_unknown_atyp(): - raw = tutils.treader("\x05\x02\x00\x02\x7f\x00\x00\x01\xDE\xAD\xBE\xEF") + raw = tutils.treader(b"\x05\x02\x00\x02\x7f\x00\x00\x01\xDE\xAD\xBE\xEF") tutils.raises(socks.SocksError, socks.Message.from_file, raw) m = socks.Message(5, 1, 0x02, tcp.Address(("example.com", 5050))) - tutils.raises(socks.SocksError, m.to_file, StringIO()) + tutils.raises(socks.SocksError, m.to_file, BytesIO()) diff --git a/test/test_utils.py b/test/test_utils.py index 0db75578..ff27486c 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -84,10 +84,10 @@ def test_parse_url(): def test_unparse_url(): - assert utils.unparse_url("http", "foo.com", 99, "") == "http://foo.com:99" - assert utils.unparse_url("http", "foo.com", 80, "") == "http://foo.com" - assert utils.unparse_url("https", "foo.com", 80, "") == "https://foo.com:80" - assert utils.unparse_url("https", "foo.com", 443, "") == "https://foo.com" + 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" def test_urlencode(): @@ -122,7 +122,7 @@ def test_multipartdecode(): "--{0}\n" "Content-Disposition: form-data; name=\"field2\"\n\n" "value2\n" - "--{0}--".format(boundary).encode("ascii") + "--{0}--".format(boundary.decode()).encode() ) form = utils.multipartdecode(headers, content) |