Commits

Lynn Rees committed 07a7d8b

- fixes

Comments (0)

Files changed (2)

urlrelay/tests/test_urlrelay.py

 
 @urlrelay.url(r'^/$')
 def index(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
+    start_response('200 OK', [('Content-Type', 'text/plain')])
     return ['index']
 
 @urlrelay.url(r'^/handle$', 'GET')
 def get_handle(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
+    start_response('200 OK', [('Content-Type', 'text/plain')])
     return ['GET']
 
 @urlrelay.url(r'^/handle$', 'POST')
 def post_handle(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
+    start_response('200 OK', [('Content-Type', 'text/plain')])
     return ['POST']
 
 @urlrelay.url(r'^/handle$', 'PUT')
 def put_handle(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
+    start_response('200 OK', [('Content-Type', 'text/plain')])
     return ['PUT']    
 
 @urlrelay.url(r'^/argtest/(?P<kwarg1>\w+)/(?P<kwarg2>\w+)/(\w+)/(\w+)$')
 def argtests(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
-    args, kwargs = environ['wsgiorg.routing_args']
-    return [' '.join([args[0], args[1], kwargs['kwarg1'], kwargs['kwarg2']])]
+    start_response('200 OK', [('Content-Type', 'text/plain')])
+    args, kw = environ['wsgiorg.routing_args']
+    return [' '.join([args[0], args[1], kw['kwarg1'], kw['kwarg2']])]
 
 @urlrelay.url(r'^/methtest/(?P<kwarg1>\w+)/(?P<kwarg2>\w+)/(\w+)/(\w+)$', 'GET')
 def meth_get(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
-    args, kwargs = environ['wsgiorg.routing_args']
-    return [' '.join(
-        ['GET', args[0], args[1], kwargs['kwarg1'], kwargs['kwarg2']]
-    )] 
+    start_response('200 OK', [('Content-Type', 'text/plain')])
+    args, kw = environ['wsgiorg.routing_args']
+    return [' '.join(['GET', args[0], args[1], kw['kwarg1'], kw['kwarg2']])] 
 
 @urlrelay.url(r'^/methtest/(?P<kwarg1>\w+)/(?P<kwarg2>\w+)/(\w+)/(\w+)$', 'POST')
 def meth_post2(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
-    args, kwargs = environ['wsgiorg.routing_args']
-    return [' '.join(
-        ['POST', args[0], args[1], kwargs['kwarg1'], kwargs['kwarg2']]
-    )] 
+    start_response('200 OK', [('Content-Type', 'text/plain')])
+    args, kw = environ['wsgiorg.routing_args']
+    return [' '.join(['POST', args[0], args[1], kw['kwarg1'], kw['kwarg2']])] 
 
 @urlrelay.url(r'^/methtest/(?P<kwarg1>\w+)/(?P<kwarg2>\w+)/(\w+)/(\w+)$', 'PUT')
 def meth_put(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
-    args, kwargs = environ['wsgiorg.routing_args']
-    return [' '.join(
-        ['PUT', args[0], args[1], kwargs['kwarg1'], kwargs['kwarg2']]
-    )]
+    start_response('200 OK', [('Content-Type', 'text/plain')])
+    args, kw = environ['wsgiorg.routing_args']
+    return [' '.join(['PUT', args[0], args[1], kw['kwarg1'], kw['kwarg2']])]
 
 @urlrelay.url(r'/spectest/', 'POST')
 def meth_post(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
+    start_response('200 OK', [('Content-Type', 'text/plain')])
     return ['Empty post']
 
 @urlrelay.url(r'/spectest/(.*)', 'GET')
 def meth_get2(environ, start_response):
-    start_response('200 Ok', [('Content-Type', 'text/plain')])
-    args, kwargs = environ['wsgiorg.routing_args']
+    start_response('200 OK', [('Content-Type', 'text/plain')])
+    args, kw = environ['wsgiorg.routing_args']
     return [' '.join(['GET', args[0]])]
 
 urlrelay.register('^/notfound$', 'urlrelay._handler')
 
     def test_inmem_argextract(self):
         '''Checks arg/kwarg extraction from URL.'''
-        environ = {'PATH_INFO':'/argtest/kwarg1/kwarg2/arg1/arg2',
-            'REQUEST_METHOD':'GET'}
+        environ = {
+            'PATH_INFO':'/argtest/kwarg1/kwarg2/arg1/arg2',
+            'REQUEST_METHOD':'GET'
+        }
         result = urlrelay.URLRelay()(environ, dummy_sr)
         self.assertEqual(result[0], 'arg1 arg2 kwarg1 kwarg2')
 
     def test_inmem_method_extract_get(self):
         '''Checks arg/kwarg extraction from URL + get method.'''
-        environ = {'PATH_INFO':'/methtest/kwarg1/kwarg2/arg1/arg2',
-            'REQUEST_METHOD':'GET'}
+        environ = {
+            'PATH_INFO':'/methtest/kwarg1/kwarg2/arg1/arg2',
+            'REQUEST_METHOD':'GET'
+        }
         result = urlrelay.URLRelay()(environ, dummy_sr)
         self.assertEqual(result[0], 'GET arg1 arg2 kwarg1 kwarg2')
 
     def test_inmem_method_extract_post(self):
         '''Checks arg/kwarg extraction from URL + post method.'''
-        environ = {'PATH_INFO':'/methtest/kwarg1/kwarg2/arg1/arg2',
-            'REQUEST_METHOD':'POST'}
+        environ = {
+            'PATH_INFO':'/methtest/kwarg1/kwarg2/arg1/arg2',
+            'REQUEST_METHOD':'POST'
+        }
         result = urlrelay.URLRelay()(environ, dummy_sr)
         self.assertEqual(result[0], 'POST arg1 arg2 kwarg1 kwarg2')
 
     def test_inmem_method_extract_put(self):
         '''Checks arg/kwarg extraction from URL + put method.'''
-        environ = {'PATH_INFO':'/methtest/kwarg1/kwarg2/arg1/arg2',
-            'REQUEST_METHOD':'PUT'}
+        environ = {
+            'PATH_INFO':'/methtest/kwarg1/kwarg2/arg1/arg2',
+            'REQUEST_METHOD':'PUT'
+        }
         result = urlrelay.URLRelay()(environ, dummy_sr)
         self.assertEqual(result[0], 'PUT arg1 arg2 kwarg1 kwarg2')
 
     def test_offdisk_method(self):
         '''Checks loading handler off of a disk.'''
-        environ = {'PATH_INFO':'/notfound',
-            'REQUEST_METHOD':'GET'}
+        environ = {'PATH_INFO':'/notfound', 'REQUEST_METHOD':'GET'}
         result = urlrelay.URLRelay()(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /notfound was not found on this server.'
+            'Requested URL was not found on this server.'
         )
 
     def test_offdisk_method_get(self):
         '''Checks loading handler off of a disk + get method.'''
-        environ = {'PATH_INFO':'/notfound2',
-            'REQUEST_METHOD':'GET'}
+        environ = {'PATH_INFO':'/notfound2', 'REQUEST_METHOD':'GET'}
         result = urlrelay.URLRelay()(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /notfound2 was not found on this server.'
+            'Requested URL was not found on this server.'
         )
 
     def test_offdisk_method_put(self):
         '''Checks loading handler off of a disk + put method.'''
-        environ = {'PATH_INFO':'/notfound2',
-            'REQUEST_METHOD':'PUT'}
+        environ = {'PATH_INFO':'/notfound2', 'REQUEST_METHOD':'PUT'}
         result = urlrelay.URLRelay()(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /notfound2 was not found on this server.'
+            'Requested URL was not found on this server.'
         )
 
     def test_offdisk_method_post(self):
         '''Checks loading handler off of a disk + post method.'''
-        environ = {'PATH_INFO':'/notfound2',
-            'REQUEST_METHOD':'POST'}
+        environ = {'PATH_INFO':'/notfound2', 'REQUEST_METHOD':'POST'}
         result = urlrelay.URLRelay()(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /notfound2 was not found on this server.'
+            'Requested URL was not found on this server.'
         )         
 
     def test_offdisk_method_modpath(self):
         '''Checks loading handler off of a disk.'''
-        environ = {'PATH_INFO':'/notfound4',
-            'REQUEST_METHOD':'GET'}
+        environ = {'PATH_INFO':'/notfound4', 'REQUEST_METHOD':'GET'}
         result = urlrelay.URLRelay(modpath='urlrelay')(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /notfound4 was not found on this server.'
+            'Requested URL was not found on this server.'
         )
 
     def test_offdisk_method_get_modpath(self):
         '''Checks loading handler off of a disk + get method.'''
-        environ = {'PATH_INFO':'/notfound3',
-            'REQUEST_METHOD':'GET'}
+        environ = {'PATH_INFO':'/notfound3', 'REQUEST_METHOD':'GET'}
         result = urlrelay.URLRelay(modpath='urlrelay')(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /notfound3 was not found on this server.'
+            'Requested URL was not found on this server.'
         )
 
     def test_offdisk_method_put_modpath(self):
         '''Checks loading handler off of a disk + put method.'''
-        environ = {'PATH_INFO':'/notfound3',
-            'REQUEST_METHOD':'PUT'}
+        environ = {'PATH_INFO':'/notfound3', 'REQUEST_METHOD':'PUT'}
         result = urlrelay.URLRelay(modpath='urlrelay')(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /notfound3 was not found on this server.'
+            'Requested URL was not found on this server.'
         )
 
     def test_offdisk_method_post_modpath(self):
         '''Checks loading handler off of a disk + post method.'''
-        environ = {'PATH_INFO':'/notfound3',
-            'REQUEST_METHOD':'POST'}
+        environ = {'PATH_INFO':'/notfound3', 'REQUEST_METHOD':'POST'}
         result = urlrelay.URLRelay(modpath='urlrelay')(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /notfound3 was not found on this server.'
+            'Requested URL was not found on this server.'
         )
 
     def test_inmem_defaultapp(self):
         result = urlrelay.URLRelay(default=index)(environ, dummy_sr)
         self.assertEqual(result[0], 'index')        
 
-    def test_inmem_defaultapp_args(self):
-        '''Checks arg/kwarg extraction from URL + put method.'''
-        environ = {'PATH_INFO':'/methkwarg1', 'REQUEST_METHOD':'GET'}
-        result = urlrelay.URLRelay(default=argtests, kwargs={'kwarg1':'kwarg1',
-            'kwarg2':'kwarg2'}, args=('arg1','arg2'))(environ, dummy_sr)
-        self.assertEqual(result[0], 'arg1 arg2 kwarg1 kwarg2')
-
     def test_inmem_defaultapp2(self):
         '''Checks using default app url.'''
         environ = {'PATH_INFO':'/plt', 'REQUEST_METHOD':'GET'}
         )(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /methkwarg1 was not found on this server.'
+            'Requested URL was not found on this server.'
         )
 
     def test_ondisk_defaultapp_modpath(self):
         )(environ, dummy_sr)
         self.assertEqual(
             result[0], 
-            'Requested URL /methkwarg1 was not found on this server.'
+            'Requested URL was not found on this server.'
         )
 
     def test_handler_override(self):
         environ = {'PATH_INFO':'/methkwarg1', 'REQUEST_METHOD':'GET'}
         result = urlrelay.URLRelay()(environ, dummy_sr)
         self.assertEqual(
-            result[0], 
-            'Requested URL /methkwarg1 was not found on this server.'
+            result[0],
+            'Requested URL was not found on this server.'
         )
 
     def test_nonregistry_paths_index(self):
         self.assertEqual(result[0], 'PUT')
 
     def test_noncallable_in_registry(self):
-        '''Checks that non callables or module strings are not allowed in
+        '''Checks that non-callables or module strings are not allowed in
         non-global path registry.'''
         environ = {'PATH_INFO':'/', 'REQUEST_METHOD':'PUT'}
         def tempfunc(): 
             test = urlrelay.URLRelay(paths=((r'^/$', []),))
             test(environ, dummy_sr)
-        self.assertRaises(AssertionError, tempfunc)   
+        self.assertRaises(AssertionError, tempfunc)
         
     def test_call_more_specific(self):
         '''Checks calling a more-specific URL than one that has a method

urlrelay/urlrelay.py

         if self._modpath != '' and isinstance(app, basestring):
             app = '.'.join([self._modpath, app])
         newapp = self._getapp(app)
-        # Cache for future use
-        self._cache[app] = newapp
         return newapp
 
     def resolve(self, path, method):
         @param method HTTP method
         '''
         key = ':'.join([path, method])
-        # Try fetching app and positional and keyword args from cache
-        try:
-            return self._cache[key]
-        except KeyError:
-            # Loop through path patterns -> applications
-            for pattern, app in self._paths:
-                # Test path for match
-                search = pattern.search(path)
-                # Continue with next iteration if no match
-                if not search: continue
-                # Get any app specifically linked to an HTTP method
-                if isinstance(app, dict): 
-                    if app is None: continue
-                    app = app.get(method)
-                app = self._loadapp(app)
-                # Ensure we have a callable
-                assert hasattr(app, '__call__')
-                # Extract any keyword arguments in the path
-                kw = search.groupdict()
-                # Extract any positional arguments, eliminating any duplicates
-                args = tuple(i for i in search.groups())
-                # Cache app, positional and keyword arguments
-                self._cache[key] = (app, args, kw)
-                return app, args, kw
-            # Return default app if no matching path and default app is set
-            if self._default is not None:
-                default = self._loadapp(self._default)
-                return default, (), {}
-            raise ImportError()
+        # Try fetching app from cache
+        app = self._cache.get(key)
+        if app is not None: return app
+        # Loop through path patterns -> applications
+        for pattern, app in self._paths:
+            # Test path for match
+            search = pattern.search(path)
+            # Continue with next iteration if no match
+            if not search: continue
+            # Get any app specifically linked to an HTTP method
+            if isinstance(app, dict): 
+                app = app.get(method)
+                if app is None: continue             
+            app = self._loadapp(app)
+            # Ensure we have a callable
+            assert hasattr(app, '__call__')
+            # Extract any keyword arguments in the path
+            kw = search.groupdict()
+            # Extract any positional arguments
+            args = tuple(i for i in search.groups() if i not in kw)
+            # Cache app, positional and keyword arguments
+            self._cache[key] = (app, args, kw)
+            return app, args, kw
+        # Return default app if no matching path and default app is set
+        if self._default is not None:
+            default = self._loadapp(self._default)
+            return default, (), {}
+        raise ImportError()