/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Robert Collins
  • Date: 2009-05-23 20:57:12 UTC
  • mfrom: (4371 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4441.
  • Revision ID: robertc@robertcollins.net-20090523205712-lcwbfqk6vwavinuv
MergeĀ .dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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
16
16
 
17
17
"""Tests for HTTP implementations.
18
18
 
113
113
                                            protocol_scenarios)
114
114
    tests.multiply_tests(tp_tests, tp_scenarios, result)
115
115
 
 
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((
 
119
                TestProxyAuth,
 
120
                )))
 
121
    proxy_auth_scheme_scenarios = [
 
122
        ('basic', dict(_auth_server=http_utils.ProxyBasicAuthServer)),
 
123
        ('digest', dict(_auth_server=http_utils.ProxyDigestAuthServer)),
 
124
        ('basicdigest',
 
125
         dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
 
126
        ]
 
127
    tppa_scenarios = tests.multiply_scenarios(tp_scenarios,
 
128
                                              proxy_auth_scheme_scenarios)
 
129
    tests.multiply_tests(tppa_tests, tppa_scenarios, result)
 
130
 
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((
119
134
                TestAuth,
120
135
                )))
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)),
 
139
        ('basicdigest',
 
140
         dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
124
141
        ]
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)
128
145
 
129
146
    # activity: activity on all http versions on all implementations
215
232
 
216
233
class TestAuthHeader(tests.TestCase):
217
234
 
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)
221
240
 
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)
237
256
 
 
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)
 
264
 
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
1312
1339
 
1313
1340
 
1440
1467
 
1441
1468
    _auth_header = 'Authorization'
1442
1469
    _password_prompt_prefix = ''
 
1470
    _username_prompt_prefix = ''
 
1471
    # Set by load_tests
 
1472
    _auth_server = None
1443
1473
 
1444
1474
    def setUp(self):
1445
1475
        super(TestAuth, self).setUp()
1448
1478
                                  ('b', 'contents of b\n'),])
1449
1479
 
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)
1454
 
        else:
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)
1460
 
        return server
 
1481
        return self._auth_server(protocol_version=self._protocol_version)
1461
1482
 
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)
1516
1537
 
 
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')
 
1543
 
 
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())
 
1551
        stdout.seek(0)
 
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',
 
1555
                                    stdout.readline())
 
1556
 
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)
1548
1588
 
 
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))
 
1594
 
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)
1593
1639
 
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."""
1618
1665
 
1619
1666
    _auth_header = 'Proxy-authorization'
1620
 
    _password_prompt_prefix='Proxy '
 
1667
    _password_prompt_prefix = 'Proxy '
 
1668
    _username_prompt_prefix = 'Proxy '
1621
1669
 
1622
1670
    def setUp(self):
1623
1671
        super(TestProxyAuth, self).setUp()
1630
1678
                                  ('b-proxied', 'contents of b\n'),
1631
1679
                                  ])
1632
1680
 
1633
 
    def create_transport_readonly_server(self):
1634
 
        if self._auth_scheme == 'basic':
1635
 
            server = http_utils.ProxyBasicAuthServer(
1636
 
                protocol_version=self._protocol_version)
1637
 
        else:
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)
1643
 
        return server
1644
 
 
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())
1905
1941
 
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
1911
1947