def set_url_query_parameter( url: str, param_name: str, param_value: str ) -> str: """Set or replace a query parameter, and return the modified URL. Args: url: str. URL string which contains the query parameter. param_name: str. Parameter name to be removed. param_value: str. Set the parameter value, if it exists. Returns: str. Formated URL that has query parameter set or replaced. Raises: Exception. If the query parameter sent is not of string type, them this exception is raised. """ if not isinstance(param_name, python_utils.BASESTRING): raise Exception( 'URL query parameter name must be a string, received %s' % param_name) scheme, netloc, path, query_string, fragment = python_utils.url_split(url) # type: ignore[no-untyped-call] query_params = python_utils.parse_query_string(query_string) # type: ignore[no-untyped-call] query_params[param_name] = [param_value] new_query_string = python_utils.url_encode(query_params, doseq=True) # type: ignore[no-untyped-call] return python_utils.url_unsplit( # type: ignore[no-any-return, no-untyped-call] (scheme, netloc, path, new_query_string, fragment))
def getcode(self) -> int: """Gets the status code of this url_open mock. Returns: int. 200 to signify status is OK. 500 otherwise. """ self.url = ( self.url[0], python_utils.parse_query_string( self.url[1]), # type: ignore[no-untyped-call] self.url[2], ) recipient_variable_0 = self.url[1]['recipient_variables'][0] # Letting mypy know that the variable is of type str. assert isinstance(recipient_variable_0, str) self.url[1]['recipient_variables'] = [ ast.literal_eval(recipient_variable_0) ] return 200 if self.url == self.expected_url else 500
def test_parse_query_string(self): response = python_utils.parse_query_string( 'http://www.google.com?search=oppia') self.assertEqual(response, {'http://www.google.com?search': ['oppia']})