1+ import contextlib
12import hmac
23import unittest
34import urllib .error
@@ -131,12 +132,13 @@ def test_basic_auth_missing_credentials(self):
131132 def test_basic_auth_missing_credentials_details (self ):
132133 with self .assertRaises (urllib .error .HTTPError ) as raised :
133134 self .loop .run_until_complete (self .make_http_request ())
134- self .assertEqual (raised .exception .code , 401 )
135- self .assertEqual (
136- raised .exception .headers ["WWW-Authenticate" ],
137- 'Basic realm="auth-tests", charset="UTF-8"' ,
138- )
139- self .assertEqual (raised .exception .read ().decode (), "Missing credentials\n " )
135+ with contextlib .closing (raised .exception ):
136+ self .assertEqual (raised .exception .code , 401 )
137+ self .assertEqual (
138+ raised .exception .headers ["WWW-Authenticate" ],
139+ 'Basic realm="auth-tests", charset="UTF-8"' ,
140+ )
141+ self .assertEqual (raised .exception .read ().decode (), "Missing credentials\n " )
140142
141143 @with_server (create_protocol = create_protocol )
142144 def test_basic_auth_unsupported_credentials (self ):
@@ -150,12 +152,15 @@ def test_basic_auth_unsupported_credentials_details(self):
150152 self .loop .run_until_complete (
151153 self .make_http_request (headers = {"Authorization" : "Digest ..." })
152154 )
153- self .assertEqual (raised .exception .code , 401 )
154- self .assertEqual (
155- raised .exception .headers ["WWW-Authenticate" ],
156- 'Basic realm="auth-tests", charset="UTF-8"' ,
157- )
158- self .assertEqual (raised .exception .read ().decode (), "Unsupported credentials\n " )
155+ with contextlib .closing (raised .exception ):
156+ self .assertEqual (raised .exception .code , 401 )
157+ self .assertEqual (
158+ raised .exception .headers ["WWW-Authenticate" ],
159+ 'Basic realm="auth-tests", charset="UTF-8"' ,
160+ )
161+ self .assertEqual (
162+ raised .exception .read ().decode (), "Unsupported credentials\n "
163+ )
159164
160165 @with_server (create_protocol = create_protocol )
161166 def test_basic_auth_invalid_username (self ):
@@ -176,9 +181,10 @@ def test_basic_auth_invalid_credentials_details(self):
176181 self .loop .run_until_complete (
177182 self .make_http_request (headers = {"Authorization" : authorization })
178183 )
179- self .assertEqual (raised .exception .code , 401 )
180- self .assertEqual (
181- raised .exception .headers ["WWW-Authenticate" ],
182- 'Basic realm="auth-tests", charset="UTF-8"' ,
183- )
184- self .assertEqual (raised .exception .read ().decode (), "Invalid credentials\n " )
184+ with contextlib .closing (raised .exception ):
185+ self .assertEqual (raised .exception .code , 401 )
186+ self .assertEqual (
187+ raised .exception .headers ["WWW-Authenticate" ],
188+ 'Basic realm="auth-tests", charset="UTF-8"' ,
189+ )
190+ self .assertEqual (raised .exception .read ().decode (), "Invalid credentials\n " )
0 commit comments