Esempio n. 1
0
 def test_detects_sequences(self):
   self.assertTrue(builtins.is_sequence([1, ]))
   self.assertTrue(builtins.is_sequence((1,)))
   self.assertTrue(builtins.is_sequence(""))
   self.assertTrue(builtins.is_sequence({}))
   self.assertTrue(builtins.is_sequence(dict()))
   self.assertTrue(builtins.is_sequence(set([1, 2])))
   self.assertTrue(builtins.is_sequence(frozenset([1, 2])))
Esempio n. 2
0
 def test_detects_sequences(self):
   self.assertTrue(is_sequence([1, ]))
   self.assertTrue(is_sequence((1,)))
   self.assertTrue(is_sequence(""))
   self.assertTrue(is_sequence({}))
   self.assertTrue(is_sequence(dict()))
   self.assertTrue(is_sequence(set([1, 2])))
   self.assertTrue(is_sequence(frozenset([1, 2])))
Esempio n. 3
0
def urlencode_sl(query_params, predicate=None):
    """
    Serializes a dictionary of query parameters into a list of query
    parameters, ``(name, value)`` pairs, sorted first by ``name`` then by
    ``value`` based on the OAuth percent-encoding rules and specification.

    Behaves like :func:`urllib.urlencode` with ``doseq=1``.

    :param query_params:
        Dictionary of query parameters.
    :param predicate:
        A callback that will be called for each query parameter and should
        return ``False`` or a falsy value if that parameter should not be
        included. By default, all query parameters are included. The function
        takes the following method signature::

            def predicate(name, value):
                return is_name_allowed(name) and is_value_allowed(value)
    :returns:
        A list of query parameters, ``(name, value)`` pairs, sorted first by
        ``name`` and then by ``value`` based on the OAuth percent-encoding rules
        and specification.
    """
    query_params = query_params or {}
    encoded_pairs = []
    for k, value in query_params.items():
        # Keys are also percent-encoded according to OAuth spec.
        key = percent_encode(k)
        if predicate and not predicate(k, value):
            continue
        elif is_bytes_or_unicode(value):
            encoded_pairs.append((key, percent_encode(value),))
        elif is_sequence(value):
            # Loop over the sequence.
            if len(value) > 0:
                for i in value:
                    encoded_pairs.append((key, percent_encode(i), ))
            # ``urllib.urlencode()`` doesn't preserve blank lists.
            # Therefore, we're discarding them.
            #else:
            #    # Preserve blank list values.
            #    encoded_pairs.append((k, "", ))
        else:
            encoded_pairs.append((key, percent_encode(value),))
    # Sort after encoding according to the OAuth spec.
    return sorted(encoded_pairs)
Esempio n. 4
0
 def test_rejects_non_sequences(self):
   self.assertFalse(builtins.is_sequence(False))
   self.assertFalse(builtins.is_sequence(True))
   self.assertFalse(builtins.is_sequence(None))
   self.assertFalse(builtins.is_sequence(5))
   self.assertFalse(builtins.is_sequence(Test_is_sequence))
Esempio n. 5
0
 def test_rejects_non_sequences(self):
   self.assertFalse(is_sequence(False))
   self.assertFalse(is_sequence(True))
   self.assertFalse(is_sequence(None))
   self.assertFalse(is_sequence(5))
   self.assertFalse(is_sequence(Test_is_sequence))