def resolve(cls, settings, url, must_raise=True, abspath_func=abspath, exists_func=exists): """Resolves a url given a string and a settings. Raises TypeError when parameters are wrong, unless the must_raise parameter is False""" if not isinstance(settings, Settings): if must_raise: raise TypeError('PageRegistry.resolve takes a pyccuracy.common.Settings object first parameter. Got %r.' % settings) else: return None if not isinstance(url, basestring): if must_raise: raise TypeError('PageRegistry.resolve argument 2 must be a string. Got %r.' % url) else: return None klass_object = cls.get_by_name(url) or cls.get_by_url(url) if klass_object: url = klass_object.url url_pieces = [] if not url.startswith("http"): if settings.base_url: url_pieces.append(settings.base_url) else: url_pieces.append(settings.tests_dirs[0]) #gotta think of a way to fix this if klass_object: url_pieces.append(klass_object.url) if hasattr(klass_object, 'port') and url_pieces[0].startswith("http"): url_pieces[0] = "%s:%d" % (url_pieces[0], klass_object.port) else: url_pieces.append(url) # if use os.path.join here, will not work on windows fix = lambda x: x.replace('//', '/').replace('http:/', 'http://').replace('https:/', 'https://') final_url = fix("/".join(url_pieces)) if not "://" in final_url: almost_final_url = (final_url.startswith("/") and final_url) or "/%s" % final_url final_url = "file://%s" % abspath_func(almost_final_url) if final_url.startswith("/"): final_url = final_url[1:] checker = URLChecker() checker.set_url(final_url) if not checker.is_valid(): error_message = "The url %r is not valid." % final_url if klass_object: error_message += " In class %s, path %r" % (klass_object.__name__, klass_object.__module__) if not final_url.startswith('file://'): raise InvalidUrlError(error_message) return klass_object, final_url
def execute(self, values, context): url = values[0] base_url = context.base_url if url.replace(" ", "") in context.all_pages: context.current_page = context.all_pages[url.replace(" ", "")] url = context.current_page.url if base_url: url = basejoin(base_url + "/", url) protocol, page_name, file_name, complement, querystring, anchor = urllib2.urlparse.urlparse(url) if not protocol: if not base_url and os.path.exists(abspath(join(context.tests_dir, url))): url = "file://" + abspath(join(context.tests_dir, url)) elif os.path.exists(url): url = "file://" + abspath(url) else: checker = URLChecker() checker.set_url(url) if not checker.is_valid(): raise ActionFailedError(self.language['page_go_to_failure'] % url) self.browser_driver.page_open(url) self.browser_driver.wait_for_page()
def test_url_checker_with_port_with_sub_folder_in_localhost(): urlmock = Mock() urlmock.expects(once()) \ .urlopen(eq("http://localhost:8080/login")) \ .will(return_value(None)) checker = URLChecker(lib=urlmock) checker.set_url("http://localhost:8080/login") assert checker.url == "http://localhost:8080/login" assert checker.is_valid() assert checker.exists() urlmock.verify()
def test_url_checker_with_port(): urlmock = Mock() urlmock.expects(once()) \ .urlopen(eq("http://foo.bar.com:8080")) \ .will(return_value(None)) checker = URLChecker(lib=urlmock) checker.set_url("http://foo.bar.com:8080") assert checker.url == "http://foo.bar.com:8080" assert checker.is_valid() assert checker.exists() urlmock.verify()
def test_url_checker_with_port_with_sub_folder_in_localhost(): mocker = Mocker() urlmock = mocker.mock() urlmock.urlopen("http://localhost:8080/login") mocker.result(None) with mocker: checker = URLChecker(lib=urlmock) checker.set_url("http://localhost:8080/login") assert checker.url == "http://localhost:8080/login" assert checker.is_valid() assert checker.exists()
def test_url_checker_with_port(): mocker = Mocker() urlmock = mocker.mock() urlmock.urlopen("http://foo.bar.com:8080") mocker.result(None) with mocker: checker = URLChecker(lib=urlmock) checker.set_url("http://foo.bar.com:8080") assert checker.url == "http://foo.bar.com:8080" assert checker.is_valid() assert checker.exists()
def resolve(cls, settings, url, must_raise=True, abspath_func=abspath, exists_func=exists): """Resolves a url given a string and a settings. Raises TypeError when parameters are wrong, unless the must_raise parameter is False""" if not isinstance(settings, Settings): if must_raise: raise TypeError( 'PageRegistry.resolve takes a pyccuracy.common.Settings object first parameter. Got %r.' % settings) else: return None if not isinstance(url, basestring): if must_raise: raise TypeError( 'PageRegistry.resolve argument 2 must be a string. Got %r.' % url) else: return None klass_object = cls.get_by_name(url) or cls.get_by_url(url) if klass_object: url = klass_object.url url_pieces = [] if not url.startswith("http"): if settings.base_url: url_pieces.append(settings.base_url) else: url_pieces.append( settings.tests_dirs[0]) #gotta think of a way to fix this if klass_object: url_pieces.append(klass_object.url) if hasattr(klass_object, 'port') and url_pieces[0].startswith("http"): url_pieces[0] = "%s:%d" % (url_pieces[0], klass_object.port) else: url_pieces.append(url) # if use os.path.join here, will not work on windows fix = lambda x: x.replace('//', '/').replace( 'http:/', 'http://').replace('https:/', 'https://') final_url = fix("/".join(url_pieces)) if not "://" in final_url: almost_final_url = (final_url.startswith("/") and final_url) or "/%s" % final_url final_url = "file://%s" % abspath_func(almost_final_url) if final_url.startswith("/"): final_url = final_url[1:] checker = URLChecker() checker.set_url(final_url) if not checker.is_valid(): error_message = "The url %r is not valid." % final_url if klass_object: error_message += " In class %s, path %r" % ( klass_object.__name__, klass_object.__module__) if not final_url.startswith('file://'): raise InvalidUrlError(error_message) return klass_object, final_url