[z3-checkins] r41921 - in z3/deliverance/DeliveranceVHoster/trunk: docs dvhoster tests

ianb at codespeak.net ianb at codespeak.net
Thu Apr 5 23:37:09 CEST 2007


Author: ianb
Date: Thu Apr  5 23:37:08 2007
New Revision: 41921

Modified:
   z3/deliverance/DeliveranceVHoster/trunk/docs/example_init_domain.py
   z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dataprovider.py
   z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py
   z3/deliverance/DeliveranceVHoster/trunk/tests/test_functional_api.py
   z3/deliverance/DeliveranceVHoster/trunk/tests/test_init_func.py
Log:
Fixed some tests that were unnecessarily failing.  Add two new hooks -- find_remote_uri and should_theme_uri, which can do custom remote URI resolution, or disable theming entirely.  Sort remote_uris by length (longest path first) so that there can never be dead (unreachable) paths.  Allow a headers key in that dictionary, to set extra headers on the request.  Removed a bunch of debugging print statements

Modified: z3/deliverance/DeliveranceVHoster/trunk/docs/example_init_domain.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/docs/example_init_domain.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/docs/example_init_domain.py	Thu Apr  5 23:37:08 2007
@@ -36,4 +36,23 @@
         ('X-Openplans-Project', project),
         ]
     
-    
+def find_remote_uri(remote_uri, remote_uri_info, environ,
+                    app_conf):
+    from paste.request import path_info_pop
+    if remote_uri is not None:
+        return remote_uri
+    path_info = environ.get('PATH_INFO', '')
+    if path_info.startswith('/tasks'):
+        remote_uri = 'http://localhost:5000'
+        # Move /tasks to SCRIPT_NAME
+        path_info_pop(environ)
+    else:
+        remote_uri = 'http://localhost:8080'
+    return remote_uri
+
+def should_theme_uri(remote_uri, environ, app_conf):
+    try:
+        no_theme = int(environ.get('HTTP_X_NO_THEME', '0'))
+    except ValueError:
+        no_theme = 0
+    return not no_theme

Modified: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dataprovider.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dataprovider.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dataprovider.py	Thu Apr  5 23:37:08 2007
@@ -231,6 +231,7 @@
     required_keys = ()
     optional_keys = ()
     trail_slash_keys = ()
+    sort_key = None
     
     def validate_python(self, value, state=None):
         # Should be like [{'path': path, 'remote_uri': uri, 'comment': str}]
@@ -263,12 +264,16 @@
                     continue
                 if not d[key].endswith('/'):
                     d[key] += '/'
+        if self.sort_key:
+            value = sorted(value, key=self.sort_key)
         return value
 
 class RemoteURIValidator(ListDictValidator):
-    required_keys = ('path', 'remote_uri')
-    optional_keys = ('comment', )
+    required_keys = ('path', )
+    optional_keys = ('comment', 'headers', 'remote_uri')
     trail_slash_keys = ('path', 'remote_uri')
+    def sort_key(self, item):
+        return -len(item['path'])
 
 class RewriteValidator(ListDictValidator):
     required_keys = ('rewrite', )

Modified: z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/dvhoster/dispatcher.py	Thu Apr  5 23:37:08 2007
@@ -33,6 +33,14 @@
         if init_domain:
             init_domain = load_func(init_domain, 'init_domain')
         self.init_domain = init_domain
+        find_remote_uri = app_conf.get('find_remote_uri')
+        if find_remote_uri:
+            find_remote_uri = load_func(find_remote_uri, 'find_remote_uri')
+        self.find_remote_uri = find_remote_uri
+        should_theme_uri = app_conf.get('should_theme_uri')
+        if should_theme_uri:
+            should_theme_uri = load_func(should_theme_uri, 'should_theme_uri')
+        self.should_theme_uri = should_theme_uri
         self.app_conf = app_conf
         self.provider = DataProvider(data_dir)
         self.rewrite_links = asbool(app_conf.get('rewrite_links', True))
@@ -59,7 +67,6 @@
             environ, with_query_string=False,
             path_info='')
         path_info = norm_path(environ.get('PATH_INFO', ''))
-        print 'path_info', repr(path_info), repr(environ['PATH_INFO'])
         if path_info.startswith('/.deliverance'):
             path_info_pop(environ)
             subapp = ProviderApp(domain_info)
@@ -129,21 +136,22 @@
         for header_name, header_value in domain_info.additional_request_headers:
             header_name = 'HTTP_%s' % header_name.upper().replace('-', '_')
             environ[header_name] = header_value
-        
+
         for remote_uri_info in remote_uris:
-            path = remote_uri_info['path']
+            path = str(remote_uri_info['path'])
             if not path.endswith('/'):
                 path += '/'
             if path_info + '/' == path:
-                print 'redirect', [remote_uri_info, path, path_info]
                 exc = httpexceptions.HTTPMovedPermanently(
                     headers=[('location', construct_url(environ, path_info=path_info+'/'))])
                 return exc(environ, start_response)
             if path_info.startswith(path):
                 # Found a match
-                remote_uri = remote_uri_info['remote_uri']
+                remote_uri = remote_uri_info.get('remote_uri')
                 environ['SCRIPT_NAME'] += path[:-1]
                 environ['PATH_INFO'] = path_info[len(path)-1:]
+                if self.find_remote_uri:
+                    remote_uri = self.find_remote_uri(remote_uri, remote_uri_info, environ, self.app_conf)
                 break
         if not remote_uri:
             exc = httpexceptions.HTTPNotFound(
@@ -152,8 +160,10 @@
                 % (path_info, ', '.join([repr(r['path'])
                                          for r in remote_uris])))
             return exc(environ, start_response)
-        print 'Proxy to %r with %r (SCRIPT_NAME=%r)' % (
-            remote_uri, construct_url(environ), environ['SCRIPT_NAME'])
+        should_theme_uri = True
+        if self.should_theme_uri:
+            should_theme_uri = self.should_theme_uri(
+                remote_uri, environ, self.app_conf)
         app = proxyapp.ForcedProxy(
             remote=remote_uri,
             force_host=True)
@@ -163,11 +173,12 @@
         rule_uri = construct_url(
             environ, with_query_string=False,
             path_info='/_rules/rule.xml')
-        app = DeliveranceMiddleware(
-            app,
-            theme_uri=domain_info.theme_uri,
-            rule_uri=rule_uri,
-            renderer=Renderer)
+        if should_theme_uri:
+            app = DeliveranceMiddleware(
+                app,
+                theme_uri=domain_info.theme_uri,
+                rule_uri=rule_uri,
+                renderer=Renderer)
         return app(environ, start_response)
 
     def find_file(self, path):

Modified: z3/deliverance/DeliveranceVHoster/trunk/tests/test_functional_api.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/tests/test_functional_api.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/tests/test_functional_api.py	Thu Apr  5 23:37:08 2007
@@ -8,6 +8,7 @@
 for attr in ['get_app', 'connect']:
     setattr(httplib.HTTPConnection, attr,
             getattr(wsgi_intercept.WSGI_HTTPConnection, attr).im_func)
+from wsgifilter.proxyapp import DebugHeaders
 
 data_filename = os.path.join(os.path.dirname(__file__), 'test-data')
 wsgi_app = make_app({}, data_dir=data_filename)
@@ -30,13 +31,6 @@
   </rules>
 '''
 
-static_app = StaticURLParser(os.path.join(os.path.dirname(__file__),
-                                          'test-static'))
-def make_static_app():
-    return static_app
-
-wsgi_intercept.add_wsgi_intercept('wsgify.org', 80, make_static_app)
-
 
 def test_everything():
     yield (reset_env,)
@@ -74,8 +68,10 @@
 
     data = '''
     [{"path": "/bar", "remote_uri": "http://wsgify.org/blah", "comment": "x"},
-     {"path": "/", "remote_uri": "http://wsgify.org/"}]
+     {"path": "/", "remote_uri": "http://wsgify.org/"}
+     ]
      '''
+    #{"path": "/testme", "headers": {"X-Test-Me": "testme"}
 
     put('/.deliverance/remote_uris', data)
     # It gets normalized, so it doesn't actually stay the same:
@@ -88,6 +84,8 @@
     #res = res.follow()
     #print res
     #assert res.status == 200
+    ## So instead we just get a page we know works:
+    res = app.get('/bar/index.html', status=200)
 
     data = '''
     [{"path": "/test1.html", "rewrite": "/test1"},
@@ -146,3 +144,14 @@
     res.mustcontain('localhost')
     assert app.get('/.deliverance/domain').body == 'localhost2'
     print 'site renamed to localhost2'
+
+
+def setup_module(module):
+    static_app = DebugHeaders(StaticURLParser(os.path.join(os.path.dirname(__file__),
+                                              'test-static')))
+
+    wsgi_intercept.add_wsgi_intercept('wsgify.org', 80, lambda : static_app)
+
+def teardown_module(module):
+    wsgi_intercept.remove_wsgi_intercept('wsgify.org', 80)
+    

Modified: z3/deliverance/DeliveranceVHoster/trunk/tests/test_init_func.py
==============================================================================
--- z3/deliverance/DeliveranceVHoster/trunk/tests/test_init_func.py	(original)
+++ z3/deliverance/DeliveranceVHoster/trunk/tests/test_init_func.py	Thu Apr  5 23:37:08 2007
@@ -1,19 +1,29 @@
 import os
 import shutil
-import shutil
 from paste.fixture import TestApp
 from dvhoster.wsgiapp import make_app
 import httplib
 import simplejson
+import urllib
+import wsgi_intercept
+for attr in ['get_app', 'connect']:
+    setattr(httplib.HTTPConnection, attr,
+            getattr(wsgi_intercept.WSGI_HTTPConnection, attr).im_func)
 
 example = os.path.join(os.path.dirname(os.path.dirname(__file__)),
                        'docs', 'example_init_domain.py')
 
 data_filename = os.path.join(os.path.dirname(__file__), 'test-data')
+if os.path.exists(data_filename):
+    shutil.rmtree(data_filename)
+os.mkdir(data_filename)
+
 wsgi_app = make_app({},
                     init_domain=example,
+                    find_remote_uri=example,
+                    should_theme_uri=example,
                     zope_location='http://localhost:8080',
-                    default_theme_uri='http://yahoo.com',
+                    default_theme_uri='http://openplans.org',
                     data_dir=data_filename)
 app = TestApp(wsgi_app)
 
@@ -30,3 +40,39 @@
     print got
     print expected
     assert got == expected
+
+def test_find_remote_uri():
+    # First make double-sure wsgi_intercept is working:
+    f = urllib.urlopen('http://localhost:8080/foo')
+    c = f.read()
+    f.close()
+    assert "SERVER_PORT: '8080'" in c
+    # Now to test find_remote_uri; first we have to get rid of the
+    # remote_uri value that init_domain set:
+    extra = {'HTTP_HOST': 'foo2.openplans.org'}
+    app.post('/.deliverance/remote_uris', '[{"path": ""}]', extra_environ={'HTTP_HOST': 'foo2.openplans.org', 'REQUEST_METHOD': 'PUT', 'CONTENT_TYPE': 'application/json'}, status=204)
+    res = app.get('/tasks', extra_environ=extra)
+    res.mustcontain("SERVER_PORT: '5000'")
+    res = app.get('/blah', extra_environ=extra)
+    res.mustcontain("SERVER_PORT: '8080'")
+
+def test_should_theme_uri():
+    # We need HTML so it can be themed:
+    setup_module(None, text=False)
+    res = app.get('/', extra_environ={'HTTP_HOST': 'foo3.openplans.org'})
+    # A sign it was themed:
+    res.mustcontain('Plone')
+    res = app.get('/', extra_environ={'HTTP_X_NO_THEME': '1', 'HTTP_HOST': 'foo3.openplans.org'})
+    assert 'Plone' not in res
+    
+def setup_module(module, text=True):
+    from paste.script import testapp
+    app = testapp.TestApplication(text=text)
+    for port in [5000, 8080]:
+        wsgi_intercept.add_wsgi_intercept(
+            'localhost', port, lambda : app)
+
+def teardown_module(module):
+    for port in [5000, 8080]:
+        wsgi_intercept.remove_wsgi_intercept(
+            'localhost', port)


More information about the z3-checkins mailing list