1146
1146
self.assertRaises(errors.TooManyRedirections, do_catching_redirections,
1147
1147
self.get_a, self.old_transport, redirected)
1150
class TestHTTPBasicAuth(TestCaseWithWebserver):
1151
"""Test basic authentication scheme"""
1153
_transport = HttpTransport_urllib
1154
_auth_header = 'Authorization'
1156
def create_transport_readonly_server(self):
1157
return BasicAuthHTTPServer()
1160
super(TestHTTPBasicAuth, self).setUp()
1161
self.build_tree_contents([('a', 'contents of a\n'),
1162
('b', 'contents of b\n'),])
1163
self.server = self.get_readonly_server()
1165
self.old_factory = ui.ui_factory
1166
self.addCleanup(self.restoreUIFactory)
1168
def restoreUIFactory(self):
1169
ui.ui_factory = self.old_factory
1171
def get_user_url(self, user=None, password=None):
1172
"""Build an url embedding user and password"""
1173
url = '%s://' % self.server._url_protocol
1174
if user is not None:
1176
if password is not None:
1177
url += ':' + password
1179
url += '%s:%s/' % (self.server.host, self.server.port)
1182
def test_empty_pass(self):
1183
self.server.add_user('joe', '')
1184
t = self._transport(self.get_user_url('joe', ''))
1185
self.assertEqual('contents of a\n', t.get('a').read())
1187
def test_user_pass(self):
1188
self.server.add_user('joe', 'foo')
1189
t = self._transport(self.get_user_url('joe', 'foo'))
1190
self.assertEqual('contents of a\n', t.get('a').read())
1192
def test_unknown_user(self):
1193
self.server.add_user('joe', 'foo')
1194
t = self._transport(self.get_user_url('bill', 'foo'))
1195
self.assertRaises(errors.InvalidHttpResponse, t.get, 'a')
1197
def test_wrong_pass(self):
1198
self.server.add_user('joe', 'foo')
1199
t = self._transport(self.get_user_url('joe', 'bar'))
1200
self.assertRaises(errors.InvalidHttpResponse, t.get, 'a')
1202
def test_prompt_for_password(self):
1203
self.server.add_user('joe', 'foo')
1204
t = self._transport(self.get_user_url('joe'))
1205
ui.ui_factory = TestUIFactory(stdin='foo\n', stdout=StringIOWrapper())
1206
self.assertEqual('contents of a\n',t.get('a').read())
1207
# stdin should be empty
1208
self.assertEqual('', ui.ui_factory.stdin.readline())
1209
# And we shouldn't prompt again for a different request
1210
# against the same transport.
1211
self.assertEqual('contents of b\n',t.get('b').read())
1213
# And neither against a clone
1214
self.assertEqual('contents of b\n',t2.get('b').read())