Rev 2370: Deeper tests, prepare the auth setting that will avoid the in file:///v/home/vila/src/bugs/72792/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Thu Apr 12 17:00:07 BST 2007
At file:///v/home/vila/src/bugs/72792/
------------------------------------------------------------
revno: 2370
revision-id: v.ladeuil+lp at free.fr-20070412160004-zrffqcemqyjb8gvq
parent: v.ladeuil+lp at free.fr-20070412142800-6e1pc8aksxlp7pg4
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 72792
timestamp: Thu 2007-04-12 18:00:04 +0200
message:
Deeper tests, prepare the auth setting that will avoid the
roundtrip with each 401.
* bzrlib/transport/http/_urllib2_wrappers.py:
(Request.__init__): Create the auth field.
(ConnectionHandler.get_key): Deleted.
(HTTPBasicAuthHandler.http_request): Add the authentication
parameter if the request requires it.
* bzrlib/tests/test_http.py:
(TestHTTPBasicAuth.setUp): Create the transport to access the
underlying _urllib2_wrappers opener.
(TestHTTPBasicAuth.process_request): Leave the opener process the
request based on the request.auth field.
modified:
bzrlib/tests/test_http.py testhttp.py-20051018020158-b2eef6e867c514d9
bzrlib/transport/http/_urllib2_wrappers.py _urllib2_wrappers.py-20060913231729-ha9ugi48ktx481ao-1
-------------- next part --------------
=== modified file 'bzrlib/tests/test_http.py'
--- a/bzrlib/tests/test_http.py 2007-04-12 13:09:53 +0000
+++ b/bzrlib/tests/test_http.py 2007-04-12 16:00:04 +0000
@@ -1147,30 +1147,34 @@
"""Test basic authentication scheme"""
_transport = HttpTransport_urllib
- _handler = _urllib2_wrappers.HTTPBasicAuthHandler()
_auth_header = 'Authorization'
_auth_type = 'basic'
def create_transport_readonly_server(self):
return HttpServer()
- def process_request(self, request):
+ def setUp(self):
+ super(TestHTTPBasicAuth, self).setUp()
+ self.transport = self._transport('http://bar.com')
+ self.opener = self.transport._opener
+
+ def process_request(self, request, user, password=None):
request.auth = self._auth_type
- self._handler.set_auth(request)
+ request.user = user
+ request.password = password
+ return self.opener.preprocess_request(request)
def test_empty_pass(self):
request = _urllib2_wrappers.Request('GET', 'http://bar.com')
request.user = 'joe'
request.password = ''
- self.process_request(request)
+ request = self.process_request(request, 'joe', '')
self.assertEqual('Basic ' + 'joe:'.encode('base64').strip(),
request.headers[self._auth_header])
def test_user_pass(self):
request = _urllib2_wrappers.Request('GET', 'http://bar.com')
- request.user = 'joe'
- request.password = 'foo'
- self.process_request(request)
+ request = self.process_request(request, 'joe', 'foo')
self.assertEqual('Basic ' + 'joe:foo'.encode('base64').strip(),
request.headers[self._auth_header])
=== modified file 'bzrlib/transport/http/_urllib2_wrappers.py'
--- a/bzrlib/transport/http/_urllib2_wrappers.py 2007-04-12 13:09:53 +0000
+++ b/bzrlib/transport/http/_urllib2_wrappers.py 2007-04-12 16:00:04 +0000
@@ -16,7 +16,7 @@
"""Implementaion of urllib2 tailored to bzr needs
-This file re-implements the urllib2 class hierarchy with custom classes.
+This file complements the urllib2 class hierarchy with custom classes.
For instance, we create a new HTTPConnection and HTTPSConnection that inherit
from the original urllib2.HTTP(s)Connection objects, but also have a new base
@@ -28,7 +28,7 @@
We have a custom Response class, which lets us maintain a keep-alive
connection even for requests that urllib2 doesn't expect to contain body data.
-And a custom Request class that lets us track redirections, and send
+And a custom Request class that lets us track redirections, and (soon) send
authentication data without requiring an extra round trip to get rejected by
the server. We also create a Request hierarchy, to make it clear what type
of request is being made.
@@ -149,6 +149,7 @@
# urllib2.Request will be confused if we don't extract
# authentification info before building the request
url, self.user, self.password = self.extract_auth(url)
+ self.auth = None # Until the first 401
urllib2.Request.__init__(self, url, data, headers,
origin_req_host, unverifiable)
self.method = method
@@ -204,15 +205,11 @@
internally used. But we need it in order to achieve
connection sharing. So, we add it to the request just before
it is processed, and then we override the do_open method for
- http[s] requests.
+ http[s] requests in AbstractHTTPHandler.
"""
handler_order = 1000 # after all pre-processings
- def get_key(self, connection):
- """Returns the key for the connection in the cache"""
- return '%s:%d' % (connection.host, connection.port)
-
def create_connection(self, request, http_connection_class):
host = request.get_host()
if not host:
@@ -737,11 +734,13 @@
request.add_header(self.auth_header,
self.get_auth(request.user, request.password))
-# def http_request(self, request):
-# """Insert an authentification header if information is available"""
-# if request.auth == 'basic' and request.password is not None:
-#
-# return request
+ def http_request(self, request):
+ """Insert an authentification header if information is available"""
+ if request.auth == 'basic' and request.password is not None:
+ self.set_auth(request)
+ return request
+
+ https_request = http_request # FIXME: Need test
class HTTPErrorProcessor(urllib2.HTTPErrorProcessor):
@@ -750,7 +749,6 @@
We don't really process the errors, quite the contrary
instead, we leave our Transport handle them.
"""
- handler_order = 1000 # after all other processing
def http_response(self, request, response):
code, msg, hdrs = response.code, response.msg, response.info()
@@ -805,7 +803,7 @@
self._opener = urllib2.build_opener( \
connection, redirect, error,
ProxyHandler,
- urllib2.HTTPBasicAuthHandler(self.password_manager),
+ HTTPBasicAuthHandler(self.password_manager),
#urllib2.HTTPDigestAuthHandler(self.password_manager),
#urllib2.ProxyBasicAuthHandler,
#urllib2.ProxyDigestAuthHandler,
@@ -821,3 +819,13 @@
import pprint
pprint.pprint(self._opener.__dict__)
+ def preprocess_request(self, request):
+ """Pre-process the request for test purposes"""
+ protocol = request.get_type()
+
+ # pre-process request
+ meth_name = protocol+"_request"
+ for processor in self._opener.process_request.get(protocol, []):
+ meth = getattr(processor, meth_name)
+ request = meth(request)
+ return request
More information about the bazaar-commits
mailing list