예제 #1
0
파일: utils.py 프로젝트: testMrinal/oppia
def set_url_query_parameter(url, param_name, param_value):
    """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)
    query_params = python_utils.parse_query_string(query_string)

    query_params[param_name] = [param_value]
    new_query_string = python_utils.url_encode(query_params, doseq=True)

    return python_utils.url_unsplit(
        (scheme, netloc, path, new_query_string, fragment))
예제 #2
0
        def getcode(self):
            """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]),
                self.url[2],
            )
            self.url[1]['recipient_variables'] = [
                ast.literal_eval(self.url[1]['recipient_variables'][0])
            ]
            return 200 if self.url == self.expected_url else 500
예제 #3
0
        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
예제 #4
0
 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']})