aboutsummaryrefslogtreecommitdiffstats
path: root/test/netlib/http/test_url.py
blob: 26b37230e33c52a2fd38cc98ab2a31ae0966ce66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from netlib import tutils
from netlib.http import url


def test_parse():
    with tutils.raises(ValueError):
        url.parse("")

    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("http://foo/bar")
    assert s == b"http"
    assert h == b"foo"
    assert po == 80
    assert pa == b"/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(b"http://foo")
    assert pa == b"/"

    s, h, po, pa = url.parse(b"https://foo")
    assert po == 443

    with tutils.raises(ValueError):
        url.parse(b"https://foo:bar")

    # Invalid IDNA
    with tutils.raises(ValueError):
        url.parse("http://\xfafoo")
    # Invalid PATH
    with tutils.raises(ValueError):
        url.parse("http:/\xc6/localhost:56121")
    # Null byte in host
    with tutils.raises(ValueError):
        url.parse("http://foo\0")
    # Port out of range
    _, _, 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('http://lo[calhost')


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.encode([('foo', 'bar')])


def test_urldecode():
    s = "one=two&three=four"
    assert len(url.decode(s)) == 2