@@ -778,13 +778,36 @@ def _check_svn_python_org(self, resp):
778778 self .assertIn ('Apache' , server_string )
779779
780780 def test_networked (self ):
781- # Default settings: no cert verification is done
781+ # Default settings: requires a valid cert from a trusted CA
782+ import ssl
782783 support .requires ('network' )
783- with support .transient_internet ('svn.python.org' ):
784- h = client .HTTPSConnection ('svn.python.org' , 443 )
784+ with support .transient_internet ('self-signed.pythontest.net' ):
785+ h = client .HTTPSConnection ('self-signed.pythontest.net' , 443 )
786+ with self .assertRaises (ssl .SSLError ) as exc_info :
787+ h .request ('GET' , '/' )
788+ self .assertEqual (exc_info .exception .reason , 'CERTIFICATE_VERIFY_FAILED' )
789+
790+ def test_networked_noverification (self ):
791+ # Switch off cert verification
792+ import ssl
793+ support .requires ('network' )
794+ with support .transient_internet ('self-signed.pythontest.net' ):
795+ context = ssl ._create_unverified_context ()
796+ h = client .HTTPSConnection ('self-signed.pythontest.net' , 443 ,
797+ context = context )
785798 h .request ('GET' , '/' )
786799 resp = h .getresponse ()
787- self ._check_svn_python_org (resp )
800+ self .assertIn ('nginx' , resp .getheader ('server' ))
801+
802+ def test_networked_trusted_by_default_cert (self ):
803+ # Default settings: requires a valid cert from a trusted CA
804+ support .requires ('network' )
805+ with support .transient_internet ('www.python.org' ):
806+ h = client .HTTPSConnection ('www.python.org' , 443 )
807+ h .request ('GET' , '/' )
808+ resp = h .getresponse ()
809+ content_type = resp .getheader ('content-type' )
810+ self .assertIn ('text/html' , content_type )
788811
789812 def test_networked_good_cert (self ):
790813 # We feed a CA cert that validates the server's cert
@@ -803,13 +826,23 @@ def test_networked_bad_cert(self):
803826 # We feed a "CA" cert that is unrelated to the server's cert
804827 import ssl
805828 support .requires ('network' )
806- with support .transient_internet ('svn.python.org ' ):
829+ with support .transient_internet ('self-signed.pythontest.net ' ):
807830 context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
808831 context .verify_mode = ssl .CERT_REQUIRED
809832 context .load_verify_locations (CERT_localhost )
810- h = client .HTTPSConnection ('svn.python.org ' , 443 , context = context )
811- with self .assertRaises (ssl .SSLError ):
833+ h = client .HTTPSConnection ('self-signed.pythontest.net ' , 443 , context = context )
834+ with self .assertRaises (ssl .SSLError ) as exc_info :
812835 h .request ('GET' , '/' )
836+ self .assertEqual (exc_info .exception .reason , 'CERTIFICATE_VERIFY_FAILED' )
837+
838+ def test_local_unknown_cert (self ):
839+ # The custom cert isn't known to the default trust bundle
840+ import ssl
841+ server = self .make_server (CERT_localhost )
842+ h = client .HTTPSConnection ('localhost' , server .port )
843+ with self .assertRaises (ssl .SSLError ) as exc_info :
844+ h .request ('GET' , '/' )
845+ self .assertEqual (exc_info .exception .reason , 'CERTIFICATE_VERIFY_FAILED' )
813846
814847 def test_local_good_hostname (self ):
815848 # The (valid) cert validates the HTTP hostname
@@ -822,7 +855,6 @@ def test_local_good_hostname(self):
822855 h .request ('GET' , '/nonexistent' )
823856 resp = h .getresponse ()
824857 self .assertEqual (resp .status , 404 )
825- del server
826858
827859 def test_local_bad_hostname (self ):
828860 # The (valid) cert doesn't validate the HTTP hostname
@@ -845,7 +877,6 @@ def test_local_bad_hostname(self):
845877 h .request ('GET' , '/nonexistent' )
846878 resp = h .getresponse ()
847879 self .assertEqual (resp .status , 404 )
848- del server
849880
850881 @unittest .skipIf (not hasattr (client , 'HTTPSConnection' ),
851882 'http.client.HTTPSConnection not available' )
0 commit comments