def test_get_url_without_query(): url_h = "http://some-host.com/wps" url_q = "{}?service=WPS".format(url_h) url_p = urlparse(url_q) assert utils.get_url_without_query(url_q) == url_h assert utils.get_url_without_query(url_p) == url_h assert utils.get_url_without_query(url_h) == url_h
def test_get_url_without_query(): url_h = "http://some-host.com/wps" url_q = f"{url_h}?service=WPS" url_p = urlparse(url_q) assert get_url_without_query(url_q) == url_h assert get_url_without_query(url_p) == url_h assert get_url_without_query(url_h) == url_h
def parse_wps_process_config(config_entry): # type: (Union[JSON, str]) -> Tuple[str, str, List[str], bool] """ Parses the available WPS provider or process entry to retrieve its relevant information. :return: WPS provider name, WPS service URL, and list of process identifier(s). :raise ValueError: if the entry cannot be parsed correctly. """ if isinstance(config_entry, dict): svc_url = config_entry["url"] svc_name = config_entry.get("name") svc_proc = config_entry.get("id", []) svc_vis = asbool(config_entry.get("visible", False)) elif isinstance(config_entry, str): svc_url = config_entry svc_name = None svc_proc = [] svc_vis = False else: raise ValueError("Invalid service value: [{!s}].".format(config_entry)) url_p = urlparse(svc_url) qs_p = parse_qs(url_p.query) svc_url = get_url_without_query(url_p) svc_name = svc_name or get_sane_name(url_p.hostname) svc_proc = svc_proc or qs_p.get("identifier", []) # noqa if not isinstance(svc_name, str): raise ValueError("Invalid service value: [{!s}].".format(svc_name)) if not isinstance(svc_proc, list): raise ValueError("Invalid process value: [{!s}].".format(svc_proc)) return svc_name, svc_url, svc_proc, svc_vis
def mocked_app_request(method, url=None, session=None, **req_kwargs): """ Mock requests under the web test application under specific conditions. Request corresponding to :func:`requests.request` that instead gets executed by :class:`webTest.TestApp`, unless permitted to call real external requests. """ # if URL starts with '/' directly, it is the shorthand path for this test app, always mock # otherwise, filter according to full URL hostname url_test_app = get_weaver_url(app.app.registry) if only_local and not url.startswith("/") and not url.startswith( url_test_app): with session or RealSession() as request_session: return real_request(request_session, method, url, **req_kwargs) url, func, req_kwargs = _parse_for_app_req(method, url, **req_kwargs) redirects = req_kwargs.pop("allow_redirects", True) if url.startswith("mock://"): path = get_url_without_query(url.replace("mock://", "")) _resp = mocked_file_response(path, url) else: _resp = func(url, expect_errors=True, **req_kwargs) if redirects: # must handle redirects manually with TestApp while 300 <= _resp.status_code < 400: _resp = _resp.follow() _patch_response_methods(_resp, url) return _resp
def get_wps_local_status_location(url_status_location, container, must_exist=True): # type: (str, AnySettingsContainer, bool) -> Optional[str] """ Attempts to retrieve the local XML file path corresponding to the WPS status location as URL. :param url_status_location: URL reference pointing to some WPS status location XML. :param container: any settings container to map configured local paths. :param must_exist: return only existing path if enabled, otherwise return the parsed value without validation. :returns: found local file path if it exists, ``None`` otherwise. """ dir_path = get_wps_output_dir(container) if url_status_location and not urlparse(url_status_location).scheme in [ "", "file" ]: wps_out_url = get_wps_output_url(container) req_out_url = get_url_without_query(url_status_location) out_path = os.path.join( dir_path, req_out_url.replace(wps_out_url, "").lstrip("/")) else: out_path = url_status_location.replace("file://", "") found = os.path.isfile(out_path) if not found and "/jobs/" in url_status_location: job_uuid = url_status_location.rsplit("/jobs/", 1)[-1].split("/", 1)[0] if is_uuid(job_uuid): out_path_join = os.path.join(dir_path, f"{job_uuid}.xml") found = os.path.isfile(out_path_join) if found or not must_exist: out_path = out_path_join if not found and must_exist: out_path_join = os.path.join( dir_path, out_path[1:] if out_path.startswith("/") else out_path) if not os.path.isfile(out_path_join): LOGGER.debug( "Could not map WPS status reference [%s] to input local file path [%s].", url_status_location, out_path) return None out_path = out_path_join LOGGER.debug("Resolved WPS status reference [%s] as local file path [%s].", url_status_location, out_path) return out_path
def mocked_app_request(method, url=None, **req_kwargs): """ Request corresponding to :func:`requests.request` that instead gets executed by :class:`webTest.TestApp`, unless permitted to call real external requests. """ # if URL starts with '/' directly, it is the shorthand path for this test app, always mock # otherwise, filter according to full URL hostname url_test_app = get_weaver_url(app.app.registry) if only_local and not url.startswith("/") and not url.startswith( url_test_app): with RealSession() as session: return real_request(session, method, url, **req_kwargs) url, func, req_kwargs = _parse_for_app_req(method, url, **req_kwargs) if not url.startswith("mock://"): resp = func(url, expect_errors=True, **req_kwargs) setattr(resp, "content", resp.body) else: path = get_url_without_query(url.replace("mock://", "")) resp = mocked_file_response(path, url) return resp
def parse_wps_process_config(config_entry): # type: (Union[JSON, str]) -> Tuple[str, str, List[str], bool] """ Parses the available WPS provider or process entry to retrieve its relevant information. :return: WPS provider name, WPS service URL, and list of process identifier(s). :raise ValueError: if the entry cannot be parsed correctly. """ if isinstance(config_entry, dict): svc_url = config_entry["url"] svc_name = config_entry.get("name") svc_proc = config_entry.get("id", []) svc_vis = asbool(config_entry.get("visible", False)) elif isinstance(config_entry, str): svc_url = config_entry svc_name = None svc_proc = [] svc_vis = False else: raise ValueError("Invalid service value: [{!s}].".format(config_entry)) url_p = urlparse(svc_url) qs_p = parse_qs(url_p.query) svc_url = get_url_without_query(url_p) # if explicit name was provided, validate it (assert fail if not), # otherwise replace silently bad character since since is requested to be inferred svc_name = get_sane_name(svc_name or url_p.hostname, assert_invalid=bool(svc_name)) svc_proc = svc_proc or qs_p.get( "identifier", []) # noqa # 'identifier=a,b,c' techically allowed svc_proc = [proc.strip() for proc in svc_proc if proc.strip()] # remote empty if not isinstance(svc_name, str): raise ValueError("Invalid service value: [{!s}].".format(svc_name)) if not isinstance(svc_proc, list): raise ValueError("Invalid process value: [{!s}].".format(svc_proc)) return svc_name, svc_url, svc_proc, svc_vis