def test_valid(self):
     assert_equal(to_utf8_if_unicode(unicode_string), utf8_bytes)
     assert_equal(to_utf8_if_unicode(utf8_bytes), utf8_bytes)
     assert_equal(to_utf8_if_unicode(None), None)
     assert_equal(to_utf8_if_unicode(False), False)
     assert_equal(to_utf8_if_unicode(5), 5)
     assert_equal(to_utf8_if_unicode([]), [])
     assert_equal(to_utf8_if_unicode(()), ())
     assert_equal(to_utf8_if_unicode({}), {})
     assert_equal(to_utf8_if_unicode(object), object)
Beispiel #2
0
 def test_oauth_test_cases(self):
     # http://wiki.oauth.net/w/page/12238556/TestCases
     ex = [
         ('abcABC123', 'abcABC123'),
         ('-._~', '-._~'),
         ('%', '%25'),
         ('+', '%2B'),
         ('&=*', '%26%3D%2A'),
         (u'\u000A', '%0A'),
         (u'\u0020', '%20'),
         (u'\u007F', '%7F'),
         (u'\u0080', '%C2%80'),
         (u'\u3001', '%E3%80%81'),
     ]
     for k, v in ex:
         assert_equal(percent_decode(v), to_utf8_if_unicode(k))
Beispiel #3
0
def parse_qs(query_string):
    """
    Parses a query parameter string according to the OAuth spec.

    Use only with OAuth query strings.

    :see: Parameter Sources (http://tools.ietf.org/html/rfc5849#section-3.4.1.3.1)
    :param query_string:
        Query string to parse. If ``query_string`` starts with a ``?`` character
        it will be ignored for convenience.
    """
    query_string = to_utf8_if_unicode(query_string) or ""
    if query_string.startswith("?"):
        logging.warning("Ignoring `?` query string prefix -- `%r`" % query_string)
        query_string = query_string[1:]
    return _parse_qs(query_string, keep_blank_values=True)
Beispiel #4
0
def percent_encode(value):
    """
    Percent-encodes according to the OAuth spec.

    Used in constructing the signature base string and the "Authorization"
    header field.

    :see: Percent Encoding (http://tools.ietf.org/html/rfc5849#section-3.6)
    :param value:
        Query string parameter value to escape. If the value is a Unicode
        string, it will be encoded to UTF-8. A byte string is considered
        exactly that, a byte string and will not be UTF-8 encoded—however, it
        will be percent-encoded.
    :returns:
        Percent-encoded string.
   """
    value = bytes(to_utf8_if_unicode(value))
    return quote(value, safe="~")