devtools/testlib.py
changeset 8720 d2f01aaae8c0
parent 8696 0bb18407c053
child 8721 b399c87df63c
equal deleted inserted replaced
8697:574bb05e40a4 8720:d2f01aaae8c0
   663     @deprecated("[3.15] app_handle_request is the new and better way"
   663     @deprecated("[3.15] app_handle_request is the new and better way"
   664                 " (beware of small semantic changes)")
   664                 " (beware of small semantic changes)")
   665     def app_publish(self, *args, **kwargs):
   665     def app_publish(self, *args, **kwargs):
   666         return self.app_handle_request(*args, **kwargs)
   666         return self.app_handle_request(*args, **kwargs)
   667 
   667 
   668     def ctrl_publish(self, req, ctrl='edit'):
   668     def ctrl_publish(self, req, ctrl='edit', rset=None):
   669         """call the publish method of the edit controller"""
   669         """call the publish method of the edit controller"""
   670         ctrl = self.vreg['controllers'].select(ctrl, req, appli=self.app)
   670         ctrl = self.vreg['controllers'].select(ctrl, req, appli=self.app)
   671         try:
   671         try:
   672             result = ctrl.publish()
   672             result = ctrl.publish(rset)
   673             req.cnx.commit()
   673             req.cnx.commit()
   674         except web.Redirect:
   674         except web.Redirect:
   675             req.cnx.commit()
   675             req.cnx.commit()
   676             raise
   676             raise
   677         return result
   677         return result
   679     def req_from_url(self, url):
   679     def req_from_url(self, url):
   680         """parses `url` and builds the corresponding CW-web request
   680         """parses `url` and builds the corresponding CW-web request
   681 
   681 
   682         req.form will be setup using the url's query string
   682         req.form will be setup using the url's query string
   683         """
   683         """
   684         req = self.request()
   684         req = self.request(url=url)
   685         if isinstance(url, unicode):
   685         if isinstance(url, unicode):
   686             url = url.encode(req.encoding) # req.setup_params() expects encoded strings
   686             url = url.encode(req.encoding) # req.setup_params() expects encoded strings
   687         querystring = urlparse.urlparse(url)[-2]
   687         querystring = urlparse.urlparse(url)[-2]
   688         params = urlparse.parse_qs(querystring)
   688         params = urlparse.parse_qs(querystring)
   689         req.setup_params(params)
   689         req.setup_params(params)
   690         return req
   690         return req
   691 
   691 
   692     def url_publish(self, url):
   692     def url_publish(self, url, data=None):
   693         """takes `url`, uses application's app_resolver to find the
   693         """takes `url`, uses application's app_resolver to find the appropriate
   694         appropriate controller, and publishes the result.
   694         controller and result set, then publishes the result.
       
   695 
       
   696         To simulate post of www-form-encoded data, give a `data` dictionary
       
   697         containing desired key/value associations.
   695 
   698 
   696         This should pretty much correspond to what occurs in a real CW server
   699         This should pretty much correspond to what occurs in a real CW server
   697         except the apache-rewriter component is not called.
   700         except the apache-rewriter component is not called.
   698         """
   701         """
   699         req = self.req_from_url(url)
   702         req = self.req_from_url(url)
       
   703         if data is not None:
       
   704             req.form.update(data)
   700         ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   705         ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   701         return self.ctrl_publish(req, ctrlid)
   706         return self.ctrl_publish(req, ctrlid, rset)
   702 
   707 
   703     @staticmethod
   708     @staticmethod
   704     def _parse_location(req, location):
   709     def _parse_location(req, location):
   705         try:
   710         try:
   706             path, params = location.split('?', 1)
   711             path, params = location.split('?', 1)