@@ -681,28 +681,35 @@ def test_default_scheme(self):
681
681
def test_parse_fragments (self ):
682
682
# Exercise the allow_fragments parameter of urlparse() and urlsplit()
683
683
tests = (
684
- ("http:#frag" , "path" ),
685
- ("//example.net#frag" , "path" ),
686
- ("index.html#frag" , "path" ),
687
- (";a=b#frag" , "params" ),
688
- ("?a=b#frag" , "query" ),
689
- ("#frag" , "path" ),
684
+ ("http:#frag" , "path" , "frag" ),
685
+ ("//example.net#frag" , "path" , "frag" ),
686
+ ("index.html#frag" , "path" , "frag" ),
687
+ (";a=b#frag" , "params" , "frag" ),
688
+ ("?a=b#frag" , "query" , "frag" ),
689
+ ("#frag" , "path" , "frag" ),
690
+ ("abc#@frag" , "path" , "@frag" ),
691
+ ("//abc#@frag" , "path" , "@frag" ),
692
+ ("//abc:80#@frag" , "path" , "@frag" ),
693
+ ("//abc#@frag:80" , "path" , "@frag:80" ),
690
694
)
691
- for url , attr in tests :
695
+ for url , attr , expected_frag in tests :
692
696
for func in (urllib .parse .urlparse , urllib .parse .urlsplit ):
693
697
if attr == "params" and func is urllib .parse .urlsplit :
694
698
attr = "path"
695
699
with self .subTest (url = url , function = func ):
696
700
result = func (url , allow_fragments = False )
697
701
self .assertEqual (result .fragment , "" )
698
- self .assertTrue (getattr (result , attr ).endswith ("#frag" ))
702
+ self .assertTrue (
703
+ getattr (result , attr ).endswith ("#" + expected_frag ))
699
704
self .assertEqual (func (url , "" , False ).fragment , "" )
700
705
701
706
result = func (url , allow_fragments = True )
702
- self .assertEqual (result .fragment , "frag" )
703
- self .assertFalse (getattr (result , attr ).endswith ("frag" ))
704
- self .assertEqual (func (url , "" , True ).fragment , "frag" )
705
- self .assertEqual (func (url ).fragment , "frag" )
707
+ self .assertEqual (result .fragment , expected_frag )
708
+ self .assertFalse (
709
+ getattr (result , attr ).endswith (expected_frag ))
710
+ self .assertEqual (func (url , "" , True ).fragment ,
711
+ expected_frag )
712
+ self .assertEqual (func (url ).fragment , expected_frag )
706
713
707
714
def test_mixed_types_rejected (self ):
708
715
# Several functions that process either strings or ASCII encoded bytes
@@ -883,6 +890,26 @@ def test_splithost(self):
883
890
self .assertEqual (splithost ('/foo/bar/baz.html' ),
884
891
(None , '/foo/bar/baz.html' ))
885
892
893
+ # bpo-30500: # starts a fragment.
894
+ self .assertEqual (splithost ('//127.0.0.1#@host.com' ),
895
+ ('127.0.0.1' , '/#@host.com' ))
896
+ self .assertEqual (splithost ('//127.0.0.1#@host.com:80' ),
897
+ ('127.0.0.1' , '/#@host.com:80' ))
898
+ self .assertEqual (splithost ('//127.0.0.1:80#@host.com' ),
899
+ ('127.0.0.1:80' , '/#@host.com' ))
900
+
901
+ # Empty host is returned as empty string.
902
+ self .assertEqual (splithost ("///file" ),
903
+ ('' , '/file' ))
904
+
905
+ # Trailing semicolon, question mark and hash symbol are kept.
906
+ self .assertEqual (splithost ("//example.net/file;" ),
907
+ ('example.net' , '/file;' ))
908
+ self .assertEqual (splithost ("//example.net/file?" ),
909
+ ('example.net' , '/file?' ))
910
+ self .assertEqual (splithost ("//example.net/file#" ),
911
+ ('example.net' , '/file#' ))
912
+
886
913
def test_splituser (self ):
887
914
splituser = urllib .parse .splituser
888
915
self .assertEqual (splituser ('User:Pass@www.python.org:080' ),
0 commit comments