[Checkins] SVN: zope.app.wsgi/branches/janjaapdriessen-webtest/src/zope/app/wsgi/testlayer.py Use AuthorizationMiddleware from zope.testbrowser.wsgi and use zope.testbrowser.wsgi.Layer.get_app() to get the application under test

Brian Sutherland jinty at web.de
Thu Mar 10 09:39:20 EST 2011


Log message for revision 120842:
  Use AuthorizationMiddleware from zope.testbrowser.wsgi and use  zope.testbrowser.wsgi.Layer.get_app() to get the application under test

Changed:
  U   zope.app.wsgi/branches/janjaapdriessen-webtest/src/zope/app/wsgi/testlayer.py

-=-
Modified: zope.app.wsgi/branches/janjaapdriessen-webtest/src/zope/app/wsgi/testlayer.py
===================================================================
--- zope.app.wsgi/branches/janjaapdriessen-webtest/src/zope/app/wsgi/testlayer.py	2011-03-10 11:09:36 UTC (rev 120841)
+++ zope.app.wsgi/branches/janjaapdriessen-webtest/src/zope/app/wsgi/testlayer.py	2011-03-10 14:39:18 UTC (rev 120842)
@@ -14,8 +14,6 @@
 from StringIO import StringIO
 import httplib
 import xmlrpclib
-import base64
-import re
 
 import transaction
 from zope.app.appsetup.testlayer import ZODBLayer
@@ -47,55 +45,7 @@
             yield entry
         self.root_factory()._p_jar.sync()
 
-basicre = re.compile('Basic (.+)?:(.+)?$')
 
-def auth_header(header):
-    """This function takes an authorization HTTP header and encode the
-    couple user, password into base 64 like the HTTP protocol wants
-    it.
-    """
-    match = basicre.match(header)
-    if match:
-        u, p = match.group(1, 2)
-        if u is None:
-            u = ''
-        if p is None:
-            p = ''
-        auth = base64.encodestring('%s:%s' % (u, p))
-        return 'Basic %s' % auth[:-1]
-    return header
-
-def is_wanted_header(header):
-    """Return True if the given HTTP header key is wanted.
-    """
-    key, value = header
-    return key.lower() not in ('x-content-type-warning', 'x-powered-by')
-
-class AuthorizationMiddleware(object):
-    """This middleware makes the WSGI application compatible with the
-    HTTPCaller behavior defined in zope.app.testing.functional:
-    - It modifies the HTTP Authorization header to encode user and
-      password into base64 if it is Basic authentication.
-    """
-
-    def __init__(self, wsgi_stack):
-        self.wsgi_stack = wsgi_stack
-
-    def __call__(self, environ, start_response):
-        # Handle authorization
-        auth_key = 'HTTP_AUTHORIZATION'
-        if auth_key in environ:
-            environ[auth_key] = auth_header(environ[auth_key])
-
-        # Remove unwanted headers
-        def application_start_response(status, headers, exc_info=None):
-            headers = filter(is_wanted_header, headers)
-            start_response(status, headers)
-
-        for entry in self.wsgi_stack(environ, application_start_response):
-            yield entry
-
-
 class HandleErrorsMiddleware(object):
     """This middleware makes the WSGI application compatible with the
     HTTPCaller behavior defined in zope.app.testing.functional:
@@ -139,7 +89,7 @@
         # off of that in testSetUp()
         fake_db = object()
         self._application = WSGIPublisherApplication(fake_db)
-        return AuthorizationMiddleware(HandleErrorsMiddleware(
+        return zope.testbrowser.wsgi.AuthorizationMiddleware(HandleErrorsMiddleware(
             self._application,
             TransactionMiddleware(
                 self.getRootFolder,
@@ -193,7 +143,7 @@
 
 def http(string, handle_errors=True):
 
-    app = zope.testbrowser.wsgi._APP_UNDER_TEST
+    app = zope.testbrowser.wsgi.Layer.get_app()
     if app is None:
         raise NotInBrowserLayer(NotInBrowserLayer.__doc__)
 



More information about the checkins mailing list