Python six.moves.urllib.parse.parse_qsl() Examples

The following are 30 code examples of six.moves.urllib.parse.parse_qsl(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six.moves.urllib.parse , or try the search function .
Example #1
Source File: test_example_app.py    From Flask-pyoidc with Apache License 2.0 6 votes vote down vote up
def test_error_view(self):
        client = app.test_client()

        auth_redirect = client.get('/')
        parsed_auth_request = dict(parse_qsl(urlparse(auth_redirect.location).query))

        # fake auth error response sent to redirect_uri
        error_auth_response = {
            'error': 'invalid_request',
            'error_description': 'test error',
            'state': parsed_auth_request['state']
        }
        error_page = client.get('/redirect_uri?{}'.format(urlencode(error_auth_response)), follow_redirects=True)

        assert json.loads(error_page.data.decode('utf-8')) == {
            'error': error_auth_response['error'],
            'message': error_auth_response['error_description']
        } 
Example #2
Source File: test_spark.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def proxy_with_warning_page_mock(url, *args, **kwargs):
    cookies = kwargs.get('cookies') or {}
    proxy_cookie = cookies.get('proxy_cookie')
    url_parts = list(urlparse(url))
    query = dict(parse_qsl(url_parts[4]))
    if proxy_cookie and query.get('proxyapproved') == 'true':
        del query['proxyapproved']
        url_parts[4] = urlencode(query)
        return standalone_requests_get_mock(urlunparse(url_parts), *args[1:], **kwargs)
    else:
        # Display the html warning page with the redirect link
        query['proxyapproved'] = 'true'
        url_parts[4] = urlencode(query)
        with open(os.path.join(FIXTURE_DIR, 'html_warning_page'), 'r') as f:
            body = f.read().replace('$REDIRECT_URL$', urlunparse(url_parts))
            cookies['proxy_cookie'] = 'foo'
            return MockedResponse(body, 200, cookies) 
Example #3
Source File: test_admin_delete_record.py    From personfinder with Apache License 2.0 6 votes vote down vote up
def test_post(self):
        """Tests POST requests to delete a record."""
        get_doc = self.to_doc(self.client.get(
            '/global/admin/delete_record/', secure=True))
        xsrf_token = get_doc.cssselect_one('input[name="xsrf_token"]').get(
            'value')
        post_resp = self.client.post('/haiti/admin/delete_record/', {
            'xsrf_token': xsrf_token,
            'id': self.person.record_id,
        }, secure=True)
        # Check that the user's redirected to the repo's main admin page.
        self.assertIsInstance(post_resp, django.http.HttpResponseRedirect)
        parse = urlparse.urlparse(post_resp.url)
        self.assertEqual('/haiti/delete', parse.path)
        query_params = dict(urlparse.parse_qsl(parse.query))
        self.assertEqual(query_params['id'], self.person.record_id)
        # Check that the signature param is present and not the empty string.
        self.assertTrue(len(query_params['signature']) > 1) 
Example #4
Source File: utils.py    From rsocks with MIT License 6 votes vote down vote up
def parse_proxy_uri(uri):
    uri = urlparse(uri)
    proxy_options = dict(parse_qsl(uri.query))
    proxy_type = {
        'SOCKS4': socks.PROXY_TYPE_SOCKS4,
        'SOCKS5': socks.PROXY_TYPE_SOCKS5,
    }.get(uri.scheme.upper())

    if not proxy_type:
        raise ValueError('%r is not supported proxy protocol' % uri.scheme)
    if not uri.hostname:
        raise ValueError('hostname is required')

    return {
        'proxy_type': proxy_type,
        'addr': uri.hostname,
        'port': uri.port or 1080,
        'rdns': proxy_options.get('rdns', True),
        'username': uri.username,
        'password': uri.password,
    } 
Example #5
Source File: blob.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def _add_query_parameters(base_url, name_value_pairs):
    """Add one query parameter to a base URL.

    :type base_url: string
    :param base_url: Base URL (may already contain query parameters)

    :type name_value_pairs: list of (string, string) tuples.
    :param name_value_pairs: Names and values of the query parameters to add

    :rtype: string
    :returns: URL with additional query strings appended.
    """
    if len(name_value_pairs) == 0:
        return base_url

    scheme, netloc, path, query, frag = urlsplit(base_url)
    query = parse_qsl(query)
    query.extend(name_value_pairs)
    return urlunsplit((scheme, netloc, path, urlencode(query), frag)) 
Example #6
Source File: filters.py    From pagure with GNU General Public License v2.0 6 votes vote down vote up
def combine_url(url, page, pagetitle, **kwargs):
    """ Add the specified arguments in the provided kwargs dictionary to
    the given URL.
    """
    url_obj = urlparse(url)
    url = url_obj.geturl().replace(url_obj.query, "").rstrip("?")
    query = {}
    for k, v in parse_qsl(url_obj.query):
        if k in query:
            if isinstance(query[k], list):
                query[k].append(v)
            else:
                query[k] = [query[k], v]
        else:
            query[k] = v
    query[pagetitle] = page
    query.update(kwargs)
    args = ""
    for key in query:
        if isinstance(query[key], list):
            for val in query[key]:
                args += "&%s=%s" % (key, val)
        else:
            args += "&%s=%s" % (key, query[key])
    return url + "?" + args[1:] 
Example #7
Source File: api.py    From Airtest with Apache License 2.0 6 votes vote down vote up
def connect_device(uri):
    """
    Initialize device with uri, and set as current device.

    :param uri: an URI where to connect to device, e.g. `android://adbhost:adbport/serialno?param=value&param2=value2`
    :return: device instance
    :Example:
        * ``android:///`` # local adb device using default params
        * ``android://adbhost:adbport/1234566?cap_method=javacap&touch_method=adb``  # remote device using custom params
        * ``windows:///`` # local Windows application
        * ``ios:///`` # iOS device
    """
    d = urlparse(uri)
    platform = d.scheme
    host = d.netloc
    uuid = d.path.lstrip("/")
    params = dict(parse_qsl(d.query))
    if host:
        params["host"] = host.split(":")
    dev = init_device(platform, uuid, **params)
    return dev 
Example #8
Source File: http_mocks.py    From hyou with Apache License 2.0 5 votes vote down vote up
def _canonicalize_uri(uri):
    scheme, netloc, path, params, query, fragment = parse.urlparse(uri)
    if query:
        query = parse.urlencode(sorted(parse.parse_qsl(query)))
    return parse.urlunparse((scheme, netloc, path, params, query, fragment)) 
Example #9
Source File: test_example_app.py    From Flask-pyoidc with Apache License 2.0 5 votes vote down vote up
def perform_authentication(self, test_client, login_endpoint, client_id, provider_metadata):
        # index page should make auth request
        auth_redirect = test_client.get(login_endpoint)
        parsed_auth_request = dict(parse_qsl(urlparse(auth_redirect.location).query))

        now = int(time.time())
        # mock token response
        id_token = IdToken(iss=provider_metadata['issuer'],
                           aud=client_id,
                           sub=self.USER_ID,
                           exp=now + 10,
                           iat=now,
                           nonce=parsed_auth_request['nonce'])
        token_response = {'access_token': 'test_access_token', 'token_type': 'Bearer', 'id_token': id_token.to_jwt()}
        responses.add(responses.POST, provider_metadata['token_endpoint'], json=token_response)

        # mock userinfo response
        userinfo = {'sub': self.USER_ID, 'name': 'Test User'}
        responses.add(responses.GET, provider_metadata['userinfo_endpoint'], json=userinfo)

        # fake auth response sent to redirect URI
        fake_auth_response = 'code=fake_auth_code&state={}'.format(parsed_auth_request['state'])
        logged_in_page = test_client.get('/redirect_uri?{}'.format(fake_auth_response), follow_redirects=True)
        result = json.loads(logged_in_page.data.decode('utf-8'))

        assert result['access_token'] == 'test_access_token'
        assert result['id_token'] == id_token.to_dict()
        assert result['userinfo'] == {'sub': self.USER_ID, 'name': 'Test User'} 
Example #10
Source File: test_spark.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, url):
        parts = urlparse(url)
        _query = frozenset(parse_qsl(parts.query))
        _path = unquote_plus(parts.path)
        parts = parts._replace(query=_query, path=_path)
        self.parts = parts 
Example #11
Source File: test_cli10.py    From python-tackerclient with Apache License 2.0 5 votes vote down vote up
def equals(self, rhs):
        lhsp = urlparse.urlparse(self.lhs)
        rhsp = urlparse.urlparse(rhs)

        lhs_qs = urlparse.parse_qsl(lhsp.query)
        rhs_qs = urlparse.parse_qsl(rhsp.query)

        return (lhsp.scheme == rhsp.scheme and
                lhsp.netloc == rhsp.netloc and
                lhsp.path == rhsp.path and
                len(lhs_qs) == len(rhs_qs) and
                set(lhs_qs) == set(rhs_qs)) 
Example #12
Source File: model.py    From personfinder with Apache License 2.0 5 votes vote down vote up
def get_thumbnail_url(local_photo_url):
        parsed_url = list(urlparse.urlparse(local_photo_url))
        params_dict = dict(urlparse.parse_qsl(
            parsed_url[Person.URL_PARSE_QUERY_INDEX]))
        params_dict['thumb'] = 'true'
        parsed_url[Person.URL_PARSE_QUERY_INDEX] = urllib.urlencode(
            params_dict)
        return urlparse.urlunparse(parsed_url) 
Example #13
Source File: netutils.py    From oslo.utils with Apache License 2.0 5 votes vote down vote up
def params(self, collapse=True):
        """Extracts the query parameters from the split urls components.

        This method will provide back as a dictionary the query parameter
        names and values that were provided in the url.

        :param collapse: Boolean, turn on or off collapsing of query values
        with the same name. Since a url can contain the same query parameter
        name with different values it may or may not be useful for users to
        care that this has happened. This parameter when True uses the
        last value that was given for a given name, while if False it will
        retain all values provided by associating the query parameter name with
        a list of values instead of a single (non-list) value.
        """
        if self.query:
            if collapse:
                return dict(parse.parse_qsl(self.query))
            else:
                params = {}
                for (key, value) in parse.parse_qsl(self.query):
                    if key in params:
                        if isinstance(params[key], list):
                            params[key].append(value)
                        else:
                            params[key] = [params[key], value]
                    else:
                        params[key] = value
                return params
        else:
            return {} 
Example #14
Source File: utils.py    From django-cas-server with GNU General Public License v3.0 5 votes vote down vote up
def do_POST(self):
        """Called on a POST request on the BaseHTTPServer"""
        url = urlparse(self.path)
        self.params = dict(parse_qsl(url.query))
        if url.path == "/samlValidate":
            self.send_headers(200, "text/xml; charset=utf-8")
            length = int(self.headers.get('content-length'))
            root = etree.fromstring(self.rfile.read(length))
            auth_req = root.getchildren()[1].getchildren()[0]
            ticket = auth_req.getchildren()[0].text.encode("ascii")
            if (
                self.server.ticket is not None and
                self.params.get("TARGET").encode("ascii") == self.server.service and
                ticket == self.server.ticket
            ):
                self.server.ticket = None
                template = loader.get_template('cas_server/samlValidate.xml')
                context = Context({
                    'IssueInstant': timezone.now().isoformat(),
                    'expireInstant': (timezone.now() + timedelta(seconds=60)).isoformat(),
                    'Recipient': self.server.service,
                    'ResponseID': utils.gen_saml_id(),
                    'username': self.server.username.decode('utf-8'),
                    'attributes': self.server.attributes,
                    'auth_date': timezone.now().replace(microsecond=0).isoformat(),
                    'is_new_login': 'true',
                })
                self.wfile.write(return_bytes(template.render(context), "utf8"))
            else:
                template = loader.get_template('cas_server/samlValidateError.xml')
                context = Context({
                    'IssueInstant': timezone.now().isoformat(),
                    'ResponseID': utils.gen_saml_id(),
                    'code': 'BAD_SERVICE_TICKET',
                    'msg': 'Valids are (%r, %r)' % (self.server.service, self.server.ticket)
                })
                self.wfile.write(return_bytes(template.render(context), "utf8"))
        else:
            self.return_404() 
Example #15
Source File: utils.py    From django-cas-server with GNU General Public License v3.0 5 votes vote down vote up
def do_GET(self):
        """Called on a GET request on the BaseHTTPServer"""
        url = urlparse(self.path)
        self.params = dict(parse_qsl(url.query))
        if url.path == "/validate":
            self.send_headers(200, "text/plain; charset=utf-8")
            if self.test_params():
                self.wfile.write(b"yes\n" + self.server.username + b"\n")
                self.server.ticket = None
            else:
                self.wfile.write(b"no\n")
        elif url.path in {
            '/serviceValidate', '/serviceValidate',
            '/p3/serviceValidate', '/p3/proxyValidate'
        }:
            self.send_headers(200, "text/xml; charset=utf-8")
            if self.test_params():
                template = loader.get_template('cas_server/serviceValidate.xml')
                context = Context({
                    'username': self.server.username.decode('utf-8'),
                    'attributes': self.server.attributes,
                    'auth_date': timezone.now().replace(microsecond=0).isoformat(),
                    'is_new_login': 'true',
                })
                self.wfile.write(return_bytes(template.render(context), "utf8"))
            else:
                template = loader.get_template('cas_server/serviceValidateError.xml')
                context = Context({
                    'code': 'BAD_SERVICE_TICKET',
                    'msg': 'Valids are (%r, %r)' % (self.server.service, self.server.ticket)
                })
                self.wfile.write(return_bytes(template.render(context), "utf8"))
        else:
            self.return_404() 
Example #16
Source File: utils.py    From django-cas-server with GNU General Public License v3.0 5 votes vote down vote up
def do_GET(self):
        """Called on a GET request on the BaseHTTPServer"""
        self.send_response(200)
        self.send_header(b"Content-type", "text/plain")
        self.end_headers()
        self.wfile.write(b"ok")
        url = urlparse(self.path)
        params = dict(parse_qsl(url.query))
        self.server.PARAMS = params 
Example #17
Source File: utils.py    From django-cas-server with GNU General Public License v3.0 5 votes vote down vote up
def update_url(url, params):
    """
        update parameters using ``params`` in the ``url`` query string

        :param url: An URL possibily with a querystring
        :type url: :obj:`unicode` or :obj:`str`
        :param dict params: A dictionary of parameters for updating the url querystring
        :return: The URL with an updated querystring
        :rtype: unicode
    """
    if not isinstance(url, bytes):
        url = url.encode('utf-8')
    for key, value in list(params.items()):
        if not isinstance(key, bytes):
            del params[key]
            key = key.encode('utf-8')
        if not isinstance(value, bytes):
            value = value.encode('utf-8')
        params[key] = value
    url_parts = list(urlparse(url))
    query = dict(parse_qsl(url_parts[4]))
    query.update(params)
    # make the params order deterministic
    query = list(query.items())
    query.sort()
    url_query = urlencode(query)
    if not isinstance(url_query, bytes):  # pragma: no cover in python3 urlencode return an unicode
        url_query = url_query.encode("utf-8")
    url_parts[4] = url_query
    return urlunparse(url_parts).decode('utf-8') 
Example #18
Source File: test_kong_consumer.py    From ansible-kong-module with MIT License 5 votes vote down vote up
def test_configure_for_plugin(self):

		expected_url = "{}/consumers/joe/auth-key" . format (mock_kong_admin_url)
		responses.add(responses.POST, expected_url, status=201)

		data = { "key": "123" }
		response = self.api.configure_for_plugin("joe", "auth-key", data)

		assert response.status_code == 201

		body = parse_qs(responses.calls[0].request.body)
		body_exactly = parse_qsl(responses.calls[0].request.body)
		assert body['key'][0] == "123", \
			"Expect correct. data to be sent. Got: {}" . format (body_exactly) 
Example #19
Source File: deezer.py    From Dailyfresh-B2C with Apache License 2.0 5 votes vote down vote up
def request_access_token(self, *args, **kwargs):
        response = self.request(*args, **kwargs)
        return dict(parse_qsl(response.text)) 
Example #20
Source File: utils.py    From barbican with Apache License 2.0 5 votes vote down vote up
def get_limit_and_offset_from_ref(ref):
    matches = dict(parse.parse_qsl(parse.urlparse(ref).query))
    ref_limit = matches['limit']
    ref_offset = matches['offset']

    return ref_limit, ref_offset 
Example #21
Source File: seeker.py    From django-seeker with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def seeker_filter_querystring(qs, keep):
    if isinstance(keep, basestring):
        keep = [keep]
    qs_parts = [part for part in parse_qsl(qs, keep_blank_values=True) if part[0] in keep]
    return urllib.urlencode(qs_parts) 
Example #22
Source File: utils.py    From python-barbicanclient with Apache License 2.0 5 votes vote down vote up
def get_limit_and_offset_from_ref(ref):
    matches = dict(urlparse.parse_qsl(urlparse.urlparse(ref).query))
    ref_limit = matches['limit']
    ref_offset = matches['offset']

    return ref_limit, ref_offset 
Example #23
Source File: utils.py    From sgx-kms with Apache License 2.0 5 votes vote down vote up
def get_limit_and_offset_from_ref(ref):
    matches = dict(parse.parse_qsl(parse.urlparse(ref).query))
    ref_limit = matches['limit']
    ref_offset = matches['offset']

    return ref_limit, ref_offset 
Example #24
Source File: test__http.py    From python-bigquery with Apache License 2.0 5 votes vote down vote up
def test_build_api_url_w_extra_query_params(self):
        from six.moves.urllib.parse import parse_qsl
        from six.moves.urllib.parse import urlsplit

        conn = self._make_one(object())
        uri = conn.build_api_url("/foo", {"bar": "baz"})
        scheme, netloc, path, qs, _ = urlsplit(uri)
        self.assertEqual("%s://%s" % (scheme, netloc), conn.API_BASE_URL)
        self.assertEqual(path, "/".join(["", "bigquery", conn.API_VERSION, "foo"]))
        parms = dict(parse_qsl(qs))
        self.assertEqual(parms["bar"], "baz") 
Example #25
Source File: zodburi.py    From db with MIT License 5 votes vote down vote up
def resolve_uri(uri):
    scheme, netloc, path, params, query, fragment = parse.urlparse(uri)
    if params:
        raise ValueError("Unexpected URI params", params)
    if fragment:
        raise ValueError("Unexpected URI fragment", fragment)

    dbkw = {}
    options = {}
    pgq = ''

    if query:
        pgq = []
        for name, value in parse.parse_qsl(query):
            if name in storage_options:
                options[name] = storage_options[name](value)
            elif name in db_options:
                dbkw[name] = db_options[name](value)
            else:
                pgq.append(name + '=' + value)
        pgq = '?' + '&'.join(pgq) if pgq else ''

    dsn = "postgresql://" + netloc + path + pgq

    def factory():
        import newt.db
        return newt.db.storage(dsn, **options)

    return factory, dbkw 
Example #26
Source File: webservice.py    From jellyfin-kodi with GNU General Public License v3.0 5 votes vote down vote up
def get_params(self):

        ''' Get the params
        '''
        try:
            path = self.path[1:]

            if '?' in path:
                path = path.split('?', 1)[1]

            params = dict(parse_qsl(path))
        except Exception:
            params = {}

        return params 
Example #27
Source File: test__http.py    From python-storage with Apache License 2.0 5 votes vote down vote up
def test_build_api_url_w_extra_query_params(self):
        from six.moves.urllib.parse import parse_qsl
        from six.moves.urllib.parse import urlsplit

        conn = self._make_one(object())
        uri = conn.build_api_url("/foo", {"bar": "baz"})
        scheme, netloc, path, qs, _ = urlsplit(uri)
        self.assertEqual("%s://%s" % (scheme, netloc), conn.API_BASE_URL)
        self.assertEqual(path, "/".join(["", "storage", conn.API_VERSION, "foo"]))
        parms = dict(parse_qsl(qs))
        self.assertEqual(parms["bar"], "baz") 
Example #28
Source File: http.py    From wextracto with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def remove_url_params(url, params):
    parsed = urlparse(url)
    qsl = parse_qsl(parsed.query)
    for item in iteritems(params):
        qsl.remove(item)
    replaced = parsed._replace(query=urlencode(qsl))
    return urlunparse(replaced) 
Example #29
Source File: fake_client.py    From eclcli with Apache License 2.0 4 votes vote down vote up
def client_request(self, client, method, url, **kwargs):
        # Check that certain things are called correctly
        if method in ["GET", "DELETE"]:
            assert "json" not in kwargs

        # Note the call
        self.callstack.append(
            (method,
             url,
             kwargs.get("headers") or {},
             kwargs.get("json") or kwargs.get("data")))
        try:
            fixture = self.fixtures[url][method]
        except KeyError:
            pass
        else:
            return TestResponse({"headers": fixture[0],
                                 "text": fixture[1]})

        # Call the method
        args = parse.parse_qsl(parse.urlparse(url)[4])
        kwargs.update(args)
        munged_url = url.rsplit('?', 1)[0]
        munged_url = munged_url.strip('/').replace('/', '_').replace('.', '_')
        munged_url = munged_url.replace('-', '_')

        callback = "%s_%s" % (method.lower(), munged_url)

        if not hasattr(self, callback):
            raise AssertionError('Called unknown API method: %s %s, '
                                 'expected fakes method name: %s' %
                                 (method, url, callback))

        resp = getattr(self, callback)(**kwargs)
        if len(resp) == 3:
            status, headers, body = resp
        else:
            status, body = resp
            headers = {}
        return TestResponse({
            "status_code": status,
            "text": body,
            "headers": headers,
        }) 
Example #30
Source File: base.py    From personfinder with Apache License 2.0 4 votes vote down vote up
def build_absolute_path(self, path=None, repo=None, params=None):
        """Builds an absolute path, including the path prefix if required.

        Django's HttpRequest objects have a similar function, but we implement
        our own so that we can handle path prefixes correctly when they're in
        use.

        Args:
            path (str, optional): A path beginning with a slash (may include a
                query string), e.g., '/abc?x=y'. If the path argument is not
                specified or is None, the current request's path will be used.
            repo (str, optional): A repo ID. If specified, the path will be
                considered relative to the repo's route. If this is specified,
                path must also be specified.
            params (list, optional): A list of tuples of query param keys and
                values to add to the path.

        Returns:
            str: An absolute path, including the sitewide OPTIONAL_PATH_PREFIX
            if it was used with the original request (e.g.,
            '/personfinder/abc?x=y'). Does not preserve query parameters from
            the original request.
        """
        if path is None:
            assert not repo
            # request.path will already include the path prefix if it's being
            # used.
            return self.request.path
        assert path[0] == '/'
        if repo:
            path = '/%s%s' % (repo, path)
        if self._request_is_for_prefixed_path():
            res = '/%s%s' % (site_settings.OPTIONAL_PATH_PREFIX, path)
        else:
            res = path
        if params:
            url_parts = list(urlparse.urlparse(res))
            url_params = dict(urlparse.parse_qsl(url_parts[4]))
            for key, value in params:
                if value is None:
                    if key in url_params:
                        del(url_params[key])
                else:
                    url_params[key] = value
            url_parts[4] = utils.urlencode(url_params)
            res = urlparse.urlunparse(url_parts)
        return res