Changeset 220:be408949926d

Show
Ignore:
Timestamp:
2007-08-10 18:40:16 (1 year ago)
Author:
Stefan Schwarzer <sschwarzer@sschwarzer.net>
branch:
default
Message:
Added implementation and tests for method `add_to_url`.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • session.py

    r219 r220  
     1# coding: utf-8 
    12# Copyright (C) 2007, Stefan Schwarzer 
    23# 
     
    2223 
    2324import cgi 
     25import urllib 
    2426import urlparse 
    2527 
     
    6163                # make sure each string is a unicode string 
    6264                for index, item in enumerate(value): 
    63                     if not isinstance(value, unicode): 
    64                         value[index] = coding.decode(item) 
     65                    value[index] = coding.decode(item) 
    6566                # turn one-element lists into scalars 
    6667                if len(value) == 1: 
     
    7172    def add_to_url(self, url, params): 
    7273        """ 
    73         Return an updated URL so that 
     74        Return an updated URL so that the parameter values from the 
     75        dictionary `params` is included in the query string of the 
     76        original URL. Only parameters from the instance variable 
     77        `params` are considered. 
     78 
     79        Example of method usage: 
     80 
     81        >>> s = Session(['a']) 
     82        >>> s.add_to_url("/page", {'a': u"Test"}) 
     83        '/page?a=Test' 
    7484        """ 
     85        parsed_url = list(urlparse.urlparse(url)) 
     86        query_string = urlparse.urlparse(url)[4] 
     87        query_mapping = cgi.parse_qs(query_string) 
     88        # use only key/value pairs qualified in `params` 
     89        for param in self.params: 
     90            if param in params: 
     91                value = params[param] 
     92                # make sure the value is a list 
     93                if isinstance(value, basestring): 
     94                    value = [value] 
     95                # if necessary, encode unicode values 
     96                for index, item in enumerate(value): 
     97                    if isinstance(item, unicode): 
     98                        value[index] = coding.encode(item) 
     99                query_mapping[param] = value 
     100        query_string = urllib.urlencode(query_mapping, doseq=True) 
     101        parsed_url[4] = query_string 
     102        return urlparse.urlunparse(parsed_url).replace("+", "%20") 
    75103 
  • test_session.txt

    r219 r220  
    1919 
    2020    >>> s = session.Session([]) 
    21     >>> s.get_from_url(u"http://localhost/page?a=1&b=2") 
     21    >>> s.get_from_url("http://localhost/page?a=1&b=2") 
    2222    {} 
    2323 
     
    2626 
    2727    >>> s = session.Session(['a']) 
    28     >>> s.get_from_url(u"http://localhost/page?a=1&b=2") 
     28    >>> s.get_from_url("http://localhost/page?a=1&b=2") 
    2929    {'a': u'1'} 
    3030 
     
    3838 
    3939    >>> s = session.Session(['a']) 
    40     >>> s.get_from_url(u"/page?a=Test%20string&b=2") 
     40    >>> s.get_from_url("/page?a=Test%20string&b=2") 
    4141    {'a': u'Test string'} 
    4242 
     
    4545 
    4646    >>> s = session.Session(['a', 'b']) 
    47     >>> s.get_from_url(u"/page?b=2") 
     47    >>> s.get_from_url("/page?b=2") 
    4848    {'b': u'2'} 
    4949 
     
    5252 
    5353    >>> s = session.Session(['a']) 
    54     >>> params = s.get_from_url(u"/page?a=Test&a=1") 
     54    >>> params = s.get_from_url("/page?a=Test&a=1") 
    5555    >>> params.keys() 
    5656    ['a'] 
     
    5858    [u'1', u'Test'] 
    5959 
    60 Testing the ``put_into_url`` method 
    61 ----------------------------------- 
     60Testing the ``add_to_url`` method 
     61--------------------------------- 
    6262 
    63 The ``put_into_url`` is the reverse of ``get_from_url``,  
     63The ``add_to_url`` is the reverse of ``get_from_url``. That is, the 
     64query string of the URL is updated to contain the values from the 
     65passed-in dictionary. The return value is an URL (unicode string). 
     66 
     67A simple example:: 
     68 
     69    >>> s = session.Session(['a']) 
     70    >>> s.add_to_url("/page", {'a': '1'}) 
     71    '/page?a=1' 
     72 
     73If the parameter is already present in the URL, it's overwritten:: 
     74 
     75    >>> s = session.Session(['a']) 
     76    >>> s.add_to_url("/page?a=1", {'a': u'2'}) 
     77    '/page?a=2' 
     78 
     79Parameters which are not in the instance variable ``params`` are 
     80ignored:: 
     81 
     82    >>> s = session.Session(['a']) 
     83    >>> s.add_to_url("/page", {'b': '1'}) 
     84    '/page' 
     85 
     86Parameter values containing special characters are properly encoded:: 
     87 
     88    >>> s = session.Session(['a']) 
     89    >>> s.add_to_url("/page?a=1", {'a': u'Test & string'}) 
     90    '/page?a=Test%20%26%20string' 
     91 
     92Parameters which are lists are converted to several "assignments" in 
     93the query string:: 
     94 
     95    >>> s = session.Session(['a']) 
     96    >>> s.add_to_url("/page?b=1", {'a': [u'1', u"2"]}) 
     97    '/page?a=1&a=2&b=1' 
     98 
     99(The last test may fail for another Python version since it depends 
     100on dictionary "order" and module implementations.) 
     101