Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix changequery to make it work correctly even when the input has mul…

…ti-valued fields (Bug#118229)
  • Loading branch information...
commit 0cae750b73efaaa779909a01b30fe8faa65de23a 1 parent 8b52cf9
@anandology anandology authored
Showing with 28 additions and 5 deletions.
  1. +15 −0 test/application.py
  2. +13 −5 web/http.py
View
15 test/application.py
@@ -292,6 +292,21 @@ def unload():
app.request('/bar')
self.assertEquals(x.a, 2)
+
+ def test_changequery(self):
+ urls = (
+ '/', 'index',
+ )
+ class index:
+ def GET(self):
+ return web.changequery(x=1)
+ app = web.application(urls, locals())
+
+ def f(path):
+ return app.request(path).data
+
+ self.assertEquals(f('/?x=2'), '/?x=1')
+ self.assertEquals(f('/?y=1&y=2&x=2'), '/?y=1&y=2&x=1')
if __name__ == '__main__':
webtest.main()
View
18 web/http.py
@@ -105,15 +105,23 @@ def write(cgi_response):
web.output(body)
-def urlencode(query):
+def urlencode(query, doseq=0):
"""
Same as urllib.urlencode, but supports unicode strings.
>>> urlencode({'text':'foo bar'})
'text=foo+bar'
+ >>> urlencode({'x': [1, 2]}, doseq=True)
+ 'x=1&x=2'
"""
- query = dict([(k, utils.utf8(v)) for k, v in query.items()])
- return urllib.urlencode(query)
+ def convert(value, doseq=False):
+ if doseq and isinstance(value, list):
+ return [convert(v) for v in value]
+ else:
+ return utils.utf8(value)
+
+ query = dict([(k, convert(v, doseq)) for k, v in query.items()])
+ return urllib.urlencode(query, doseq=doseq)
def changequery(query=None, **kw):
"""
@@ -122,7 +130,7 @@ def changequery(query=None, **kw):
changed.
"""
if query is None:
- query = web.input(_method='get')
+ query = web.rawinput(method='get')
for k, v in kw.iteritems():
if v is None:
query.pop(k, None)
@@ -130,7 +138,7 @@ def changequery(query=None, **kw):
query[k] = v
out = web.ctx.path
if query:
- out += '?' + urlencode(query)
+ out += '?' + urlencode(query, doseq=True)
return out
def url(path=None, **kw):
Please sign in to comment.
Something went wrong with that request. Please try again.