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