As a block test send request for multiple flags with the same name under webapp2

Using webapp2 I create unit tests for a form where there are flags for votes, so for the vote field you can put several values ​​and they are retrieved via request.POST.getall('vote') :

 <input type="checkbox" name="vote" value="Better"> <input type="checkbox" name="vote" value="Faster"> <input type="checkbox" name="vote" value="Stronger"> 

In unit test, I tried passing a list:

 response = app.get_response('/vote', POST={'vote': [u'Better', u'Faster', u'Stronger']}, headers=[('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')] ) 

But it looks like it's just converted to a string:

 votes = self.request.POST.getall('vote') # => [u"[u'Better', u'Faster', u'Stronger']"] 

How to pass several values ​​for vote , which will be obtained as a list through request.POST.getall() ?

+4
source share
2 answers

POST data is encoded using the query string encoding, and several elements with the same name are represented by a duplicate key with different values. For instance:

 vote=Better&vote=Faster&vote=Stronger 

Python has library functions for this, but:

 urllib.urlencode({ 'vote': ['Better', 'Faster', 'Stronger'], }, True) 

The second argument ( True ) - urlencode is called the "dose" and instructs urlencode to encode sequences as lists of individual elements.

+4
source

A website library is useful for these test cases.

http://webtest.pythonpaste.org/en/latest/index.html#form-submissions

+1
source

All Articles