testtools.py (1522B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\werkzeug\contrib\testtools.py 2 from werkzeug import Response, cached_property, import_string 3 4 class ContentAccessors(object): 5 6 def xml(self): 7 if 'xml' not in self.mimetype: 8 raise AttributeError('Not a XML response (Content-Type: %s)' % self.mimetype) 9 for module in ['xml.etree.ElementTree', 'ElementTree', 'elementtree.ElementTree']: 10 etree = import_string(module, silent=True) 11 if etree is not None: 12 return etree.XML(self.body) 13 14 raise RuntimeError('You must have ElementTree installed to use TestResponse.xml') 15 16 xml = cached_property(xml) 17 18 def lxml(self): 19 if 'html' not in self.mimetype and 'xml' not in self.mimetype: 20 raise AttributeError('Not an HTML/XML response') 21 from lxml import etree 22 try: 23 from lxml.html import fromstring 24 except ImportError: 25 fromstring = etree.HTML 26 27 if self.mimetype == 'text/html': 28 return fromstring(self.data) 29 return etree.XML(self.data) 30 31 lxml = cached_property(lxml) 32 33 def json(self): 34 if 'json' not in self.mimetype: 35 raise AttributeError('Not a JSON response') 36 try: 37 from simplejson import loads 38 except: 39 from json import loads 40 41 return loads(self.data) 42 43 json = cached_property(json) 44 45 46 class TestResponse(Response, ContentAccessors): 47 pass