The response object is based on webob.response.Response with some additions to help with testing.
The inherited attributes that are most interesting:
The added methods:
Footnotes
[1] | The whitespace normalization replace sequences of whitespace characters and \n \r \t by a single space. |
There are several ways to get parsed versions of the response. These are the attributes:
Return a BeautifulSoup version of the response body:
>>> res = app.get('/index.html')
>>> res.html
<html><body><div id="content">hey!</div></body></html>
>>> res.html.__class__
<class '...BeautifulSoup'>
Return an ElementTree version of the response body:
>>> res = app.get('/document.xml')
>>> res.xml
<Element 'xml' ...>
>>> res.xml[0].tag
'message'
>>> res.xml[0].text
'hey!'
Return an lxml version of the response body:
>>> res = app.get('/index.html')
>>> res.lxml
<Element html at ...>
>>> res.lxml.xpath('//body/div')[0].text
'hey!'
>>> res = app.get('/document.xml')
>>> res.lxml
<Element xml at ...>
>>> res.lxml[0].tag
'message'
>>> res.lxml[0].text
'hey!'
Return an PyQuery version of the response body:
>>> res.pyquery('message')
[<message>]
>>> res.pyquery('message').text()
'hey!'
Return the parsed JSON (parsed with simplejson):
>>> res = app.get('/object.json')
>>> sorted(res.json.values())
[1, 2]
In each case the content-type must be correct or an AttributeError is raised. If you do not have the necessary library installed (none of them are required by WebTest), you will get an ImportError.