~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/HTTPTestUtil.py

  • Committer: Vincent Ladeuil
  • Date: 2007-04-17 10:33:39 UTC
  • mto: (2420.1.21 bzr.http.auth)
  • mto: This revision was merged to the branch mainline in revision 2463.
  • Revision ID: v.ladeuil+lp@free.fr-20070417103339-3kywr38d0p50czrw
Define tests for http proxy basic authentication. They fail.

* bzrlib/tests/test_http.py:
(TestHttpProxyWhiteBox._set_and_capture_env_var): Deleted. YAGNI.
(TestProxyHttpServer._set_and_capture_env_var): Deleted. YAGNI
(TestHTTPAuth): Made abstract so it can be reused for proxy.
(TestHTTPAuth.test_prompt_for_password): New class.
(TestHTTPAuth.test_prompt_for_password): New class. Failed tests so
far.

* bzrlib/tests/HTTPTestUtil.py:
(AbstractBasicAuthRequestHandler): Made abstract from
BasicAuthRequestHandler so it can be reused for proxy.
(ProxyBasicAuthRequestHandler): New class.
(ProxyBasicAuthHTTPServer): New class.

Show diffs side-by-side

added added

removed removed

Lines of Context:
309
309
       self.old_server = self.get_secondary_server()
310
310
 
311
311
 
312
 
class BasicAuthRequestHandler(TestingHTTPRequestHandler):
 
312
class AbstractBasicAuthRequestHandler(TestingHTTPRequestHandler):
313
313
    """Requires a basic authentication to process requests.
314
314
 
315
315
    This is intended to be used with a server that always and
316
316
    only use basic authentication.
317
317
    """
318
318
 
 
319
    # The following attribute should be set dy daughter classes
 
320
    _auth_header_sent = None
 
321
    _auth_header_recv = None
 
322
    _auth_error_code = None
 
323
 
319
324
    def do_GET(self):
320
325
        tcs = self.server.test_case_server
321
326
        if tcs.auth_scheme == 'basic':
322
 
            auth_header = self.headers.get('Authorization')
 
327
            auth_header = self.headers.get(self._auth_header_recv)
323
328
            authorized = False
324
329
            if auth_header and auth_header.lower().startswith('basic '):
325
330
                coded_auth = auth_header[len('Basic '):]
326
331
                user, password = coded_auth.decode('base64').split(':')
327
332
                authorized = tcs.authorized(user, password)
328
333
            if not authorized:
329
 
                self.send_response(401)
330
 
                self.send_header('www-authenticate',
 
334
                self.send_response(self._auth_error_code)
 
335
                self.send_header(self._auth_header_sent,
331
336
                                 'Basic realm="Thou should not pass"')
332
337
                self.end_headers()
333
338
                return
362
367
        return expected_password is not None and password == expected_password
363
368
 
364
369
 
 
370
class BasicAuthRequestHandler(AbstractBasicAuthRequestHandler,
 
371
                              FakeProxyRequestHandler):
 
372
    """Requires a basic authentication to process requests.
 
373
 
 
374
    Note: Each of the inherited request handler overrides
 
375
    different parts of processing in a compatible way, so it is
 
376
    okay to inherit from both.
 
377
    """
 
378
 
 
379
    _auth_header_sent = 'WWW-Authenticate'
 
380
    _auth_header_recv = 'Authorization'
 
381
    _auth_error_code = 401
 
382
 
 
383
 
365
384
class BasicAuthHTTPServer(AuthHTTPServer):
366
385
    """An HTTP server requiring basic authentication"""
367
386
 
368
387
    def __init__(self):
369
388
        AuthHTTPServer.__init__(self, BasicAuthRequestHandler, 'basic')
 
389
 
 
390
 
 
391
class ProxyBasicAuthRequestHandler(AbstractBasicAuthRequestHandler,
 
392
                                   FakeProxyRequestHandler):
 
393
    """Requires a basic authentication to proxy requests.
 
394
 
 
395
    Note: Each of the inherited request handler overrides
 
396
    different parts of processing in a compatible way, so it is
 
397
    okay to inherit from both.
 
398
    """
 
399
 
 
400
    _auth_header_sent = 'Proxy-Authenticate'
 
401
    _auth_header_recv = 'Proxy-Authorization'
 
402
    _auth_error_code = 407
 
403
 
 
404
 
 
405
class ProxyBasicAuthHTTPServer(AuthHTTPServer):
 
406
    """An HTTP server requiring basic authentication"""
 
407
 
 
408
    def __init__(self):
 
409
        AuthHTTPServer.__init__(self, ProxyBasicAuthRequestHandler, 'basic')