1
+ import contextlib
1
2
import hmac
2
3
import unittest
3
4
import urllib .error
@@ -131,12 +132,13 @@ def test_basic_auth_missing_credentials(self):
131
132
def test_basic_auth_missing_credentials_details (self ):
132
133
with self .assertRaises (urllib .error .HTTPError ) as raised :
133
134
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 " )
140
142
141
143
@with_server (create_protocol = create_protocol )
142
144
def test_basic_auth_unsupported_credentials (self ):
@@ -150,12 +152,15 @@ def test_basic_auth_unsupported_credentials_details(self):
150
152
self .loop .run_until_complete (
151
153
self .make_http_request (headers = {"Authorization" : "Digest ..." })
152
154
)
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
+ )
159
164
160
165
@with_server (create_protocol = create_protocol )
161
166
def test_basic_auth_invalid_username (self ):
@@ -176,9 +181,10 @@ def test_basic_auth_invalid_credentials_details(self):
176
181
self .loop .run_until_complete (
177
182
self .make_http_request (headers = {"Authorization" : authorization })
178
183
)
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