@@ -331,7 +331,7 @@ def test_willclose(self):
331331 self .unfakehttp ()
332332
333333 @unittest .skipUnless (ssl , "ssl module required" )
334- def test_url_with_control_char_rejected (self ):
334+ def test_url_path_with_control_char_rejected (self ):
335335 for char_no in list (range (0 , 0x21 )) + [0x7f ]:
336336 char = chr (char_no )
337337 schemeless_url = f"//localhost:7777/test{ char } /"
@@ -358,7 +358,7 @@ def test_url_with_control_char_rejected(self):
358358 self .unfakehttp ()
359359
360360 @unittest .skipUnless (ssl , "ssl module required" )
361- def test_url_with_newline_header_injection_rejected (self ):
361+ def test_url_path_with_newline_header_injection_rejected (self ):
362362 self .fakehttp (b"HTTP/1.1 200 OK\r \n \r \n Hello." )
363363 host = "localhost:7777?a=1 HTTP/1.1\r \n X-injected: header\r \n TEST: 123"
364364 schemeless_url = "//" + host + ":8080/test/?test=a"
@@ -383,6 +383,38 @@ def test_url_with_newline_header_injection_rejected(self):
383383 finally :
384384 self .unfakehttp ()
385385
386+ @unittest .skipUnless (ssl , "ssl module required" )
387+ def test_url_host_with_control_char_rejected (self ):
388+ for char_no in list (range (0 , 0x21 )) + [0x7f ]:
389+ char = chr (char_no )
390+ schemeless_url = f"//localhost{ char } /test/"
391+ self .fakehttp (b"HTTP/1.1 200 OK\r \n \r \n Hello." )
392+ try :
393+ escaped_char_repr = repr (char ).replace ('\\ ' , r'\\' )
394+ InvalidURL = http .client .InvalidURL
395+ with self .assertRaisesRegex (
396+ InvalidURL , f"contain control.*{ escaped_char_repr } " ):
397+ urlopen (f"http:{ schemeless_url } " )
398+ with self .assertRaisesRegex (InvalidURL , f"contain control.*{ escaped_char_repr } " ):
399+ urlopen (f"https:{ schemeless_url } " )
400+ finally :
401+ self .unfakehttp ()
402+
403+ @unittest .skipUnless (ssl , "ssl module required" )
404+ def test_url_host_with_newline_header_injection_rejected (self ):
405+ self .fakehttp (b"HTTP/1.1 200 OK\r \n \r \n Hello." )
406+ host = "localhost\r \n X-injected: header\r \n "
407+ schemeless_url = "//" + host + ":8080/test/?test=a"
408+ try :
409+ InvalidURL = http .client .InvalidURL
410+ with self .assertRaisesRegex (
411+ InvalidURL , r"contain control.*\\r" ):
412+ urlopen (f"http:{ schemeless_url } " )
413+ with self .assertRaisesRegex (InvalidURL , r"contain control.*\\n" ):
414+ urlopen (f"https:{ schemeless_url } " )
415+ finally :
416+ self .unfakehttp ()
417+
386418 def test_read_0_9 (self ):
387419 # "0.9" response accepted (but not "simple responses" without
388420 # a status line)
0 commit comments