@@ -755,28 +755,35 @@ def test_default_scheme(self):
755
755
def test_parse_fragments (self ):
756
756
# Exercise the allow_fragments parameter of urlparse() and urlsplit()
757
757
tests = (
758
- ("http:#frag" , "path" ),
759
- ("//example.net#frag" , "path" ),
760
- ("index.html#frag" , "path" ),
761
- (";a=b#frag" , "params" ),
762
- ("?a=b#frag" , "query" ),
763
- ("#frag" , "path" ),
758
+ ("http:#frag" , "path" , "frag" ),
759
+ ("//example.net#frag" , "path" , "frag" ),
760
+ ("index.html#frag" , "path" , "frag" ),
761
+ (";a=b#frag" , "params" , "frag" ),
762
+ ("?a=b#frag" , "query" , "frag" ),
763
+ ("#frag" , "path" , "frag" ),
764
+ ("abc#@frag" , "path" , "@frag" ),
765
+ ("//abc#@frag" , "path" , "@frag" ),
766
+ ("//abc:80#@frag" , "path" , "@frag" ),
767
+ ("//abc#@frag:80" , "path" , "@frag:80" ),
764
768
)
765
- for url , attr in tests :
769
+ for url , attr , expected_frag in tests :
766
770
for func in (urllib .parse .urlparse , urllib .parse .urlsplit ):
767
771
if attr == "params" and func is urllib .parse .urlsplit :
768
772
attr = "path"
769
773
with self .subTest (url = url , function = func ):
770
774
result = func (url , allow_fragments = False )
771
775
self .assertEqual (result .fragment , "" )
772
- self .assertTrue (getattr (result , attr ).endswith ("#frag" ))
776
+ self .assertTrue (
777
+ getattr (result , attr ).endswith ("#" + expected_frag ))
773
778
self .assertEqual (func (url , "" , False ).fragment , "" )
774
779
775
780
result = func (url , allow_fragments = True )
776
- self .assertEqual (result .fragment , "frag" )
777
- self .assertFalse (getattr (result , attr ).endswith ("frag" ))
778
- self .assertEqual (func (url , "" , True ).fragment , "frag" )
779
- self .assertEqual (func (url ).fragment , "frag" )
781
+ self .assertEqual (result .fragment , expected_frag )
782
+ self .assertFalse (
783
+ getattr (result , attr ).endswith (expected_frag ))
784
+ self .assertEqual (func (url , "" , True ).fragment ,
785
+ expected_frag )
786
+ self .assertEqual (func (url ).fragment , expected_frag )
780
787
781
788
def test_mixed_types_rejected (self ):
782
789
# Several functions that process either strings or ASCII encoded bytes
@@ -983,6 +990,26 @@ def test_splithost(self):
983
990
self .assertEqual (splithost ('/foo/bar/baz.html' ),
984
991
(None , '/foo/bar/baz.html' ))
985
992
993
+ # bpo-30500: # starts a fragment.
994
+ self .assertEqual (splithost ('//127.0.0.1#@host.com' ),
995
+ ('127.0.0.1' , '/#@host.com' ))
996
+ self .assertEqual (splithost ('//127.0.0.1#@host.com:80' ),
997
+ ('127.0.0.1' , '/#@host.com:80' ))
998
+ self .assertEqual (splithost ('//127.0.0.1:80#@host.com' ),
999
+ ('127.0.0.1:80' , '/#@host.com' ))
1000
+
1001
+ # Empty host is returned as empty string.
1002
+ self .assertEqual (splithost ("///file" ),
1003
+ ('' , '/file' ))
1004
+
1005
+ # Trailing semicolon, question mark and hash symbol are kept.
1006
+ self .assertEqual (splithost ("//example.net/file;" ),
1007
+ ('example.net' , '/file;' ))
1008
+ self .assertEqual (splithost ("//example.net/file?" ),
1009
+ ('example.net' , '/file?' ))
1010
+ self .assertEqual (splithost ("//example.net/file#" ),
1011
+ ('example.net' , '/file#' ))
1012
+
986
1013
def test_splituser (self ):
987
1014
splituser = urllib .parse .splituser
988
1015
self .assertEqual (splituser ('User:Pass@www.python.org:080' ),
0 commit comments