source: uri/tests/test_normalizers.py

Last change on this file was 230, checked in by wouter, 3 years ago

#91 clone https://pypi.org/project/rfc3986/

File size: 3.4 KB
RevLine 
[230]1# -*- coding: utf-8 -*-
2import pytest
3
4from rfc3986.uri import URIReference
5from rfc3986.normalizers import (
6 normalize_scheme,
7 normalize_percent_characters,
8 remove_dot_segments,
9 encode_component,
10 normalize_host,
11)
12
13
14def test_normalize_scheme():
15 assert "http" == normalize_scheme("htTp")
16 assert "http" == normalize_scheme("http")
17 assert "http" == normalize_scheme("HTTP")
18
19
20def test_normalize_percent_characters():
21 expected = "%3Athis_should_be_lowercase%DF%AB%4C"
22 assert expected == normalize_percent_characters(
23 "%3athis_should_be_lowercase%DF%ab%4c"
24 )
25 assert expected == normalize_percent_characters(
26 "%3Athis_should_be_lowercase%DF%AB%4C"
27 )
28 assert expected == normalize_percent_characters(
29 "%3Athis_should_be_lowercase%DF%aB%4C"
30 )
31
32
33paths = [
34 # (Input, expected output)
35 ("/foo/bar/.", "/foo/bar/"),
36 ("/foo/bar/", "/foo/bar/"),
37 ("/foo/bar", "/foo/bar"),
38 ("./foo/bar", "foo/bar"),
39 ("/./foo/bar", "/foo/bar"),
40 ("/foo%20bar/biz%2Abaz", "/foo%20bar/biz%2Abaz"),
41 ("../foo/bar", "foo/bar"),
42 ("/../foo/bar", "/foo/bar"),
43 ("a/./b/../b/%63/%7Bfoo%7D", "a/b/%63/%7Bfoo%7D"),
44 ("//a/./b/../b/%63/%7Bfoo%7D", "//a/b/%63/%7Bfoo%7D"),
45 ("mid/content=5/../6", "mid/6"),
46 ("/a/b/c/./../../g", "/a/g"),
47]
48
49
50@pytest.fixture(params=paths)
51def path_fixture(request):
52 return request.param
53
54
55@pytest.fixture(params=paths)
56def uris(request):
57 to_norm, normalized = request.param
58 return (
59 URIReference(None, None, to_norm, None, None),
60 URIReference(None, None, normalized, None, None),
61 )
62
63
64def test_remove_dot_segments(path_fixture):
65 to_normalize, expected = path_fixture
66 assert expected == remove_dot_segments(to_normalize)
67
68
69def test_normalized_equality(uris):
70 assert uris[0] == uris[1]
71
72
73def test_hostname_normalization():
74 assert URIReference(
75 None, "EXAMPLE.COM", None, None, None
76 ) == URIReference(None, "example.com", None, None, None)
77
78
79@pytest.mark.parametrize(
80 ["authority", "expected_authority"],
81 [
82 ("user%2aName@EXAMPLE.COM", "user%2AName@example.com"),
83 ("[::1%eth0]", "[::1%25eth0]"),
84 ],
85)
86def test_authority_normalization(authority, expected_authority):
87 uri = URIReference(None, authority, None, None, None).normalize()
88 assert uri.authority == expected_authority
89
90
91def test_fragment_normalization():
92 uri = URIReference(None, "example.com", None, None, "fiz%DF").normalize()
93 assert uri.fragment == "fiz%DF"
94
95
96@pytest.mark.parametrize(
97 ["component", "encoded_component"],
98 [
99 ("/%", "/%25"),
100 ("/~", "/~"),
101 ("/%a", "/%25a"),
102 ("/%ag", "/%25ag"),
103 ("/%af", "/%af"),
104 ("/%20/%", "/%2520/%25"),
105 ("/%20%25", "/%20%25"),
106 ("/%21%22%23%ah%12%ff", "/%2521%2522%2523%25ah%2512%25ff"),
107 ],
108)
109def test_detect_percent_encoded_component(component, encoded_component):
110 assert encode_component(component, "utf-8") == encoded_component
111
112
113@pytest.mark.parametrize(
114 ["host", "normalized_host"],
115 [
116 ("LOCALHOST", "localhost"),
117 ("[::1%eth0]", "[::1%25eth0]"),
118 ("[::1%25]", "[::1%2525]"),
119 ("[::1%%25]", "[::1%25%25]"),
120 ("[::1%25%25]", "[::1%25%25]"),
121 ("[::Af%Ff]", "[::af%25Ff]"),
122 ("[::Af%%Ff]", "[::af%25%Ff]"),
123 ("[::Af%25Ff]", "[::af%25Ff]"),
124 ],
125)
126def test_normalize_host(host, normalized_host):
127 assert normalize_host(host) == normalized_host
Note: See TracBrowser for help on using the repository browser.