devtools/testlib.py
changeset 9931 013dd868e6da
parent 9929 16163ee1cdf9
child 9933 3674f249ab1d
equal deleted inserted replaced
9930:d20c2b262f55 9931:013dd868e6da
   881         except web.Redirect:
   881         except web.Redirect:
   882             req.cnx.commit()
   882             req.cnx.commit()
   883             raise
   883             raise
   884         return result
   884         return result
   885 
   885 
       
   886     @deprecated('[3.19] use .admin_request_from_url instead')
   886     def req_from_url(self, url):
   887     def req_from_url(self, url):
   887         """parses `url` and builds the corresponding CW-web request
   888         """parses `url` and builds the corresponding CW-web request
   888 
   889 
   889         req.form will be setup using the url's query string
   890         req.form will be setup using the url's query string
   890         """
   891         """
   894         querystring = urlparse.urlparse(url)[-2]
   895         querystring = urlparse.urlparse(url)[-2]
   895         params = urlparse.parse_qs(querystring)
   896         params = urlparse.parse_qs(querystring)
   896         req.setup_params(params)
   897         req.setup_params(params)
   897         return req
   898         return req
   898 
   899 
       
   900     @contextmanager
       
   901     def admin_request_from_url(self, url):
       
   902         """parses `url` and builds the corresponding CW-web request
       
   903 
       
   904         req.form will be setup using the url's query string
       
   905         """
       
   906         with self.admin_access.web_request(url=url) as req:
       
   907             if isinstance(url, unicode):
       
   908                 url = url.encode(req.encoding) # req.setup_params() expects encoded strings
       
   909             querystring = urlparse.urlparse(url)[-2]
       
   910             params = urlparse.parse_qs(querystring)
       
   911             req.setup_params(params)
       
   912             yield req
       
   913 
   899     def url_publish(self, url, data=None):
   914     def url_publish(self, url, data=None):
   900         """takes `url`, uses application's app_resolver to find the appropriate
   915         """takes `url`, uses application's app_resolver to find the appropriate
   901         controller and result set, then publishes the result.
   916         controller and result set, then publishes the result.
   902 
   917 
   903         To simulate post of www-form-encoded data, give a `data` dictionary
   918         To simulate post of www-form-encoded data, give a `data` dictionary
   904         containing desired key/value associations.
   919         containing desired key/value associations.
   905 
   920 
   906         This should pretty much correspond to what occurs in a real CW server
   921         This should pretty much correspond to what occurs in a real CW server
   907         except the apache-rewriter component is not called.
   922         except the apache-rewriter component is not called.
   908         """
   923         """
   909         req = self.req_from_url(url)
   924         with self.admin_request_from_url(url) as req:
   910         if data is not None:
   925             if data is not None:
   911             req.form.update(data)
   926                 req.form.update(data)
   912         ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   927             ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   913         return self.ctrl_publish(req, ctrlid, rset)
   928             return self.ctrl_publish(req, ctrlid, rset)
   914 
   929 
   915     def http_publish(self, url, data=None):
   930     def http_publish(self, url, data=None):
   916         """like `url_publish`, except this returns a http response, even in case
   931         """like `url_publish`, except this returns a http response, even in case
   917         of errors. You may give form parameters using the `data` argument.
   932         of errors. You may give form parameters using the `data` argument.
   918         """
   933         """
   919         req = self.req_from_url(url)
   934         with self.admin_request_from_url(url) as req:
   920         if data is not None:
   935             if data is not None:
   921             req.form.update(data)
   936                 req.form.update(data)
   922         with real_error_handling(self.app):
   937             with real_error_handling(self.app):
   923             result = self.app_handle_request(req, req.relative_path(False))
   938                 result = self.app_handle_request(req, req.relative_path(False))
   924         return result, req
   939             return result, req
   925 
   940 
   926     @staticmethod
   941     @staticmethod
   927     def _parse_location(req, location):
   942     def _parse_location(req, location):
   928         try:
   943         try:
   929             path, params = location.split('?', 1)
   944             path, params = location.split('?', 1)