def normalize_url(url): """ Returns the given URL with all query keys properly escaped. Args: url (str): The URL to normalize. Returns: str: The normalized URL. """ uri = urlparse(url) query = uri.query or "" pairs = parse_qsl(query) decoded_pairs = [(unquote(key), value) for key, value in pairs] encoded_pairs = [(quote(key), value) for key, value in decoded_pairs] normalized_query = urlencode(encoded_pairs) return ParseResult(scheme=uri.scheme, netloc=uri.netloc, path=uri.path, params=uri.params, query=normalized_query, fragment=uri.fragment).geturl()
def resolves_for(self, session): """ Returns whether this query resolves for the given session. Args: session (Session): The session for which this query should be executed. Returns: bool: Whether this query resolves. """ if self.url: self.actual_path = session.current_url else: result = urlparse(session.current_url) if self.only_path: self.actual_path = result.path else: request_uri = result.path if result.query: request_uri += "?{0}".format(result.query) self.actual_path = request_uri if isregex(self.expected_path): return self.expected_path.search(self.actual_path) else: return normalize_url(self.actual_path) == normalize_url( self.expected_path)
def test_fetches_a_response_when_absolute_uri_does_not_have_a_trailing_slash( self, session): # Preparation session.visit("/") root_uri = urlparse(session.current_url) session.visit("http://{}".format(root_uri.netloc)) assert session.has_text("Hello world!")
def current_path(self): """ str: Path of the current page, without any domain information. """ if not self.current_url: return path = urlparse(self.current_url).path return path if path else None
def test_fetches_a_response_from_the_driver_with_an_absolute_url_with_a_port( self, session): # Preparation session.visit("/") root_uri = urlparse(session.current_url) session.visit("http://{}/".format(root_uri.netloc)) assert session.has_text("Hello world!") session.visit("http://{}/foo".format(root_uri.netloc)) assert session.has_text("Another World")
def current_host(self): """ str: Host of the current page. """ if not self.current_url: return result = urlparse(self.current_url) scheme, netloc = result.scheme, result.netloc host = netloc.split(":")[0] if netloc else None return "{0}://{1}".format(scheme, host) if host else None
def visit(self, visit_uri): """ Navigate to the given URL. The URL can either be a relative URL or an absolute URL. The behavior of either depends on the driver. :: session.visit("/foo") session.visit("http://google.com") For drivers which can run against an external application, such as the Selenium driver, giving an absolute URL will navigate to that page. This allows testing applications running on remote servers. For these drivers, setting :data:`capybara.app_host` will make the remote server the default. For example:: capybara.app_host = "http://google.com" session.visit("/") # visits the Google homepage Args: visit_uri (str): The URL to navigate to. """ self.raise_server_error() visit_uri = urlparse(visit_uri) if capybara.app_host: uri_base = urlparse(capybara.app_host) elif self.server: uri_base = urlparse("http://{}:{}".format(self.server.host, self.server.port)) else: uri_base = None visit_uri = ParseResult(scheme=visit_uri.scheme or (uri_base.scheme if uri_base else ""), netloc=visit_uri.netloc or (uri_base.netloc if uri_base else ""), path=visit_uri.path, params=visit_uri.params, query=visit_uri.query, fragment=visit_uri.fragment) self.driver.visit(visit_uri.geturl())
def _process(self, method, path, params=None, headers=None): self._reset_cache() requested_uri = urlparse(path) base_uri = ParseResult(scheme=requested_uri.scheme or self._current_scheme, netloc=requested_uri.netloc or self._current_netloc, path="", params="", query="", fragment="") requested_path = (self._request_path if path.startswith("?") else requested_uri.path) path_uri = ParseResult(scheme="", netloc="", path=requested_path, params=requested_uri.params, query=requested_uri.query, fragment="") self._current_scheme = base_uri.scheme self._current_netloc = base_uri.netloc env_options = dict(method=method, path=path_uri.geturl(), base_url=base_uri.geturl() or None, data=params, headers=headers) self._last_request_env_options = env_options env = create_environ(**env_options) self.last_request = Request(env) self.last_response = self.client.open(env)
def _reset_host(self): if capybara.app_host: uri = urlparse(capybara.app_host) self._current_scheme = uri.scheme self._current_netloc = uri.netloc