• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import httplib2
2import pytest
3
4
5def test_from_std66():
6    cases = (
7        ("http://example.com", ("http", "example.com", "", None, None)),
8        ("https://example.com", ("https", "example.com", "", None, None)),
9        ("https://example.com:8080", ("https", "example.com:8080", "", None, None)),
10        ("http://example.com/", ("http", "example.com", "/", None, None)),
11        ("http://example.com/path", ("http", "example.com", "/path", None, None)),
12        (
13            "http://example.com/path?a=1&b=2",
14            ("http", "example.com", "/path", "a=1&b=2", None),
15        ),
16        (
17            "http://example.com/path?a=1&b=2#fred",
18            ("http", "example.com", "/path", "a=1&b=2", "fred"),
19        ),
20        (
21            "http://example.com/path?a=1&b=2#fred",
22            ("http", "example.com", "/path", "a=1&b=2", "fred"),
23        ),
24    )
25    for a, b in cases:
26        assert httplib2.parse_uri(a) == b
27
28
29def test_norm():
30    cases = (
31        ("http://example.org", "http://example.org/"),
32        ("http://EXAMple.org", "http://example.org/"),
33        ("http://EXAMple.org?=b", "http://example.org/?=b"),
34        ("http://EXAMple.org/mypath?a=b", "http://example.org/mypath?a=b"),
35        ("http://localhost:80", "http://localhost:80/"),
36    )
37    for a, b in cases:
38        assert httplib2.urlnorm(a)[-1] == b
39
40    assert httplib2.urlnorm("http://localhost:80/") == httplib2.urlnorm(
41        "HTTP://LOCALHOST:80"
42    )
43
44    try:
45        httplib2.urlnorm("/")
46        assert False, "Non-absolute URIs should raise an exception"
47    except httplib2.RelativeURIError:
48        pass
49
50
51@pytest.mark.parametrize(
52    "data",
53    (
54        ("", ",d41d8cd98f00b204e9800998ecf8427e"),
55        (
56            "http://example.org/fred/?a=b",
57            "example.orgfreda=b,58489f63a7a83c3b7794a6a398ee8b1f",
58        ),
59        (
60            "http://example.org/fred?/a=b",
61            "example.orgfreda=b,8c5946d56fec453071f43329ff0be46b",
62        ),
63        (
64            "http://www.example.org/fred?/a=b",
65            "www.example.orgfreda=b,499c44b8d844a011b67ea2c015116968",
66        ),
67        (
68            "https://www.example.org/fred?/a=b",
69            "www.example.orgfreda=b,692e843a333484ce0095b070497ab45d",
70        ),
71        (
72            httplib2.urlnorm("http://WWW")[-1],
73            httplib2.safename(httplib2.urlnorm("http://www")[-1]),
74        ),
75        (
76            u"http://\u2304.org/fred/?a=b",
77            ".orgfreda=b,ecaf0f97756c0716de76f593bd60a35e",
78        ),
79        (
80            "normal-resource-name.js",
81            "normal-resource-name.js,8ff7c46fd6e61bf4e91a0a1606954a54",
82        ),
83        (
84            "foo://dom/path/brath/carapath",
85            "dompathbrathcarapath,83db942781ed975c7a5b7c24039f8ca3",
86        ),
87        ("with/slash", "withslash,17cc656656bb8ce2411bd41ead56d176"),
88        (
89            "thisistoomuch" * 42,
90            ("thisistoomuch" * 6) + "thisistoomuc,c4553439dd179422c6acf6a8ac093eb6",
91        ),
92        (u"\u043f\u0440", ",9f18c0db74a9734e9d18461e16345083"),
93        (u"\u043f\u0440".encode("utf-8"), ",9f18c0db74a9734e9d18461e16345083"),
94        (
95            b"column\tvalues/unstr.zip",
96            "columnvaluesunstr.zip,b9740dcd0553e11b526450ceb8f76683",
97        ),
98    ),
99    ids=str,
100)
101def test_safename(data):
102    result = httplib2.safename(data[0])
103    assert result == data[1]
104
105
106def test_safename2():
107    assert httplib2.safename("http://www") != httplib2.safename("https://www")
108
109    # Test the max length limits
110    uri = "http://" + ("w" * 200) + ".org"
111    uri2 = "http://" + ("w" * 201) + ".org"
112    assert httplib2.safename(uri) != httplib2.safename(uri2)
113    # Max length should be 90 + 1 (',') + 32 = 123
114    assert len(httplib2.safename(uri2)) == 123
115    assert len(httplib2.safename(uri)) == 123
116