@@ -1113,6 +1113,7 @@ def test_local_bad_hostname(self):
11131113 server = self .make_server (CERT_fakehostname )
11141114 context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
11151115 context .verify_mode = ssl .CERT_REQUIRED
1116+ context .check_hostname = True
11161117 context .load_verify_locations (CERT_fakehostname )
11171118 h = client .HTTPSConnection ('localhost' , server .port , context = context )
11181119 with self .assertRaises (ssl .CertificateError ):
@@ -1123,11 +1124,24 @@ def test_local_bad_hostname(self):
11231124 with self .assertRaises (ssl .CertificateError ):
11241125 h .request ('GET' , '/' )
11251126 # With check_hostname=False, the mismatching is ignored
1127+ context .check_hostname = False
11261128 h = client .HTTPSConnection ('localhost' , server .port , context = context ,
11271129 check_hostname = False )
11281130 h .request ('GET' , '/nonexistent' )
11291131 resp = h .getresponse ()
11301132 self .assertEqual (resp .status , 404 )
1133+ # The context's check_hostname setting is used if one isn't passed to
1134+ # HTTPSConnection.
1135+ context .check_hostname = False
1136+ h = client .HTTPSConnection ('localhost' , server .port , context = context )
1137+ h .request ('GET' , '/nonexistent' )
1138+ self .assertEqual (h .getresponse ().status , 404 )
1139+ # Passing check_hostname to HTTPSConnection should override the
1140+ # context's setting.
1141+ h = client .HTTPSConnection ('localhost' , server .port , context = context ,
1142+ check_hostname = True )
1143+ with self .assertRaises (ssl .CertificateError ):
1144+ h .request ('GET' , '/' )
11311145
11321146 @unittest .skipIf (not hasattr (client , 'HTTPSConnection' ),
11331147 'http.client.HTTPSConnection not available' )
0 commit comments