@@ -1445,40 +1445,64 @@ def test_osx_proxy_bypass(self):
1445
1445
bypass = {'exclude_simple' : True , 'exceptions' : []}
1446
1446
self .assertTrue (_proxy_bypass_macosx_sysconf ('test' , bypass ))
1447
1447
1448
- def test_basic_auth (self , quote_char = '"' ):
1449
- opener = OpenerDirector ()
1450
- password_manager = MockPasswordManager ()
1451
- auth_handler = urllib .request .HTTPBasicAuthHandler (password_manager )
1452
- realm = "ACME Widget Store"
1453
- http_handler = MockHTTPHandler (
1454
- 401 , 'WWW-Authenticate: Basic realm=%s%s%s\r \n \r \n ' %
1455
- (quote_char , realm , quote_char ))
1456
- opener .add_handler (auth_handler )
1457
- opener .add_handler (http_handler )
1458
- self ._test_basic_auth (opener , auth_handler , "Authorization" ,
1459
- realm , http_handler , password_manager ,
1460
- "http://acme.example.com/protected" ,
1461
- "http://acme.example.com/protected" ,
1462
- )
1463
-
1464
- def test_basic_auth_with_single_quoted_realm (self ):
1465
- self .test_basic_auth (quote_char = "'" )
1466
-
1467
- def test_basic_auth_with_unquoted_realm (self ):
1468
- opener = OpenerDirector ()
1469
- password_manager = MockPasswordManager ()
1470
- auth_handler = urllib .request .HTTPBasicAuthHandler (password_manager )
1471
- realm = "ACME Widget Store"
1472
- http_handler = MockHTTPHandler (
1473
- 401 , 'WWW-Authenticate: Basic realm=%s\r \n \r \n ' % realm )
1474
- opener .add_handler (auth_handler )
1475
- opener .add_handler (http_handler )
1476
- with self .assertWarns (UserWarning ):
1448
+ def check_basic_auth (self , headers , realm ):
1449
+ with self .subTest (realm = realm , headers = headers ):
1450
+ opener = OpenerDirector ()
1451
+ password_manager = MockPasswordManager ()
1452
+ auth_handler = urllib .request .HTTPBasicAuthHandler (password_manager )
1453
+ body = '\r \n ' .join (headers ) + '\r \n \r \n '
1454
+ http_handler = MockHTTPHandler (401 , body )
1455
+ opener .add_handler (auth_handler )
1456
+ opener .add_handler (http_handler )
1477
1457
self ._test_basic_auth (opener , auth_handler , "Authorization" ,
1478
- realm , http_handler , password_manager ,
1479
- "http://acme.example.com/protected" ,
1480
- "http://acme.example.com/protected" ,
1481
- )
1458
+ realm , http_handler , password_manager ,
1459
+ "http://acme.example.com/protected" ,
1460
+ "http://acme.example.com/protected" )
1461
+
1462
+ def test_basic_auth (self ):
1463
+
1464
+
1465
+ basic = f'Basic realm="{ realm } "'
1466
+ basic2 = f'Basic realm="{ realm2 } "'
1467
+ other_no_realm = 'Otherscheme xxx'
1468
+ digest = (f'Digest realm="{ realm2 } ", '
1469
+ f'qop="auth, auth-int", '
1470
+ f'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", '
1471
+ f'opaque="5ccc069c403ebaf9f0171e9517f40e41"' )
1472
+ for realm_str in (
1473
+ # test "quote" and 'quote'
1474
+ f'Basic realm="{ realm } "' ,
1475
+ f"Basic realm='{ realm } '" ,
1476
+
1477
+ # charset is ignored
1478
+ f'Basic realm="{ realm } ", charset="UTF-8"' ,
1479
+
1480
+ # Multiple challenges per header
1481
+ f'{ basic } , { basic2 } ' ,
1482
+ f'{ basic } , { other_no_realm } ' ,
1483
+ f'{ other_no_realm } , { basic } ' ,
1484
+ f'{ basic } , { digest } ' ,
1485
+ f'{ digest } , { basic } ' ,
1486
+ ):
1487
+ headers = [f'WWW-Authenticate: { realm_str } ' ]
1488
+ self .check_basic_auth (headers , realm )
1489
+
1490
+ # no quote: expect a warning
1491
+ with support .check_warnings (("Basic Auth Realm was unquoted" ,
1492
+ UserWarning )):
1493
+ headers = [f'WWW-Authenticate: Basic realm={ realm } ' ]
1494
+ self .check_basic_auth (headers , realm )
1495
+
1496
+ # Multiple headers: one challenge per header.
1497
+ # Use the first Basic realm.
1498
+ for challenges in (
1499
+ [basic , basic2 ],
1500
+ [basic , digest ],
1501
+ [digest , basic ],
1502
+ ):
1503
+ headers = [f'WWW-Authenticate: { challenge } '
1504
+ for challenge in challenges ]
1505
+ self .check_basic_auth (headers , realm )
1482
1506
1483
1507
def test_proxy_basic_auth (self ):
1484
1508
opener = OpenerDirector ()
0 commit comments