Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 376

Show
Ignore:
Timestamp:
06/24/05 12:29:34
Author:
fumanchu
Message:

Fixed InternalRedirect? to handle query strings.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cphttptools.py

    r371 r376  
    332332            return 
    333333        except cperror.InternalRedirect, x: 
    334             # Set the path to the new one (provided in the exception) 
    335             # and try again. 
    336             path = x.args[0] 
     334            # Try again with the new path 
     335            path = x.path 
    337336 
    338337def iterable(body): 
  • trunk/cherrypy/cperror.py

    r371 r376  
    5656 
    5757class InternalRedirect(Exception): 
    58     """Exception raised when processing should be handled by a different path.""" 
    59     pass 
     58    """Exception raised when processing should be handled by a different path. 
     59     
     60    If you supply a queryString, it will be used to re-populate paramMap. 
     61     
     62    If you omit queryString, the paramMap from the original request will 
     63    remain in effect, including any POST parameters. 
     64    """ 
     65     
     66    def __init__(self, path, queryString=None): 
     67        from cherrypy import cpg 
     68        import cgi 
     69         
     70        self.path = path 
     71        if queryString is not None: 
     72            cpg.request.queryString = queryString 
     73             
     74            pm = cgi.parse_qs(cpg.request.queryString, keep_blank_values=True) 
     75            for key, val in pm.items(): 
     76                if len(val) == 1: 
     77                    pm[key] = val[0] 
     78            cpg.request.paramMap = pm 
     79         
     80        cpg.request.browserUrl = cpg.request.base + path 
     81 
     82 
    6083 
    6184class HTTPRedirect(Exception): 
  • trunk/cherrypy/test/test_core.py

    r371 r376  
    9494    def internal(self): 
    9595        raise cperror.InternalRedirect("/") 
     96     
     97    def internal2(self, user_id): 
     98        if user_id == "parrot": 
     99            # Trade it for a slug when redirecting 
     100            raise cperror.InternalRedirect('/image/getImagesByUser', 
     101                                           "user_id=slug") 
     102        else: 
     103            raise cperror.InternalRedirect('/image/getImagesByUser') 
     104 
     105 
     106class Image(Test): 
     107     
     108    def getImagesByUser(self, user_id): 
     109        return "0 images for %s" % user_id 
    96110 
    97111 
     
    272286        self.assertEqual(cpg.response.body, 'hello') 
    273287        self.assertEqual(cpg.response.status, '200 OK') 
     288         
     289        helper.request("/redirect/internal2?user_id=Sir-not-appearing-in-this-film") 
     290        self.assertEqual(cpg.response.body, '0 images for Sir-not-appearing-in-this-film') 
     291        self.assertEqual(cpg.response.status, '200 OK') 
     292         
     293        helper.request("/redirect/internal2?user_id=parrot") 
     294        self.assertEqual(cpg.response.body, '0 images for slug') 
     295        self.assertEqual(cpg.response.status, '200 OK') 
    274296     
    275297    def testFlatten(self): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets