Changeset 220:be408949926d
- 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
| r219 |
r220 |
|
| | 1 | # coding: utf-8 |
|---|
| 1 | 2 | # Copyright (C) 2007, Stefan Schwarzer |
|---|
| 2 | 3 | # |
|---|
| … | … | |
| 22 | 23 | |
|---|
| 23 | 24 | import cgi |
|---|
| | 25 | import urllib |
|---|
| 24 | 26 | import urlparse |
|---|
| 25 | 27 | |
|---|
| … | … | |
| 61 | 63 | # make sure each string is a unicode string |
|---|
| 62 | 64 | for index, item in enumerate(value): |
|---|
| 63 | | if not isinstance(value, unicode): |
|---|
| 64 | | value[index] = coding.decode(item) |
|---|
| | 65 | value[index] = coding.decode(item) |
|---|
| 65 | 66 | # turn one-element lists into scalars |
|---|
| 66 | 67 | if len(value) == 1: |
|---|
| … | … | |
| 71 | 72 | def add_to_url(self, url, params): |
|---|
| 72 | 73 | """ |
|---|
| 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' |
|---|
| 74 | 84 | """ |
|---|
| | 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") |
|---|
| 75 | 103 | |
|---|
| r219 |
r220 |
|
| 19 | 19 | |
|---|
| 20 | 20 | >>> 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") |
|---|
| 22 | 22 | {} |
|---|
| 23 | 23 | |
|---|
| … | … | |
| 26 | 26 | |
|---|
| 27 | 27 | >>> 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") |
|---|
| 29 | 29 | {'a': u'1'} |
|---|
| 30 | 30 | |
|---|
| … | … | |
| 38 | 38 | |
|---|
| 39 | 39 | >>> 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") |
|---|
| 41 | 41 | {'a': u'Test string'} |
|---|
| 42 | 42 | |
|---|
| … | … | |
| 45 | 45 | |
|---|
| 46 | 46 | >>> s = session.Session(['a', 'b']) |
|---|
| 47 | | >>> s.get_from_url(u"/page?b=2") |
|---|
| | 47 | >>> s.get_from_url("/page?b=2") |
|---|
| 48 | 48 | {'b': u'2'} |
|---|
| 49 | 49 | |
|---|
| … | … | |
| 52 | 52 | |
|---|
| 53 | 53 | >>> 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") |
|---|
| 55 | 55 | >>> params.keys() |
|---|
| 56 | 56 | ['a'] |
|---|
| … | … | |
| 58 | 58 | [u'1', u'Test'] |
|---|
| 59 | 59 | |
|---|
| 60 | | Testing the ``put_into_url`` method |
|---|
| 61 | | ----------------------------------- |
|---|
| | 60 | Testing the ``add_to_url`` method |
|---|
| | 61 | --------------------------------- |
|---|
| 62 | 62 | |
|---|
| 63 | | The ``put_into_url`` is the reverse of ``get_from_url``, |
|---|
| | 63 | The ``add_to_url`` is the reverse of ``get_from_url``. That is, the |
|---|
| | 64 | query string of the URL is updated to contain the values from the |
|---|
| | 65 | passed-in dictionary. The return value is an URL (unicode string). |
|---|
| | 66 | |
|---|
| | 67 | A simple example:: |
|---|
| | 68 | |
|---|
| | 69 | >>> s = session.Session(['a']) |
|---|
| | 70 | >>> s.add_to_url("/page", {'a': '1'}) |
|---|
| | 71 | '/page?a=1' |
|---|
| | 72 | |
|---|
| | 73 | If 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 | |
|---|
| | 79 | Parameters which are not in the instance variable ``params`` are |
|---|
| | 80 | ignored:: |
|---|
| | 81 | |
|---|
| | 82 | >>> s = session.Session(['a']) |
|---|
| | 83 | >>> s.add_to_url("/page", {'b': '1'}) |
|---|
| | 84 | '/page' |
|---|
| | 85 | |
|---|
| | 86 | Parameter 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 | |
|---|
| | 92 | Parameters which are lists are converted to several "assignments" in |
|---|
| | 93 | the 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 |
|---|
| | 100 | on dictionary "order" and module implementations.) |
|---|
| | 101 | |
|---|