@@ -4,21 +4,107 @@ from lightbug_http.strings import NetworkType
44
55
66def test_split_host_port ():
7- # IPv4
8- var hp = parse_address(" 127.0.0.1:8080" )
7+ # TCP4
8+ var hp = parse_address(NetworkType.tcp4, " 127.0.0.1:8080" )
99 testing.assert_equal(hp[0 ], " 127.0.0.1" )
1010 testing.assert_equal(hp[1 ], 8080 )
1111
12- # IPv6
13- hp = parse_address(" [::1]:8080" )
12+ # TCP4 with localhost
13+ hp = parse_address(NetworkType.tcp4, " localhost:8080" )
14+ testing.assert_equal(hp[0 ], " 127.0.0.1" )
15+ testing.assert_equal(hp[1 ], 8080 )
16+
17+ # TCP6
18+ hp = parse_address(NetworkType.tcp6, " [::1]:8080" )
1419 testing.assert_equal(hp[0 ], " ::1" )
1520 testing.assert_equal(hp[1 ], 8080 )
1621
17- # # TODO : IPv6 long form - Not supported yet.
22+ # TCP6 with localhost
23+ hp = parse_address(NetworkType.tcp6, " localhost:8080" )
24+ testing.assert_equal(hp[0 ], " ::1" )
25+ testing.assert_equal(hp[1 ], 8080 )
26+
27+ # UDP4
28+ hp = parse_address(NetworkType.udp4, " 192.168.1.1:53" )
29+ testing.assert_equal(hp[0 ], " 192.168.1.1" )
30+ testing.assert_equal(hp[1 ], 53 )
31+
32+ # UDP4 with localhost
33+ hp = parse_address(NetworkType.udp4, " localhost:53" )
34+ testing.assert_equal(hp[0 ], " 127.0.0.1" )
35+ testing.assert_equal(hp[1 ], 53 )
36+
37+ # UDP6
38+ hp = parse_address(NetworkType.udp6, " [2001:db8::1]:53" )
39+ testing.assert_equal(hp[0 ], " 2001:db8::1" )
40+ testing.assert_equal(hp[1 ], 53 )
41+
42+ # UDP6 with localhost
43+ hp = parse_address(NetworkType.udp6, " localhost:53" )
44+ testing.assert_equal(hp[0 ], " ::1" )
45+ testing.assert_equal(hp[1 ], 53 )
46+
47+ # IP4 (no port)
48+ hp = parse_address(NetworkType.ip4, " 192.168.1.1" )
49+ testing.assert_equal(hp[0 ], " 192.168.1.1" )
50+ testing.assert_equal(hp[1 ], 0 )
51+
52+ # IP4 with localhost
53+ hp = parse_address(NetworkType.ip4, " localhost" )
54+ testing.assert_equal(hp[0 ], " 127.0.0.1" )
55+ testing.assert_equal(hp[1 ], 0 )
56+
57+ # IP6 (no port)
58+ hp = parse_address(NetworkType.ip6, " 2001:db8::1" )
59+ testing.assert_equal(hp[0 ], " 2001:db8::1" )
60+ testing.assert_equal(hp[1 ], 0 )
61+
62+ # IP6 with localhost
63+ hp = parse_address(NetworkType.ip6, " localhost" )
64+ testing.assert_equal(hp[0 ], " ::1" )
65+ testing.assert_equal(hp[1 ], 0 )
66+
67+ # TODO : IPv6 long form - Not supported yet.
1868 # hp = parse_address("0:0:0:0:0:0:0:1:8080")
1969 # testing.assert_equal(hp[0], "0:0:0:0:0:0:0:1")
2070 # testing.assert_equal(hp[1], 8080)
2171
72+ # Error cases
73+ # IP protocol with port
74+ try :
75+ _ = parse_address(NetworkType.ip4, " 192.168.1.1:80" )
76+ testing.assert_false(" Should have raised an error for IP protocol with port" )
77+ except Error:
78+ testing.assert_true(True )
79+
80+ # Missing port
81+ try :
82+ _ = parse_address(NetworkType.tcp4, " 192.168.1.1" )
83+ testing.assert_false(" Should have raised MissingPortError" )
84+ except MissingPortError:
85+ testing.assert_true(True )
86+
87+ # Missing port
88+ try :
89+ _ = parse_address(NetworkType.tcp6, " [::1]" )
90+ testing.assert_false(" Should have raised MissingPortError" )
91+ except MissingPortError:
92+ testing.assert_true(True )
93+
94+ # Port out of range
95+ try :
96+ _ = parse_address(NetworkType.tcp4, " 192.168.1.1:70000" )
97+ testing.assert_false(" Should have raised error for invalid port" )
98+ except Error:
99+ testing.assert_true(True )
100+
101+ # Missing closing bracket
102+ try :
103+ _ = parse_address(NetworkType.tcp6, " [::1:8080" )
104+ testing.assert_false(" Should have raised error for missing bracket" )
105+ except Error:
106+ testing.assert_true(True )
107+
22108
23109def test_join_host_port ():
24110 # IPv4
0 commit comments