devtools/testlib.py
changeset 8312 6c2119509fac
parent 8311 76a44a0d7f4b
child 8318 e8a2fd7d9606
equal deleted inserted replaced
8311:76a44a0d7f4b 8312:6c2119509fac
   606         args = [dump(arg) for arg in args]
   606         args = [dump(arg) for arg in args]
   607         req = self.request(fname=fname, pageid='123', arg=args)
   607         req = self.request(fname=fname, pageid='123', arg=args)
   608         ctrl = self.vreg['controllers'].select('ajax', req)
   608         ctrl = self.vreg['controllers'].select('ajax', req)
   609         return ctrl.publish(), req
   609         return ctrl.publish(), req
   610 
   610 
   611     def app_publish(self, req, path='view'):
   611     def app_handle_request(self, req, path='view'):
   612         return self.app.publish(path, req)
   612         return self.app.core_handle(req, path)
   613 
   613 
   614     def ctrl_publish(self, req, ctrl='edit'):
   614     def ctrl_publish(self, req, ctrl='edit'):
   615         """call the publish method of the edit controller"""
   615         """call the publish method of the edit controller"""
   616         ctrl = self.vreg['controllers'].select(ctrl, req, appli=self.app)
   616         ctrl = self.vreg['controllers'].select(ctrl, req, appli=self.app)
   617         try:
   617         try:
   644         """
   644         """
   645         req = self.req_from_url(url)
   645         req = self.req_from_url(url)
   646         ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   646         ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   647         return self.ctrl_publish(req, ctrlid)
   647         return self.ctrl_publish(req, ctrlid)
   648 
   648 
       
   649     @staticmethod
       
   650     def _parse_location(req, location):
       
   651         try:
       
   652             path, params = location.split('?', 1)
       
   653         except ValueError:
       
   654             path = location
       
   655             params = {}
       
   656         else:
       
   657             cleanup = lambda p: (p[0], unquote(p[1]))
       
   658             params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p)
       
   659         if path.startswith(req.base_url()): # may be relative
       
   660             path = path[len(req.base_url()):]
       
   661         return path, params
       
   662 
   649     def expect_redirect(self, callback, req):
   663     def expect_redirect(self, callback, req):
   650         """call the given callback with req as argument, expecting to get a
   664         """call the given callback with req as argument, expecting to get a
   651         Redirect exception
   665         Redirect exception
   652         """
   666         """
   653         try:
   667         try:
   654             callback(req)
   668             callback(req)
   655         except Redirect, ex:
   669         except Redirect, ex:
   656             try:
   670             return self._parse_location(req, ex.location)
   657                 path, params = ex.location.split('?', 1)
       
   658             except ValueError:
       
   659                 path = ex.location
       
   660                 params = {}
       
   661             else:
       
   662                 cleanup = lambda p: (p[0], unquote(p[1]))
       
   663                 params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p)
       
   664             if path.startswith(req.base_url()): # may be relative
       
   665                 path = path[len(req.base_url()):]
       
   666             return path, params
       
   667         else:
   671         else:
   668             self.fail('expected a Redirect exception')
   672             self.fail('expected a Redirect exception')
   669 
   673 
   670     def expect_redirect_publish(self, req, path='edit'):
   674     def expect_redirect_handle_request(self, req, path='edit'):
   671         """call the publish method of the application publisher, expecting to
   675         """call the publish method of the application publisher, expecting to
   672         get a Redirect exception
   676         get a Redirect exception
   673         """
   677         """
   674         return self.expect_redirect(lambda x: self.app_publish(x, path), req)
   678         result = self.app_handle_request(req, path)
       
   679         self.assertTrue(300 <= req.status_out <400, req.status_out)
       
   680         location = req.get_response_header('location')
       
   681         return self._parse_location(req, location)
   675 
   682 
   676     def set_auth_mode(self, authmode, anonuser=None):
   683     def set_auth_mode(self, authmode, anonuser=None):
   677         self.set_option('auth-mode', authmode)
   684         self.set_option('auth-mode', authmode)
   678         self.set_option('anonymous-user', anonuser)
   685         self.set_option('anonymous-user', anonuser)
   679         if anonuser is None:
   686         if anonuser is None: