Exemple #1
0
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()
Exemple #2
0
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()
Exemple #3
0
    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