13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for HTTP implementations.
113
113
protocol_scenarios)
114
114
tests.multiply_tests(tp_tests, tp_scenarios, result)
116
# proxy auth: each auth scheme on all http versions on all implementations.
117
tppa_tests, remaining_tests = tests.split_suite_by_condition(
118
remaining_tests, tests.condition_isinstance((
121
proxy_auth_scheme_scenarios = [
122
('basic', dict(_auth_server=http_utils.ProxyBasicAuthServer)),
123
('digest', dict(_auth_server=http_utils.ProxyDigestAuthServer)),
125
dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
127
tppa_scenarios = tests.multiply_scenarios(tp_scenarios,
128
proxy_auth_scheme_scenarios)
129
tests.multiply_tests(tppa_tests, tppa_scenarios, result)
116
131
# auth: each auth scheme on all http versions on all implementations.
117
132
tpa_tests, remaining_tests = tests.split_suite_by_condition(
118
133
remaining_tests, tests.condition_isinstance((
121
136
auth_scheme_scenarios = [
122
('basic', dict(_auth_scheme='basic')),
123
('digest', dict(_auth_scheme='digest')),
137
('basic', dict(_auth_server=http_utils.HTTPBasicAuthServer)),
138
('digest', dict(_auth_server=http_utils.HTTPDigestAuthServer)),
140
dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
125
142
tpa_scenarios = tests.multiply_scenarios(tp_scenarios,
126
auth_scheme_scenarios)
143
auth_scheme_scenarios)
127
144
tests.multiply_tests(tpa_tests, tpa_scenarios, result)
129
146
# activity: activity on all http versions on all implementations
216
233
class TestAuthHeader(tests.TestCase):
218
def parse_header(self, header):
219
ah = _urllib2_wrappers.AbstractAuthHandler()
220
return ah._parse_auth_header(header)
235
def parse_header(self, header, auth_handler_class=None):
236
if auth_handler_class is None:
237
auth_handler_class = _urllib2_wrappers.AbstractAuthHandler
238
self.auth_handler = auth_handler_class()
239
return self.auth_handler._parse_auth_header(header)
222
241
def test_empty_header(self):
223
242
scheme, remainder = self.parse_header('')
235
254
self.assertEquals('basic', scheme)
236
255
self.assertEquals('realm="Thou should not pass"', remainder)
257
def test_basic_extract_realm(self):
258
scheme, remainder = self.parse_header(
259
'Basic realm="Thou should not pass"',
260
_urllib2_wrappers.BasicAuthHandler)
261
match, realm = self.auth_handler.extract_realm(remainder)
262
self.assertTrue(match is not None)
263
self.assertEquals('Thou should not pass', realm)
238
265
def test_digest_header(self):
239
266
scheme, remainder = self.parse_header(
240
267
'Digest realm="Thou should not pass"')
1307
1334
# Since the tests using this class will replace
1308
1335
# _urllib2_wrappers.Request, we can't just call the base class __init__
1309
1336
# or we'll loop.
1310
RedirectedRequest.init_orig(self, method, url, args, kwargs)
1337
RedirectedRequest.init_orig(self, method, url, *args, **kwargs)
1311
1338
self.follow_redirections = True
1441
1468
_auth_header = 'Authorization'
1442
1469
_password_prompt_prefix = ''
1470
_username_prompt_prefix = ''
1444
1474
def setUp(self):
1445
1475
super(TestAuth, self).setUp()
1448
1478
('b', 'contents of b\n'),])
1450
1480
def create_transport_readonly_server(self):
1451
if self._auth_scheme == 'basic':
1452
server = http_utils.HTTPBasicAuthServer(
1453
protocol_version=self._protocol_version)
1455
if self._auth_scheme != 'digest':
1456
raise AssertionError('Unknown auth scheme: %r'
1457
% self._auth_scheme)
1458
server = http_utils.HTTPDigestAuthServer(
1459
protocol_version=self._protocol_version)
1481
return self._auth_server(protocol_version=self._protocol_version)
1462
1483
def _testing_pycurl(self):
1463
1484
return pycurl_present and self._transport == PyCurlTransport
1514
1535
# initial 'who are you' and 'this is not you, who are you')
1515
1536
self.assertEqual(2, self.server.auth_required_errors)
1538
def test_prompt_for_username(self):
1539
if self._testing_pycurl():
1540
raise tests.TestNotApplicable(
1541
'pycurl cannot prompt, it handles auth by embedding'
1542
' user:pass in urls only')
1544
self.server.add_user('joe', 'foo')
1545
t = self.get_user_transport(None, None)
1546
stdout = tests.StringIOWrapper()
1547
ui.ui_factory = tests.TestUIFactory(stdin='joe\nfoo\n', stdout=stdout)
1548
self.assertEqual('contents of a\n',t.get('a').read())
1549
# stdin should be empty
1550
self.assertEqual('', ui.ui_factory.stdin.readline())
1552
expected_prompt = self._expected_username_prompt(t._unqualified_scheme)
1553
self.assertEquals(expected_prompt, stdout.read(len(expected_prompt)))
1554
self._check_password_prompt(t._unqualified_scheme, 'joe',
1517
1557
def test_prompt_for_password(self):
1518
1558
if self._testing_pycurl():
1519
1559
raise tests.TestNotApplicable(
1546
1586
self.server.auth_realm)))
1547
1587
self.assertEquals(expected_prompt, actual_prompt)
1589
def _expected_username_prompt(self, scheme):
1590
return (self._username_prompt_prefix
1591
+ "%s %s:%d, Realm: '%s' username: " % (scheme.upper(),
1592
self.server.host, self.server.port,
1593
self.server.auth_realm))
1549
1595
def test_no_prompt_for_password_when_using_auth_config(self):
1550
1596
if self._testing_pycurl():
1551
1597
raise tests.TestNotApplicable(
1592
1638
self.assertEqual(1, self.server.auth_required_errors)
1594
1640
def test_changing_nonce(self):
1595
if self._auth_scheme != 'digest':
1596
raise tests.TestNotApplicable('HTTP auth digest only test')
1641
if self._auth_server not in (http_utils.HTTPDigestAuthServer,
1642
http_utils.ProxyDigestAuthServer):
1643
raise tests.TestNotApplicable('HTTP/proxy auth digest only test')
1597
1644
if self._testing_pycurl():
1598
1645
raise tests.KnownFailure(
1599
1646
'pycurl does not handle a nonce change')
1617
1664
"""Test proxy authentication schemes."""
1619
1666
_auth_header = 'Proxy-authorization'
1620
_password_prompt_prefix='Proxy '
1667
_password_prompt_prefix = 'Proxy '
1668
_username_prompt_prefix = 'Proxy '
1622
1670
def setUp(self):
1623
1671
super(TestProxyAuth, self).setUp()
1630
1678
('b-proxied', 'contents of b\n'),
1633
def create_transport_readonly_server(self):
1634
if self._auth_scheme == 'basic':
1635
server = http_utils.ProxyBasicAuthServer(
1636
protocol_version=self._protocol_version)
1638
if self._auth_scheme != 'digest':
1639
raise AssertionError('Unknown auth scheme: %r'
1640
% self._auth_scheme)
1641
server = http_utils.ProxyDigestAuthServer(
1642
protocol_version=self._protocol_version)
1645
1681
def get_user_transport(self, user, password):
1646
1682
self._install_env({'all_proxy': self.get_user_url(user, password)})
1647
1683
return self._transport(self.server.get_url())
1906
1942
# We override at class level because constructors may propagate the
1907
1943
# bound method and render instance overriding ineffective (an
1908
# alternative would be be to define a specific ui factory instead...)
1944
# alternative would be to define a specific ui factory instead...)
1909
1945
self.orig_report_activity = self._transport._report_activity
1910
1946
self._transport._report_activity = report_activity