From 4e6c9c4e935458d23add259dc63c5e0a85fba9c8 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 31 May 2016 18:42:56 +1200 Subject: Extract url functions from netlib.utils and move to netlib.http.url --- test/netlib/http/test_url.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/netlib/http/test_url.py (limited to 'test/netlib/http/test_url.py') diff --git a/test/netlib/http/test_url.py b/test/netlib/http/test_url.py new file mode 100644 index 00000000..d777a949 --- /dev/null +++ b/test/netlib/http/test_url.py @@ -0,0 +1,65 @@ +from netlib import tutils +from netlib.http import url + +def test_parse_url(): + with tutils.raises(ValueError): + url.parse_url("") + + s, h, po, pa = url.parse_url(b"http://foo.com:8888/test") + assert s == b"http" + assert h == b"foo.com" + assert po == 8888 + assert pa == b"/test" + + s, h, po, pa = url.parse_url("http://foo/bar") + assert s == b"http" + assert h == b"foo" + assert po == 80 + assert pa == b"/bar" + + s, h, po, pa = url.parse_url(b"http://user:pass@foo/bar") + assert s == b"http" + assert h == b"foo" + assert po == 80 + assert pa == b"/bar" + + s, h, po, pa = url.parse_url(b"http://foo") + assert pa == b"/" + + s, h, po, pa = url.parse_url(b"https://foo") + assert po == 443 + + with tutils.raises(ValueError): + url.parse_url(b"https://foo:bar") + + # Invalid IDNA + with tutils.raises(ValueError): + url.parse_url("http://\xfafoo") + # Invalid PATH + with tutils.raises(ValueError): + url.parse_url("http:/\xc6/localhost:56121") + # Null byte in host + with tutils.raises(ValueError): + url.parse_url("http://foo\0") + # Port out of range + _, _, port, _ = url.parse_url("http://foo:999999") + assert port == 80 + # Invalid IPv6 URL - see http://www.ietf.org/rfc/rfc2732.txt + with tutils.raises(ValueError): + url.parse_url('http://lo[calhost') + + +def test_unparse_url(): + assert url.unparse_url("http", "foo.com", 99, "") == "http://foo.com:99" + assert url.unparse_url("http", "foo.com", 80, "/bar") == "http://foo.com/bar" + assert url.unparse_url("https", "foo.com", 80, "") == "https://foo.com:80" + assert url.unparse_url("https", "foo.com", 443, "") == "https://foo.com" + + +def test_urlencode(): + assert url.urlencode([('foo', 'bar')]) + + +def test_urldecode(): + s = "one=two&three=four" + assert len(url.urldecode(s)) == 2 -- cgit v1.2.3 From 6dda2b2ee544c3890f04b7bf99272998e29992b6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 31 May 2016 18:46:19 +1200 Subject: Module is part of the name - url.decode, not url.urldecode A pattern we need to use far more often in the codebase --- test/netlib/http/test_url.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'test/netlib/http/test_url.py') diff --git a/test/netlib/http/test_url.py b/test/netlib/http/test_url.py index d777a949..697c83a8 100644 --- a/test/netlib/http/test_url.py +++ b/test/netlib/http/test_url.py @@ -1,65 +1,65 @@ from netlib import tutils from netlib.http import url -def test_parse_url(): +def test_parse(): with tutils.raises(ValueError): - url.parse_url("") + url.parse("") - s, h, po, pa = url.parse_url(b"http://foo.com:8888/test") + s, h, po, pa = url.parse(b"http://foo.com:8888/test") assert s == b"http" assert h == b"foo.com" assert po == 8888 assert pa == b"/test" - s, h, po, pa = url.parse_url("http://foo/bar") + s, h, po, pa = url.parse("http://foo/bar") assert s == b"http" assert h == b"foo" assert po == 80 assert pa == b"/bar" - s, h, po, pa = url.parse_url(b"http://user:pass@foo/bar") + s, h, po, pa = url.parse(b"http://user:pass@foo/bar") assert s == b"http" assert h == b"foo" assert po == 80 assert pa == b"/bar" - s, h, po, pa = url.parse_url(b"http://foo") + s, h, po, pa = url.parse(b"http://foo") assert pa == b"/" - s, h, po, pa = url.parse_url(b"https://foo") + s, h, po, pa = url.parse(b"https://foo") assert po == 443 with tutils.raises(ValueError): - url.parse_url(b"https://foo:bar") + url.parse(b"https://foo:bar") # Invalid IDNA with tutils.raises(ValueError): - url.parse_url("http://\xfafoo") + url.parse("http://\xfafoo") # Invalid PATH with tutils.raises(ValueError): - url.parse_url("http:/\xc6/localhost:56121") + url.parse("http:/\xc6/localhost:56121") # Null byte in host with tutils.raises(ValueError): - url.parse_url("http://foo\0") + url.parse("http://foo\0") # Port out of range - _, _, port, _ = url.parse_url("http://foo:999999") + _, _, port, _ = url.parse("http://foo:999999") assert port == 80 # Invalid IPv6 URL - see http://www.ietf.org/rfc/rfc2732.txt with tutils.raises(ValueError): - url.parse_url('http://lo[calhost') + url.parse('http://lo[calhost') -def test_unparse_url(): - assert url.unparse_url("http", "foo.com", 99, "") == "http://foo.com:99" - assert url.unparse_url("http", "foo.com", 80, "/bar") == "http://foo.com/bar" - assert url.unparse_url("https", "foo.com", 80, "") == "https://foo.com:80" - assert url.unparse_url("https", "foo.com", 443, "") == "https://foo.com" +def test_unparse(): + assert url.unparse("http", "foo.com", 99, "") == "http://foo.com:99" + assert url.unparse("http", "foo.com", 80, "/bar") == "http://foo.com/bar" + assert url.unparse("https", "foo.com", 80, "") == "https://foo.com:80" + assert url.unparse("https", "foo.com", 443, "") == "https://foo.com" def test_urlencode(): - assert url.urlencode([('foo', 'bar')]) + assert url.encode([('foo', 'bar')]) def test_urldecode(): s = "one=two&three=four" - assert len(url.urldecode(s)) == 2 + assert len(url.decode(s)) == 2 -- cgit v1.2.3 From 40a030f215e1943aefdb2eb6fe2a264b9b1ee33c Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 31 May 2016 19:58:28 +1200 Subject: Satisfy flake8 --- test/netlib/http/test_url.py | 1 + 1 file changed, 1 insertion(+) (limited to 'test/netlib/http/test_url.py') diff --git a/test/netlib/http/test_url.py b/test/netlib/http/test_url.py index 697c83a8..26b37230 100644 --- a/test/netlib/http/test_url.py +++ b/test/netlib/http/test_url.py @@ -1,6 +1,7 @@ from netlib import tutils from netlib.http import url + def test_parse(): with tutils.raises(ValueError): url.parse("") -- cgit v1.2.3