294 requestcls = fake.FakeRequest |
294 requestcls = fake.FakeRequest |
295 tags = TestCase.tags | Tags('cubicweb', 'cw_repo') |
295 tags = TestCase.tags | Tags('cubicweb', 'cw_repo') |
296 test_db_id = DEFAULT_EMPTY_DB_ID |
296 test_db_id = DEFAULT_EMPTY_DB_ID |
297 _cnxs = set() # establised connection |
297 _cnxs = set() # establised connection |
298 # stay on connection for leak detection purpose |
298 # stay on connection for leak detection purpose |
|
299 |
|
300 # anonymous is logged by default in cubicweb test cases |
|
301 anonymous_allowed = True |
299 |
302 |
300 def __init__(self, *args, **kwargs): |
303 def __init__(self, *args, **kwargs): |
301 self._admin_session = None |
304 self._admin_session = None |
302 self._admin_clt_cnx = None |
305 self._admin_clt_cnx = None |
303 self._current_session = None |
306 self._current_session = None |
890 querystring = urlparse.urlparse(url)[-2] |
895 querystring = urlparse.urlparse(url)[-2] |
891 params = urlparse.parse_qs(querystring) |
896 params = urlparse.parse_qs(querystring) |
892 req.setup_params(params) |
897 req.setup_params(params) |
893 return req |
898 return req |
894 |
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 |
895 def url_publish(self, url, data=None): |
914 def url_publish(self, url, data=None): |
896 """takes `url`, uses application's app_resolver to find the appropriate |
915 """takes `url`, uses application's app_resolver to find the appropriate |
897 controller and result set, then publishes the result. |
916 controller and result set, then publishes the result. |
898 |
917 |
899 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 |
900 containing desired key/value associations. |
919 containing desired key/value associations. |
901 |
920 |
902 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 |
903 except the apache-rewriter component is not called. |
922 except the apache-rewriter component is not called. |
904 """ |
923 """ |
905 req = self.req_from_url(url) |
924 with self.admin_request_from_url(url) as req: |
906 if data is not None: |
925 if data is not None: |
907 req.form.update(data) |
926 req.form.update(data) |
908 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)) |
909 return self.ctrl_publish(req, ctrlid, rset) |
928 return self.ctrl_publish(req, ctrlid, rset) |
910 |
929 |
911 def http_publish(self, url, data=None): |
930 def http_publish(self, url, data=None): |
912 """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 |
913 of errors. You may give form parameters using the `data` argument. |
932 of errors. You may give form parameters using the `data` argument. |
914 """ |
933 """ |
915 req = self.req_from_url(url) |
934 with self.admin_request_from_url(url) as req: |
916 if data is not None: |
935 if data is not None: |
917 req.form.update(data) |
936 req.form.update(data) |
918 with real_error_handling(self.app): |
937 with real_error_handling(self.app): |
919 result = self.app_handle_request(req, req.relative_path(False)) |
938 result = self.app_handle_request(req, req.relative_path(False)) |
920 return result, req |
939 return result, req |
921 |
940 |
922 @staticmethod |
941 @staticmethod |
923 def _parse_location(req, location): |
942 def _parse_location(req, location): |
924 try: |
943 try: |
925 path, params = location.split('?', 1) |
944 path, params = location.split('?', 1) |