@@ -839,6 +839,19 @@ def test_request_port(self):
839839        req  =  urllib .request .Request ("http://www.acme.com/" ,
840840                                     headers = {"Host" : "www.acme.com:4321" })
841841        self .assertEqual (request_port (req ), DEFAULT_HTTP_PORT )
842+         req  =  urllib .request .Request ("http://www.acme.com:" ,
843+                                      headers = {"Host" : "www.acme.com:4321" })
844+         self .assertEqual (request_port (req ), DEFAULT_HTTP_PORT )
845+         req  =  urllib .request .Request ("http://www.acme.com:not_a_port" ,
846+                                      headers = {"Host" : "www.acme.com:4321" })
847+         self .assertEqual (request_port (req ), DEFAULT_HTTP_PORT )
848+         req  =  urllib .request .Request ("http://[::1]:1234" ,
849+                                      headers = {"Host" : "[::1]:4321" })
850+         self .assertEqual (request_port (req ), "1234" )
851+         req  =  urllib .request .Request ("http://[::1]" ,
852+                                      headers = {"Host" : "[::1]:4321" })
853+         self .assertEqual (request_port (req ), DEFAULT_HTTP_PORT )
854+ 
842855
843856    def  test_request_host (self ):
844857        # this request is illegal (RFC2616, 14.2.3) 
@@ -1265,41 +1278,67 @@ def test_missing_final_slash(self):
12651278    def  test_set_ok_port (self ):
12661279        pol  =  DefaultCookiePolicy ()
12671280        c  =  CookieJar (policy = pol )
1268-         headers  =  ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; port=1234" ]
1281+         headers_with_port_1234  =  ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; port=1234" ]
1282+         headers_with_default_port  =  ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; port=80" ]
12691283        req  =  urllib .request .Request ("http://127.0.0.1:1234" )
1270-         res  =  FakeResponse (headers , "http://127.0.0.1:1234" )
1284+         res  =  FakeResponse (headers_with_port_1234 , "http://127.0.0.1:1234" )
12711285        self .assertTrue (pol .set_ok_port (c .make_cookies (res , req )[0 ], req ))
12721286
12731287        req  =  urllib .request .Request ("http://acme.com:1234" )
1274-         res  =  FakeResponse (headers , "http://acme.com:1234" )
1288+         res  =  FakeResponse (headers_with_port_1234 , "http://acme.com:1234" )
12751289        self .assertTrue (pol .set_ok_port (c .make_cookies (res , req )[0 ], req ))
12761290
12771291        req  =  urllib .request .Request ("http://[::1]:1234" )
1278-         res  =  FakeResponse (headers , "http://[::1]:1234" )
1292+         res  =  FakeResponse (headers_with_port_1234 , "http://[::1]:1234" )
12791293        self .assertTrue (pol .set_ok_port (c .make_cookies (res , req )[0 ], req ))
12801294
12811295        req  =  urllib .request .Request ("http://[::1]:1235" )
1282-         res  =  FakeResponse (headers , "http://[::1]:1235" )
1296+         res  =  FakeResponse (headers_with_port_1234 , "http://[::1]:1235" )
1297+         self .assertFalse (pol .set_ok_port (c .make_cookies (res , req )[0 ], req ))
1298+ 
1299+         req  =  urllib .request .Request ("http://acme.com:" )
1300+         res  =  FakeResponse (headers_with_default_port , "http://acme.com:" )
1301+         self .assertTrue (pol .set_ok_port (c .make_cookies (res , req )[0 ], req ))
1302+         res  =  FakeResponse (headers_with_port_1234 , "http://acme.com:" )
1303+         self .assertFalse (pol .set_ok_port (c .make_cookies (res , req )[0 ], req ))
1304+ 
1305+         req  =  urllib .request .Request ("http://acme.com:not_a_port" )
1306+         res  =  FakeResponse (headers_with_default_port , "http://acme.com:not_a_port" )
1307+         self .assertTrue (pol .set_ok_port (c .make_cookies (res , req )[0 ], req ))
1308+         res  =  FakeResponse (headers_with_port_1234 , "http://acme.com:not_a_port" )
12831309        self .assertFalse (pol .set_ok_port (c .make_cookies (res , req )[0 ], req ))
12841310
12851311    def  test_return_ok_port (self ):
12861312        pol  =  DefaultCookiePolicy ()
12871313        c  =  CookieJar (policy = pol )
1288-         headers  =  ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; port=1234" ]
1314+         headers_with_port_1234  =  ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; port=1234" ]
1315+         headers_with_default_port  =  ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; port=80" ]
12891316        req  =  urllib .request .Request ("http://127.0.0.1:1234" )
1290-         res  =  FakeResponse (headers , "http://127.0.0.1:1234" )
1317+         res  =  FakeResponse (headers_with_port_1234 , "http://127.0.0.1:1234" )
12911318        self .assertTrue (pol .return_ok_port (c .make_cookies (res , req )[0 ], req ))
12921319
12931320        req  =  urllib .request .Request ("http://acme.com:1234" )
1294-         res  =  FakeResponse (headers , "http://acme.com:1234" )
1321+         res  =  FakeResponse (headers_with_port_1234 , "http://acme.com:1234" )
12951322        self .assertTrue (pol .return_ok_port (c .make_cookies (res , req )[0 ], req ))
12961323
12971324        req  =  urllib .request .Request ("http://[::1]:1234" )
1298-         res  =  FakeResponse (headers , "http://[::1]:1234" )
1325+         res  =  FakeResponse (headers_with_port_1234 , "http://[::1]:1234" )
12991326        self .assertTrue (pol .return_ok_port (c .make_cookies (res , req )[0 ], req ))
13001327
13011328        req  =  urllib .request .Request ("http://[::1]:1235" )
1302-         res  =  FakeResponse (headers , "http://[::1]:1235" )
1329+         res  =  FakeResponse (headers_with_port_1234 , "http://[::1]:1235" )
1330+         self .assertFalse (pol .return_ok_port (c .make_cookies (res , req )[0 ], req ))
1331+ 
1332+         req  =  urllib .request .Request ("http://acme.com:" )
1333+         res  =  FakeResponse (headers_with_default_port , "http://acme.com:" )
1334+         self .assertTrue (pol .return_ok_port (c .make_cookies (res , req )[0 ], req ))
1335+         res  =  FakeResponse (headers_with_port_1234 , "http://acme.com:" )
1336+         self .assertFalse (pol .return_ok_port (c .make_cookies (res , req )[0 ], req ))
1337+ 
1338+         req  =  urllib .request .Request ("http://acme.com:not_a_port" )
1339+         res  =  FakeResponse (headers_with_default_port , "http://acme.com:not_a_port" )
1340+         self .assertTrue (pol .return_ok_port (c .make_cookies (res , req )[0 ], req ))
1341+         res  =  FakeResponse (headers_with_port_1234 , "http://acme.com:not_a_port" )
13031342        self .assertFalse (pol .return_ok_port (c .make_cookies (res , req )[0 ], req ))
13041343
13051344    def  test_domain_mirror (self ):
0 commit comments