Show
Ignore:
Timestamp:
09/06/08 10:47:31 (2 years ago)
Author:
cmlenz
Message:

Divan: Add some very basic tests for the OpenID support.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/examples/divan/divan/comments.py

    r200 r201  
    9898            'Host': get_hostname(comment.openid_server) 
    9999        }) 
    100         result = parse_kv_encoding(body.strip()) 
     100        result = decode_kvform(body.strip()) 
    101101        if result['is_valid'] == 'true': 
    102102            log.info('OpenID authentication for %r validated by %r', 
     
    201201 
    202202def get_hostname(url): 
     203    """Extract the host name from the given URL, which does not include the 
     204    port for HTTP and HTTPS URLs using the respective default port. 
     205     
     206    >>> get_hostname('http://me.yahoo.com/') 
     207    'me.yahoo.com' 
     208    >>> get_hostname('http://me.yahoo.com:80/') 
     209    'me.yahoo.com' 
     210    >>> get_hostname('https://me.yahoo.com:443/') 
     211    'me.yahoo.com' 
     212     
     213    :param url: the URL from which to extract the host name 
     214    :return: the extracted host name 
     215    """ 
    203216    scheme, netloc, path, query, fragment = list(urlsplit(url)) 
    204217    if ':' in netloc: 
    205218        hostname, port = netloc.split(':') 
    206         if port == '80' and scheme == 'https': 
     219        if port == '80' and scheme == 'http': 
    207220            port = None 
    208221        elif port == '443' and scheme == 'https': 
     
    213226 
    214227def normalize_openid(url): 
     228    """Normalize the given OpenID URL. 
     229     
     230    >>> normalize_openid('me.yahoo.com') 
     231    'http://me.yahoo.com/' 
     232    >>> normalize_openid('https://me.yahoo.com') 
     233    'https://me.yahoo.com/' 
     234    >>> normalize_openid('https://me.yahoo.com/joe') 
     235    'https://me.yahoo.com/joe' 
     236     
     237    :param url: the URL to normalize 
     238    :return: the normalized URL 
     239    """ 
    215240    if '://' not in url: 
    216241        url = 'http://%s' % url 
     
    221246 
    222247 
    223 def parse_kv_encoding(string): 
    224     lines = string.splitlines() 
     248def decode_kvform(string): 
     249    """Parse a key-value form encoded string as defined in the OpenID 
     250    specification, and return a dictionary mapping values to keys. 
     251     
     252    >>> encoded = 'ns:http://specs.openid.net/auth/2.0\\nis_valid:true' 
     253    >>> parsed = decode_kvform(encoded) 
     254    >>> parsed['ns'] 
     255    u'http://specs.openid.net/auth/2.0' 
     256    >>> parsed['is_valid'] 
     257    u'true' 
     258     
     259    :param string: the key-value form encoded string 
     260    :return: dictionary mapping values to keys 
     261    """ 
     262    if not isinstance(string, unicode): 
     263        string = string.decode('utf-8') 
     264    string = string.strip() 
    225265    retval = {} 
    226     for line in lines: 
     266    for line in string.split('\n'): 
    227267        key, value = line.split(':', 1) 
    228         retval[key] = value 
     268        retval[key.strip()] = value.strip() 
    229269    return retval 
    230270