-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pyfaup.py
More file actions
140 lines (115 loc) · 4.84 KB
/
test_pyfaup.py
File metadata and controls
140 lines (115 loc) · 4.84 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
from __future__ import annotations
import unittest
from pyfaup import Host, Hostname, Url
class TestPyFaupRR(unittest.TestCase):
def test_http_url(self) -> None:
parsed_url = Url(
"https://user:pass@sub.example.com:8080/path?query=value#fragment"
)
self.assertEqual(
parsed_url.orig,
"https://user:pass@sub.example.com:8080/path?query=value#fragment",
)
self.assertEqual(parsed_url.scheme, "https")
self.assertEqual(parsed_url.username, "user")
self.assertEqual(parsed_url.password, "pass")
self.assertEqual(str(parsed_url.host), "sub.example.com")
if parsed_url.host is not None:
self.assertEqual(parsed_url.host.subdomain(), "sub")
if parsed_url.host is not None:
self.assertEqual(parsed_url.host.domain(), "example.com")
if parsed_url.host is not None:
self.assertEqual(parsed_url.host.suffix(), "com")
self.assertEqual(parsed_url.port, 8080)
self.assertEqual(parsed_url.path, "/path")
self.assertEqual(parsed_url.query, "query=value")
self.assertEqual(parsed_url.fragment, "fragment")
def test_file_url(self) -> None:
u = Url("file:///tmp/test.txt")
self.assertEqual(u.scheme, "file")
self.assertEqual(u.host, None)
self.assertEqual(u.path, "/tmp/test.txt")
def test_hostname(self) -> None:
hn = Hostname("sub.example.com")
self.assertEqual(hn.subdomain, "sub")
self.assertEqual(hn.domain, "example.com")
self.assertEqual(str(hn.suffix), "com")
if hn.suffix is not None:
self.assertTrue(hn.suffix.is_known())
self.assertEqual(str(hn), "sub.example.com")
with self.assertRaises(ValueError):
Hostname("192.168.1.1")
def test_unknown_suffix(self) -> None:
hn = Hostname("test.custom-tld")
if hn.suffix is not None:
self.assertFalse(hn.suffix.is_known())
hn = Hostname("SSH-2.0-OpenSSH_9.2p1")
if hn.suffix is not None:
self.assertFalse(hn.suffix.is_known())
hn = Hostname("laptop.local")
if hn.suffix is not None:
self.assertFalse(hn.suffix.is_known())
def test_host(self) -> None:
# Test hostname
host = Host("sub.example.com")
self.assertTrue(host.is_hostname())
self.assertFalse(host.is_ip_addr())
self.assertFalse(host.is_ipv4())
self.assertFalse(host.is_ipv6())
self.assertEqual(str(host), "sub.example.com")
# Test IPv4
host = Host("192.168.1.1")
self.assertFalse(host.is_hostname())
self.assertTrue(host.is_ip_addr())
self.assertTrue(host.is_ipv4())
self.assertFalse(host.is_ipv6())
self.assertEqual(str(host), "192.168.1.1")
self.assertEqual(host.try_into_ip(), "192.168.1.1")
with self.assertRaises(ValueError):
host.try_into_hostname()
# Test IPv6
host = Host("::1")
self.assertFalse(host.is_hostname())
self.assertTrue(host.is_ip_addr())
self.assertFalse(host.is_ipv4())
self.assertTrue(host.is_ipv6())
self.assertEqual(str(host), "::1")
self.assertEqual(host.try_into_ip(), "::1")
with self.assertRaises(ValueError):
host.try_into_hostname()
# Test hostname conversion
host = Host("sub.example.com")
hn = host.try_into_hostname()
self.assertEqual(str(hn), "sub.example.com")
self.assertEqual(hn.subdomain, "sub")
self.assertEqual(hn.domain, "example.com")
self.assertEqual(str(hn.suffix), "com")
with self.assertRaises(ValueError):
host.try_into_ip()
# Test domain(), subdomain(), and suffix() methods
host = Host("sub.example.com")
self.assertEqual(host.domain(), "example.com")
self.assertEqual(host.subdomain(), "sub")
self.assertIsNotNone(host.suffix())
self.assertEqual(str(host.suffix()), "com")
# Test domain(), subdomain(), and suffix() with simple domain
host = Host("example.com")
self.assertEqual(host.domain(), "example.com")
self.assertIsNone(host.subdomain())
self.assertIsNotNone(host.suffix())
self.assertEqual(str(host.suffix()), "com")
# Test domain(), subdomain(), and suffix() with IP addresses
host = Host("192.168.1.1")
self.assertIsNone(host.domain())
self.assertIsNone(host.subdomain())
self.assertIsNone(host.suffix())
host = Host("::1")
self.assertIsNone(host.domain())
self.assertIsNone(host.subdomain())
self.assertIsNone(host.suffix())
# Test invalid host
with self.assertRaises(ValueError):
Host("invalid host")
if __name__ == "__main__":
unittest.main()