From e50610bd3a8c5a5bad3f19353ce5214f11333e74 Mon Sep 17 00:00:00 2001 From: Elias Dorneles Date: Tue, 11 Aug 2015 14:06:24 -0300 Subject: [PATCH] set base_url in kwargs to be fully backward compatible --- scrapy/selector/unified.py | 1 + tests/test_selector.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/scrapy/selector/unified.py b/scrapy/selector/unified.py index a8f80c84d..25cf6c98d 100644 --- a/scrapy/selector/unified.py +++ b/scrapy/selector/unified.py @@ -46,6 +46,7 @@ class Selector(ParselSelector, object_ref): if response is not None: text = response.body_as_unicode() + kwargs.setdefault('base_url', response.url) self.response = response super(Selector, self).__init__(text=text, type=st, root=root, **kwargs) diff --git a/tests/test_selector.py b/tests/test_selector.py index 2d6d8c439..19b807a3f 100644 --- a/tests/test_selector.py +++ b/tests/test_selector.py @@ -33,6 +33,13 @@ class SelectorTestCase(unittest.TestCase): self.assertEqual([x.extract() for x in sel.xpath("concat(//input[@name='a']/@value, //input[@name='b']/@value)")], [u'12']) + def test_root_base_url(self): + body = b'
' + url = "http://example.com" + response = TextResponse(url=url, body=body, encoding='utf-8') + sel = Selector(response) + self.assertEqual(url, sel.root.base) + def test_deprecated_root_argument(self): with warnings.catch_warnings(record=True) as w: root = etree.fromstring(u'')