document and test p.encode.urlencode(quote_via=...)
| | |
| | | |
| | | def urlencode(query, doseq=True, quote_via=quote_plus): |
| | | """ |
| | | An alternate implementation of Python's stdlib `urllib.urlencode |
| | | function <http://docs.python.org/library/urllib.html>`_ which |
| | | accepts unicode keys and values within the ``query`` |
| | | dict/sequence; all Unicode keys and values are first converted to |
| | | UTF-8 before being used to compose the query string. |
| | | An alternate implementation of Python's stdlib |
| | | :func:`urllib.parse.urlencode` function which accepts unicode keys and |
| | | values within the ``query`` dict/sequence; all Unicode keys and values are |
| | | first converted to UTF-8 before being used to compose the query string. |
| | | |
| | | The value of ``query`` must be a sequence of two-tuples |
| | | representing key/value pairs *or* an object (often a dictionary) |
| | |
| | | the ``doseq=True`` mode, no matter what the value of the second |
| | | argument. |
| | | |
| | | See the Python stdlib documentation for ``urllib.urlencode`` for |
| | | more information. |
| | | Both the key and value are encoded using the ``quote_via`` function which |
| | | by default is using a similar algorithm to :func:`urllib.parse.quote_plus` |
| | | which converts spaces into '+' characters and '/' into '%2F'. |
| | | |
| | | .. versionchanged:: 1.5 |
| | | In a key/value pair, if the value is ``None`` then it will be |
| | | dropped from the resulting output. |
| | | |
| | | .. versionchanged:: 1.9 |
| | | Added the ``quote_via`` argument to allow alternate quoting algorithms |
| | | to be used. |
| | | |
| | | """ |
| | | try: |
| | | # presumed to be a dictionary |
| | |
| | | ) |
| | | |
| | | class UrlEncodeTests(unittest.TestCase): |
| | | def _callFUT(self, query, doseq=False): |
| | | def _callFUT(self, query, doseq=False, **kw): |
| | | from pyramid.encode import urlencode |
| | | return urlencode(query, doseq) |
| | | return urlencode(query, doseq, **kw) |
| | | |
| | | def test_ascii_only(self): |
| | | result = self._callFUT([('a',1), ('b',2)]) |
| | |
| | | result = self._callFUT([('a', '1'), ('b', None), ('c', None)]) |
| | | self.assertEqual(result, 'a=1&b=&c=') |
| | | |
| | | def test_quote_via(self): |
| | | def my_quoter(value): |
| | | return 'xxx' + value |
| | | result = self._callFUT([('a', '1'), ('b', None), ('c', None)], |
| | | quote_via=my_quoter) |
| | | self.assertEqual(result, 'xxxa=xxx1&xxxb=&xxxc=') |
| | | |
| | | class URLQuoteTests(unittest.TestCase): |
| | | def _callFUT(self, val, safe=''): |
| | | from pyramid.encode import url_quote |