|
1 | 1 | import re
|
2 | 2 | import warnings
|
3 | 3 | import weakref
|
| 4 | +import six |
4 | 5 | from twisted.trial import unittest
|
5 | 6 | from scrapy.exceptions import ScrapyDeprecationWarning
|
6 | 7 | from scrapy.http import TextResponse, HtmlResponse, XmlResponse
|
@@ -188,17 +189,19 @@ def test_selector_over_text(self):
|
188 | 189 | self.assertEqual(xs.xpath('.').extract(), [u'<root>lala</root>'])
|
189 | 190 |
|
190 | 191 | def test_invalid_xpath(self):
|
| 192 | + "Test invalid xpath raises ValueError with the invalid xpath" |
191 | 193 | response = XmlResponse(url="http://example.com", body="<html></html>")
|
192 | 194 | x = self.sscls(response)
|
193 | 195 | xpath = "//test[@foo='bar]"
|
194 |
| - try: |
195 |
| - x.xpath(xpath) |
196 |
| - except ValueError as e: |
197 |
| - assert xpath in str(e), "Exception message does not contain invalid xpath" |
198 |
| - except Exception: |
199 |
| - raise AssertionError("A invalid XPath does not raise ValueError") |
200 |
| - else: |
201 |
| - raise AssertionError("A invalid XPath does not raise an exception") |
| 196 | + self.assertRaisesRegexp(ValueError, re.escape(xpath), x.xpath, xpath) |
| 197 | + |
| 198 | + def test_invalid_xpath_unicode(self): |
| 199 | + "Test *Unicode* invalid xpath raises ValueError with the invalid xpath" |
| 200 | + response = XmlResponse(url="http://example.com", body="<html></html>") |
| 201 | + x = self.sscls(response) |
| 202 | + xpath = u"//test[@foo='\u0431ar]" |
| 203 | + encoded = xpath if six.PY3 else xpath.encode('unicode_escape') |
| 204 | + self.assertRaisesRegexp(ValueError, re.escape(encoded), x.xpath, xpath) |
202 | 205 |
|
203 | 206 | def test_http_header_encoding_precedence(self):
|
204 | 207 | # u'\xa3' = pound symbol in unicode
|
|
0 commit comments