improved invalid xpath exception message in xpath selectors, and added unittests

This commit is contained in:
Pablo Hoffman 2009-07-11 17:19:20 -03:00
parent fc64360a34
commit 1a153d47f3
2 changed files with 18 additions and 1 deletions

View File

@ -163,6 +163,20 @@ class XPathTestCase(unittest.TestCase):
self.assertEqual(x.x("//p:SecondTestTag").x("./xmlns:price/text()")[0].extract(), '90')
self.assertEqual(x.x("//p:SecondTestTag/xmlns:material").extract()[0], '<material/>')
@libxml2debug
def test_selector_invalid_xpath(self):
response = XmlResponse(url="http://example.com", body="<html></html>")
x = HtmlXPathSelector(response)
xpath = "//test[@foo='bar]"
try:
x.x(xpath)
except ValueError, e:
assert xpath in str(e), "Exception message does not contain invalid xpath"
except Exception:
raise AssertionError("A invalid XPath does not raise ValueError")
else:
raise AssertionError("A invalid XPath does not raise an exception")
@libxml2debug
def test_http_header_encoding_precedence(self):
# u'\xa3' = pound symbol in unicode

View File

@ -45,7 +45,10 @@ class XPathSelector(object):
return a XPathSelectorList of the result"""
if hasattr(self.xmlNode, 'xpathEval'):
self.doc.xpathContext.setContextNode(self.xmlNode)
xpath_result = self.doc.xpathContext.xpathEval(xpath)
try:
xpath_result = self.doc.xpathContext.xpathEval(xpath)
except libxml2.xpathError:
raise ValueError("Invalid XPath: %s" % xpath)
cls = type(self)
if hasattr(xpath_result, '__iter__'):
return XPathSelectorList([cls(node=node, parent=self, expr=xpath, response=self.response)