Commits

Robert Brewer committed 2775d7b

Test and fix for #371 (Redirect of index method to trailing-slash URI uses the wrong path info).

  • Participants
  • Parent commits 2c4847f

Comments (0)

Files changed (2)

File cherrypy/_cphttptools.py

         # Check if the original path had a trailing slash (otherwise, do
         #   a redirect)
         if path[-1] != '/':
-            newUrl = path + '/'
-            if cherrypy.request.queryString:
-                newUrl += "?" + cherrypy.request.queryString
+            atoms = cherrypy.request.browserUrl.split("?", 1)
+            newUrl = atoms.pop(0) + '/'
+            if atoms:
+                newUrl += "?" + atoms[0]
             if getattr(cherrypy, "debug", None):
                 cherrypy.log("    Found: redirecting to %s" % newUrl, "DEBUG")
             raise cherrypy.HTTPRedirect(newUrl)

File cherrypy/test/test_core.py

 
 class Root:
     
+    _cpFilterList = []
+    
     def index(self):
         return "hello"
     index.exposed = True
 
 
 class TestType(type):
-    """Metaclass which automatically exposes all functions in each subclass."""
+    """Metaclass which automatically exposes all functions in each subclass,
+    and adds an instance of the subclass as an attribute of cherrypy.root.
+    """
     def __init__(cls, name, bases, dct):
         type.__init__(name, bases, dct)
         for value in dct.itervalues():
             raise cherrypy.InternalRedirect('/image/getImagesByUser')
 
 
+class Slash(Test):
+    def index(self):
+        return "slashed!"
+
+class VPrefix:
+    def __init__(self, prefix):
+        self.prefix = prefix
+    def onStartResource(self):
+        path = cherrypy.request.path
+        if path.startswith(self.prefix):
+            cherrypy.request.objectPath = path[len(self.prefix):]
+cherrypy.root._cpFilterList.append(VPrefix("/vpath"))
+
+
 class Image(Test):
     
     def getImagesByUser(self, user_id):
         self.getPage("/redirect/error")
         self.assertStatus('303 See Other')
         self.assertInBody('/errpage')
+        
+        # Trailing slash redirect on a virtualpath child.
+        self.getPage("/vpath/slash")
+        self.assertStatus('303 See Other')
+        self.assertInBody('/vpath/slash/')
     
     def testCPFilterList(self):
         self.getPage("/cpfilterlist/")